0
votes

I have this JTable and I have a Jextfield, both of which are in 2 different jPanels. I want the user to be able to click a certain cell in the jtable and get the value of the cell into jtextfield from the row selected but in the first column, regardless of what other column the user pressed. I don't see why when I get an error when clicking on the jtable...suggestions?

Thanks in Advance ^_^

    package computerscienceia;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class NewLease2 extends jFrame {

public NewLease2(String key) throws IOException {
    try {
        initComponents();
        this.setTitle(key+"'s Available Flats");

        RandomAccessFile file = new RandomAccessFile("Ds.txt","rw");
        jPanel1.setLayout(new BorderLayout());

        JTable tbl = new JTable();
        DefaultTableModel dtm = new DefaultTableModel();            
        String colomnName[] = {"Floor number","Flat number","Area (sqm)","View","Vacant"};
        dtm.setColumnIdentifiers(colomnName);
        tbl.setEnabled(false);

       tbl.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(final MouseEvent e){
               if(e.getClickCount() == 1){
                   final JTable target = (JTable)e.getSource();
                   final int row = target.getSelectedRow();

                   final String text = (String)target.getValueAt(row, 0).toString();
                   jTextField1.setText(text);
               }
           }

       });

        int position;
        int flatnum ;
        String line;
        ArrayList allFilecontents = new ArrayList();
        ArrayList redefinedList = new ArrayList();


        while((line = file.readLine())!= null){     
            if(!line.contains("/////////")){
            allFilecontents.add(line);
            }
        }
        for(int x=0; x<allFilecontents.size(); x++){
        String line2 = (String) allFilecontents.get(x);
        //System.out.println("["+line2+"]");

        if(line2.contains("-")){
        String[] Splittedline2 = line2.split("-");
            if(Splittedline2[0].equals(key)){
             position = x;                                                      ///WHOLE Point is to Find Key in File and its position
             flatnum = Integer.parseInt(Splittedline2[3]);
             System.out.println("Position: "+position);
             System.out.println("Flatnum: "+flatnum);

             for(int i=0; i<= flatnum; i++){
                 redefinedList.add(allFilecontents.get(position+i));

            }
             break;
            }
        }
        }
            for(int e=0; e<redefinedList.size()-1; e++){ 
                Object[] SplittedE = redefinedList.get(e+1).toString().split("_");
                if(SplittedE.length == 5){
                for(int t=0; t<SplittedE.length; t++){
                    System.out.print(SplittedE[t]+",");
            }

                }

                System.out.println(SplittedE.length);//At this point all the data is at FinalList. So
                dtm.addRow(SplittedE);
            }


            tbl.setPreferredScrollableViewportSize(new Dimension(100,400));
            tbl.setFillsViewportHeight(true);
            tbl.getTableHeader().setReorderingAllowed(false);
            tbl.setModel(dtm);
            jPanel1.add(new JScrollPane(tbl));     

        file.close();
    } 
    catch (Exception e) {
        e.getCause();
    }   
}

Full Error Stacktrace: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.Vector.elementData(Vector.java:734) at java.util.Vector.elementAt(Vector.java:477) at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648) at javax.swing.JTable.getValueAt(JTable.java:2719) at computerscienceia.NewLease2$1.mouseClicked(NewLease2.java:37) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6530) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6292) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4883) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4705) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4705) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:719) at java.awt.EventQueue$4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

2
May you post here error stacktrace?Yuriy Vinogradov
We need full stack trace and also post please full NewLease2.java code with lline numbers.Yuriy Vinogradov
Check that a row is actually selected, that is row must be different from -1 .Arnaud
If you mean the headers, I haven't clicked the headers and I still get an error.YOPI

2 Answers

1
votes

You can override the isCellEditable() method as shown below to make all cells uneditable:

JTable tbl = new JTable(){
              @Override
                public boolean isCellEditable(int row, int column) {
                   //all cells false
                   return false;
                };
0
votes

I think I partly solved the issue. At one point I entered: tbl.setEnabled(false); becuase I didnt want the user to make changes to any cell. I however removed it and now its working.

However I now have a new problem. Does anyone know a way as to make the cells uneditable.