1

somehow in debugging data is fully retrieved and resultModel actually has got column names, and data for rows. Although when compiled and ran in Netbeans after a search, table disappears, no data is shown even column names. Here is the code:

private void search(){
    String[][] rowData = new String[0][4];
    String[] columns = {"appointmentid", "fname", "lname", "registration", "make", "model", "engine", "year", "mileage", "type", "date", "time"};
    resultModel = new DefaultTableModel(rowData,columns);
     add(new JScrollPane(jTable1));

    jTable1 = new JTable(resultModel);

    jTable1.setAutoCreateRowSorter(true); 

     try{
        Model_Customer[] appointment = Controller_ManageCustomer.FindCustomers(Searchtxt.getText());
        resultModel.setRowCount(0);
        for(int i = 0; i < appointment.length; i++)
            resultModel.insertRow(i,new Object[]{appointment[i].GetID(),appointment[i].GetFName(), appointment[i].GetLName(), appointment[i].GetRegistration(), appointment[i].GetMake(), appointment[i].GetModel(), appointment[i].GetEngine(), appointment[i].GetYear(), appointment[i].GetMileage(), appointment[i].GetType(), appointment[i].GetDate(), appointment[i].GetTime()});

    }catch(Exception ex){
        JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
    }

}
  DefaultTableModel resultModel

1 Answer 1

4

It could just be me...but this looks very suspicious...

add(new JScrollPane(jTable1));
jTable1 = new JTable(resultModel);

Not to mention, you go a build and nice new DefaultTableModel but don't actually apply it to anything that is actually on the screen...

Try something more like...

resultModel = new DefaultTableModel(rowData,columns);

 try{
    Model_Customer[] appointment = Controller_ManageCustomer.FindCustomers(Searchtxt.getText());
    resultModel.setRowCount(0);
    for(int i = 0; i < appointment.length; i++) {
        resultModel.insertRow(i,new Object[]{appointment[i].GetID(),appointment[i].GetFName(), appointment[i].GetLName(), appointment[i].GetRegistration(), appointment[i].GetMake(), appointment[i].GetModel(), appointment[i].GetEngine(), appointment[i].GetYear(), appointment[i].GetMileage(), appointment[i].GetType(), appointment[i].GetDate(), appointment[i].GetTime()});
    }

    if (jTable1 == null) {
        jTable1 = new JTable(resultModel);
        add(new JScrollPane(jTable1));
    } else {
        jTable1.setModel(resultModel);
    }

}catch(Exception ex){
    JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}

Now, personally, I would simply create the JTable and add it to the screen, and leave it alone, and simply change the TableModel when ever you wanted to update it's content...

Swing uses a form of the MVC paradigm, which means it separates the view from the model, meaning that when you want to change what the JTable is showing (the view), you simply change the model...

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, ever so much! deleting the code from the top, and adding if statement helped!
You really should avoid creating a new instance of the JTable if it can be helped
I'm quite new to Java, and especially to NetBeans deign tools, of which i'm far from even knowing half of design features and netbeans features at all... but i'm learning
Take the time to learn how to build UI's by hand. Form designers are great tools, but they need a good understanding of the fundamentals of how to use the UI before they really become useful
I have used GridBagConstraints before and coded in BlueJ... doing this project in NetBeans as it is easier to design indeed, and not as time consuming. I do really appreciate your help, and taking ur time for giving hints :) Thanks again. My next challenge from tomorrow (as it is 5am right now) is to edit data from this table if it is possible
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.