Hey guys, I have this code wherein I have 4 buttons to determine what set of values to display on my jtable. I've successfully displayed my data.
After running the program, clicking a button, data is displayed in the jtable. Problem is, when I minimize/maximize the window, the data disappears. I can't click the rows. I want the rows to be clickable and not disappear. I want to know how this work. Please help me guys. This is my code.

private void display(int yr){
        try {
            DefaultTableModel model = (DefaultTableModel) schedTable.getModel();
           
            int ctr = 0;

          //removes the previously selected set of data
            while(ctr < model.getRowCount()){
                model.removeRow(ctr);
            }
            ctr = 0

             PreparedStatement stmt = con.prepareStatement("select * from APP.SCHEDULE where yr=" + yr);
             ResultSet sched = stmt.executeQuery();

             while(sched.next()){
                 PreparedStatement stmt2 = con.prepareStatement("select * from APP.SUBJECT where subjectNumber='" + sched.getString("subjectNum") + "'");
                 ResultSet subj = stmt2.executeQuery();
                 subj.next();

                 model.addRow(new Object[] {
                     sched.getInt("numOfStud"),
                     subj.getString("name"),
                     subj.getInt("unit"),
                     sched.getString("day") + " " + sched.getString("startTime") + " - "                                     + sched.getString("endTime"),
                     sched.getString("room"),
                     sched.getInt("yr")
                  });
                 model.fireTableRowsInserted(ctr, ctr); 
                 ++ctr;
                 }
                   
 
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
       
    }

anyone? :(

1. Above code only show how you load data for initial display, doesn't tell us what rest of the code is doing and how the above method is integrated in that code
2. You should call a dedicated method that will return table data in correct format instead of establishing connection inside display method each time that method is called

This is the whole class.. I'm also trying to understand how mouse listeners work. I can't do it because I can't click the newly added rows.
So, here's how this works.
If user click button 1, it displays all the data assigned to button 1. If the user clicks button 2, it should remove the data in the jtable that assigned to previous button (button 1) and display the data assigned to button 2.

public class Schedule extends javax.swing.JFrame {
    private Connection con;
    private String session = null;
    

    
    public Schedule(Main main, String flag) {
        initComponents();
        
        this.con = main.con;
        if(flag.equals("addSubject"))
            session = main.session;
        
         schedTable.addMouseListener(new MouseAdapter() {
                     public void mousePressed(MouseEvent e){
                         int row = schedTable.rowAtPoint(e.getPoint());
                         int col = schedTable.columnAtPoint(e.getPoint());
                         System.out.println("Row = " + row + " Column = " + col);                    
                     }
                    
                 });
        initComponents();
           
    }

     private void display(int yr){
        try {
            DefaultTableModel model = (DefaultTableModel) schedTable.getModel();            
           
            int ctr = 0;

            while(ctr < model.getRowCount()){
                model.removeRow(ctr);
            }

             PreparedStatement stmt = con.prepareStatement("select * from APP.SCHEDULE where yr=" + yr);
             ResultSet sched = stmt.executeQuery();

             while(sched.next()){
                 PreparedStatement stmt2 = con.prepareStatement("select * from APP.SUBJECT where subjectNumber='" + sched.getString("subjectNum") + "'");
                 ResultSet subj = stmt2.executeQuery();
                 subj.next();
                 
                 Vector vec = new Vector();
                 vec.add(sched.getInt("numOfStud"));
                 vec.add(subj.getString("name"));
                 vec.add(subj.getInt("unit"));
                 vec.add(sched.getString("day") + " " + sched.getString("startTime") + " - " + sched.getString("endTime"));
                 vec.add(sched.getString("room"));
                 vec.add(sched.getInt("yr"));
                 
                 model.addRow(vec);                    
              
                 }
                   
 
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
       
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        display(1);
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        display(2);
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        display(3);
        
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        display(4);  
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Schedule(new Main(), null).setVisible(true);
            }
        });
    } 
        private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        schedTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButton6 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

I can't see there any problem :-)

^^ When I minimize/maximize the window, the added rows disappear. These rows are also not clickable. I need to click the rows for my mouse listener to work.

post here exceptions, or better would be

1) there are some NPE then you tried to assign TableModel is null

2) or ArrayIndexOfException number of Columns doesn't corresponet with SQL statement, or vice versa Resultset starts with number 1 but Vector starts with 0, check that

it successfully displays data. there are no errors when I click the button. It's just what I mentioned above, rows are not clickable.. :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.