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?