1

I have a problem. I'm pretty new to Java and at the moment i try to learn some basic stuff.

So i created a little GUI with a Textfield vor Input and a Button to fire a query to my Database with the query (that contain the Input as Variable).

I filled a ResultSet with the result of the query and want to display the result in a JTable. I tryed it with this:

public class AppWindow extends JFrame {

    private ResultSet rs = null;

    JTextField  textfield_where_1;
    JTextField  textfield_where_2;

    JTable tabellenspass;

    JButton     button;


    public AppWindow() {
        this.getContentPane().setLayout(null);

        this.initWindow();

    protected void initWindow() 
    {
        // Instanzieren:
        textfield_where_1 = new JTextField();
        textfield_where_2 = new JTextField();
        tabellenspass = new JTable();


        button = new JButton("muh");

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                try {
                    Abfrage();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });

        // Positionen festlegen
        textfield_where_1.setBounds(5,10,400,25);

        tabellenspass.setBounds(5,40,400,100);
        button.setBounds(300,110,100,30);

        // Elemente dem Fenster hinzufügen:
        this.getContentPane().add(textfield_where_1);

        this.getContentPane().add(button);

        this.getContentPane().add(tabellenspass);
        this.pack();
    }

    public void Abfrage() throws Exception
    {
        // Eingabestrings holen
        String eingabe1 = textfield_where_1.getText();

        select_dies_das ausgabe = new select_dies_das();

        rs = ausgabe.select(eingabe1);
        Ausgabe_Tabelle(rs);


    }

    private void Ausgabe_Tabelle(ResultSet rs)
    {
         try
         {

                // Rows / Columns der Tabelle setzen

            ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
            // Tabellenmodel

            DefaultTableModel model = new DefaultTableModel();
            tabellenspass.setModel(model);

            //model.fireTableRowsInserted(0, 5);
            int columns = rsmd.getColumnCount();

            // Resultset Zeiger zurueck setzen
            rs.beforeFirst();    

            while (rs.next()) {

                Object[] row = new Object[columns];
                    for (int i = 1; i <= columns; i++)
                    {  
                        row[i - 1] = rs.getObject(i);
                    }

                    model.insertRow(rs.getRow() -1,row);
                   System.out.println("miau");
            }

           // tabellenspass.setModel(model);  
            tabellenspass.repaint();
         }
         catch(Exception e)
         {
             System.out.println(e);
         }
    }

With Abfrage() i get the filled resultset and with Ausgabe_Tabelle() I fill the JTable, but the shown Table wont change.

I create a row Object[] that i fill with the Data of a whole row, and this Object i insert into the JTable.

With initWinodw() I create the little GUI.

Can someone explain me why the table wont refresh after the inserts?

user3793935
  • 411
  • 5
  • 22

2 Answers2

4

I'm thinking the problem is with your TableModel that is not correctly initializated, you need to use the public DefaultTableModel(Object[][] data, Object[] columnNames){...} constructor for give it the header names :)

DefaultTableModel model = new DefaultTableModel(null,new String[]{"Col1","Col2"});

Of course you need to add or remove columns to the model according to your needs, also the rs.beforeFirst(); statement is unnecessary becouse you already are using a while loop that checks if the ResultSet has another row with the next() method of this interface, if you use this statement, also confirm that your ResultSet is scrollable or you will get an

org.postgresql.util.PSQLException: Operation requires a scrollable ResultSet, but this ResultSet is FORWARD_ONLY.

Better comment this line

//rs.beforeFirst();

Also you can change your model.insertRow(rs.getRow() - 1, row); statement with this model.addRow(row); I have tried your code and this is the result

enter image description here

Maybe you should consider adding the JTable to a JScrollPane for keep visible the headers, and set layouts to your design, something like this

setTitle("Test");
this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
JPanel data = new JPanel();
this.getContentPane().add(textfield_where_1);
this.getContentPane().add(data);
data.add(button);
JScrollPane scroll = new JScrollPane(tabellenspass);
data.add(scroll);
scroll.setPreferredSize(new Dimension(400, 100));
button.setPreferredSize(new Dimension(100, 100));
textfield_where_1.setPreferredSize(new Dimension(400, 25));

enter image description here

Osuka
  • 106
  • 1
  • 9
  • Hey, i use the rs.beforefirst(); because i already did a print in the console after the query to check if there is a result. (was just a test for me, because as i said, i try to learn some basics ;D) Okay, i will try your stuff and hope it work for me too. – user3793935 May 29 '15 at 13:17
  • @user3793935 This is solved but please read the edited answer :) – Osuka May 29 '15 at 13:50
0

To notify JTable about changes use fireTableDataChanged:

Notifies all listeners that all cell values in the table's rows may have changed.

Also you can try revalidate and repaint or resizeAndRepaint():

tabellenspass.revalidate();
tabellenspass.repaint();

or

tabellenspass.resizeAndRepaint();

And check here for further info...

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109