2

I am working on a GUI that will feature a table that has been manually connected to a database. Above this table there are 3 radio buttons that will "decide " what criteria is to be used when fetching the data( All rows have a boolean value, depending on the button pressed it is supposed to return either 1, 0 or both).

enter image description here

This is the code for the table(NOTE i am using netbeans gui designer)

ServiceTable = new javax.swing.JTable();
int radiovalue = 0; 
if (RadioActive.isSelected()) radiovalue = 0;
else if (RadioAll.isSelected()) radiovalue = 1;
else if (RadioFinished.isSelected()) radiovalue = 2;
Object[][] DataAct = null;
try { 
DataAct = SQL.MYSQL_FETCH_OMNI_DATA(radiovalue);
} catch (Exception ex) {
Logger.getLogger(MainforAdmin.class.getName()).log(Level.SEVERE, null, ex);
}
String[] Colnombs = SQL.MYSQL_ROW_NOMBER();
ServiceTable.setAutoCreateRowSorter(true);

ServiceTable.setModel(new javax.swing.table.DefaultTableModel( DataAct, Colnombs ));

TableContainer.setViewportView(ServiceTable);

This works as it should and the 2 external functions return arrays that make the table display what it should display (Which is as the program starts all the "active " transactions)

However i want to be able to change the table so that it will evaluate if radioactive is equals to 0, 1 or 2 (The number is going to determined what data the function fetches). The program outputs the MYSQL tables trough System.out.print perfectly with the diffrent criterias. So I know my functions are working. But I can not figure out how I am to make the entire table "refresh" after another radiobutton is selected..

This is my event code for Mousepressed for a radio button.

TableRefresher();
System.out.println("Pressed");

And Pressed is outputed so i know this code has been summoned after clicking on the radio button. Here is the TableRefresher function.

 Write.Echo("The TableRefresher method hath been summoned");
    //This code is going to evaluate which button is selected as of now.
    MainforAdmin table = new MainforAdmin();
    if (table.RadioActive.isSelected()) radiovalue = 0;
    else if (table.RadioAll.isSelected()) radiovalue = 1;
    else if (table.RadioFinished.isSelected()) radiovalue = 2;
    Object[][] DataAct = null; //This code is going to innitialize the tablecontents.
    try {
        DataAct = SQL.MYSQL_FETCH_OMNI_DATA(radiovalue);//This code assigns the table contents And i know this works because it correctly outputs the table with the diffrent where clause (where state = x or the status as you saw on the picture.)
    } catch (Exception ex) {
        Logger.getLogger(MainforAdmin.class.getName()).log(Level.SEVERE, null, ex);
    }
    String[] Colnombs = SQL.MYSQL_ROW_NOMBER(); //Assigns the column names and works is used as the table is created so this works.
    table.TableContainer.remove(table.ServiceTable);
    table.add(table.ServiceTable, null);
    table.ServiceTable.setModel(new javax.swing.table.DefaultTableModel( DataAct, Colnombs ));
    table.ServiceTable.revalidate();
    table.ServiceTable.repaint();
    table.TableContainer.setViewportView(table.ServiceTable);

Yet as this method is summoned(Which i know it is from the console output) nothing happens to the JTable in the GUI...It stays the same.

So how am i supposed to refresh the table after a different criteria for fetching the data has been applied ? I have looked at other suggestions here on this site but none of them worked or gave me what i needed.

Any answers would be very appreciated, and please forgive me if this is an easy question I am by no means a Programming deity.

If it makes any difference the JTable is in a Scrollpane..

Sincerly...

//Orville Nordström.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Orvil Nordström
  • 325
  • 6
  • 18

2 Answers2

2

Just as a start:

If it makes any difference the JTable is in a Scrollpane.

That's correct and it must keep in this way. It makes no difference to solve the main problem though.

So how am i supposed to refresh the table after a different criteria for fetching the data has been applied?

Code quote:

table.TableContainer.remove(table.ServiceTable);
table.add(table.ServiceTable, null);
table.ServiceTable.setModel(new javax.swing.table.DefaultTableModel( DataAct, Colnombs ));
table.ServiceTable.revalidate();
table.ServiceTable.repaint();
table.TableContainer.setViewportView(table.ServiceTable);

Please note this is kind of spaghetti code and it's not clear which is the table to be updated. However the correct way to update a table is not removing / re-creating / re-locating it but working with / refreshing its table model instead. See examples here (includes SwingWorker to make database calls in a background thread), here and here. Please have a look to those answers, there are explanations to make the point clear.

Off-topic

Looking at the quoted lines above, I'd suggest you to avoid using static members. Those are intended to very specific cases (i.e.: constants) but not to general use, and often breaks the encapsulation principle. In this particular case they led to an unintelligible chain of calls that are probably wrong and causing an unexpected hard-to-debug (for us) behavior.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • Thank you a bunch for this answer. I was able to solve my problem by creating my own model for the table by creating a new JTable default model, setting it as the model for the table and (Don't know if needed) revalidating and repainting and finally adding table to viewport. I am also happy for the advice of not using static methods unnecessarly and the links you provided where helpfull. – Orvil Nordström Dec 03 '14 at 01:23
  • You are welcome! I'm glad it helped and you've solved your problem :) @OrvilNordström – dic19 Dec 03 '14 at 11:54
1

if I understand your problem is that you can not "refresh" the table, in my programs I use this method (DefaultTableModel):

private void jButtonActionPerformed(java.awt.event.ActionEvent evt) { .......
..................

jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][]{},
            new String[]{
                "CLMN1", "CLMN2", "CLMN3", "CLMN..."
            }) {});
enter code here
 model = (DefaultTableModel) jTable.getModel();

model.addRow(new Object[]{("YOUR ROW"}); ----> in a While(or FOR), for any rows

bye :)

Iron Fede
  • 46
  • 5