0

I have a JTable, that is connected through my database,

I am initializing my table by calling this method,

private void initializeRecordsPanel(){
    disableAllPanels();        
    lblCityJail = new JLabel("Meycauayan City Jail\r\n");
    lblCityJail.setBounds(244, 11, 283, 50);
    lblCityJail.setForeground(Color.BLACK);
    lblCityJail.setFont(new Font("Tahoma", Font.BOLD, 25));
    recordsPanel.add(lblCityJail);

    btnRefresh.setVisible(true);
    try{
        connection ();                

        String columns [] = {
                "Last Name", "First Name", "Alias", "Sex", "Case1", "Case2"
        };
        int count = 0 ;

        while(rs.next()){
            count ++;
        }

        String datas[][] = new String [count] [7] ;

        int i=0;

        connection();
        while(rs.next()){
            datas[i][0]=rs.getString("lname").toString();
            datas[i][1]=rs.getString("fname").toString();
            datas[i][2]=rs.getString("alias").toString();
            datas[i][3]=rs.getString("sex").toString();
            datas[i][4]=rs.getString("case1").toString();
            datas[i][5]=rs.getString("case2").toString();
            datas[i][6]=rs.getString("ID").toString();
            i++;
        }


        scrollPane_1 = new JScrollPane();

        scrollPane_1.setBounds(48, 104, 650, 314);
        recordsPanel.add(scrollPane_1);

        tblListOfInmates = new JTable(datas, columns){
            public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
        };

        //TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tblListOfInmates.getModel());
        //tblListOfInmates.setRowSorter(sorter);

        tblListOfInmates.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try{
                    int row = tblListOfInmates.getSelectedRow();
                    Table_click = tblListOfInmates.getModel().getValueAt(row, 6).toString();


                    if(rs.next()){
                        addRecord ar = new addRecord();
                        ar.connection();
                        ar.setupFrame();
                        ar.setSelectedRowToTextFields();
                        ar.edit.setVisible(false);
                        ar.save.setVisible(false);
                        ar.btnEdit.setVisible(true);                            
                        ar.setTextFieldsEditable(false);

                        mainframe.setEnabled(false);
                        ar.addWindowListener(new WindowAdapter() {
                               public void windowClosing(WindowEvent we) {
                                  dispose();
                                  MainFrame.mainframe.setEnabled(true);
                               }
                            });
                        st.close();
                        connection();
                        }

                }catch(Exception e1){e1.printStackTrace();}
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                tblListOfInmates.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                tblListOfInmates.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }

        });
        scrollPane_1.setViewportView(tblListOfInmates);

        JLabel lblSelectToView = new JLabel("* Select to view full record");
        lblSelectToView.setBounds(47, 74, 171, 14);
        recordsPanel.add(lblSelectToView);



        st.close();
        connection();
    }catch(Exception e){e.printStackTrace();}


    recordsPanel.setVisible(true);
}

when i click on a row, a frame will pop up and contains the data that i select. and then i update the table with this code:

try{
    connection();

    String columns [] = {
            "Last Name", "First Name", "Alias", "Sex", "Case1", "Case2"
    };
    int count = 0 ;

    while(rs.next()){
        count ++;
    }

    String datas[][] = new String [count] [7] ;

    int i=0;

    connection();
    while(rs.next()){
            datas[i][0]=rs.getString("lname").toString();
            datas[i][1]=rs.getString("fname").toString();
            datas[i][2]=rs.getString("alias").toString();
            datas[i][3]=rs.getString("sex").toString();
            datas[i][4]=rs.getString("case1").toString();
            datas[i][5]=rs.getString("case2").toString();
            datas[i][6]=rs.getString("ID").toString();
            i++;
    }

    scrollPane_1 = new JScrollPane();

    scrollPane_1.setBounds(48, 104, 650, 314);
    recordsPanel.add(scrollPane_1);



    tblListOfInmates = new JTable(datas, columns){
        public boolean isCellEditable(int nRow, int nCol) {
            return false;
        }
    };
    scrollPane_1.setViewportView(tblListOfInmates);

}catch(Exception e){e.printStackTrace();}

the table is successfully updated, but when i click on a row again , it gives me this error:

java.lang.ArrayIndexOutOfBoundsException: -1
    at javax.swing.JTable$1.getValueAt(Unknown Source)
    at RecordManagementSystem.MainFrame$16.mouseClicked(MainFrame.java:805)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

on this line:

Table_click = tblListOfInmates.getModel().getValueAt(row, 6).toString();
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
dens14345
  • 192
  • 1
  • 4
  • 17
  • 1
    `scrollPane_1.setBounds(48, 104, 650, 314);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 07 '14 at 03:35
  • 1) For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). 2) Don't update the table, update the model. Everything else is automatic. – Andrew Thompson Mar 07 '14 at 03:36

2 Answers2

5

There are several problems with what you are doing...

When you load your data, you do...

scrollPane_1 = new JScrollPane();

scrollPane_1.setBounds(48, 104, 650, 314);
recordsPanel.add(scrollPane_1);

tblListOfInmates = new JTable(datas, columns){
    public boolean isCellEditable(int nRow, int nCol) {
        return false;
    }
};
//...
scrollPane_1.setViewportView(tblListOfInmates);

And when you update the table, you do this...

scrollPane_1 = new JScrollPane();

scrollPane_1.setBounds(48, 104, 650, 314);
recordsPanel.add(scrollPane_1);

tblListOfInmates = new JTable(datas, columns){
    public boolean isCellEditable(int nRow, int nCol) {
        return false;
    }
};
scrollPane_1.setViewportView(tblListOfInmates);

The first problem is, you haven't removed the previous table, so you're now overlaying components on top of each other, meaning, you have no idea which table you are actually interacting with.

The next problem is, you don't need to do all this. Simply create the JTable, apply it to the JScrollPane and add it to what ever container you need.

When you load the data, create a new TableModel for the data from the database an set it as the previously created JTable's model...

TableModel model = ...;
//...
tblListOfInmates.setModel(model);

This is the principle of the Model-View-Control pattern

This:

tblListOfInmates = new JTable(datas, columns){
    public boolean isCellEditable(int nRow, int nCol) {
        return false;
    }
};

Is a bad idea, the TableModel should control what can and can't be edited.

Take a look at How to Use Tables and Creating a Table Model for more details

You should also make use an appropriate layout manager as you don't control the screen resolution, DPI, rendering pipeline or fonts that might be in use on different systems, all of which will take your pixel perfect layout and turn into garbage...

You may also find it easier if you created a POJO representation of the data you are extract from the database, rather then relying on 2D arrays, but that's just me...

You MouseListener also worries me...

tblListOfInmates.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        try{
            int row = tblListOfInmates.getSelectedRow();
            Table_click = tblListOfInmates.getModel().getValueAt(row, 6).toString();

            if(rs.next()){

By the time the if statement is executed, you've already moved to the end of the result set...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Assuming you are using a DefaultTableModel, then you just do:

model.setRowCount(0);

Joe
  • 111
  • 3
  • 14