I have a problem with my JTable. My JTable displays content of a database. One database table has the name category. Every category is displayed in the JComboBox. If I click on a category it should update the table content.
Here is a short snipped of my code for you, so it is easier to help me. The code should be runable:
(TestClass - Main)
package test;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TestClass implements ActionListener{
String[] header = {"head", "head", "head"};
Object[][] data = {{Boolean.FALSE, "text", "text"}, {Boolean.FALSE, "text", "text"}, {Boolean.FALSE, "text", "text"}};
LinkedList<String> newdata = new LinkedList<String>();
String[] combolist = {"apple", "banana", "cranberry"};
JComboBox<String> combobox = new JComboBox<String>(combolist);
JTable table = new JTable(new TestTableModel(data, header));
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(1, 0, 1, 0));
public TestClass() {
combobox.addActionListener(this);
panel.add(combobox);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.pack();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.combobox) {
JComboBox<String> combobox = this.combobox;
newdata.add("Test1");
newdata.add("Test2");
TestTableModel model = (TestTableModel) table.getModel();
int i = 0;
for (String text : newdata) {
data[i][0] = Boolean.TRUE;
data[i][1] = text;
data[i][2] = text;
i++;
}
model.setData(data);
}
}
public static void main(String[] args) {
new TestClass();
}
}
(TestTableModel - AbstractTableModel if required (you need it to execute the code!))
package test;
import javax.swing.table.AbstractTableModel;
public class TestTableModel extends AbstractTableModel {
private static final long serialVersionUID = 5044877015250409328L;
private String[] header;
private Object[][] data;
public TestTableModel(Object[][] data, String[] header) {
this.header = header;
this.data = data;
}
public void setData(Object[][] data) {
this.data = data;
fireTableDataChanged();
}
@Override
public Class<?> getColumnClass(int column) {
if (column == 0) {
return Boolean.class;
}
return super.getColumnClass(column);
}
@Override
public int getColumnCount() {
return header.length;
}
@Override
public String getColumnName(int column) {
return header[column];
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public Object getValueAt(int row, int column) {
return data[row][column];
}
@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
}
@Override
public void setValueAt(Object value, int row, int column) {
data[row][column] = value;
}
}
With this short code the table freeze if you change the category. At my whole code,t it freeze too, but I am able to see the updated Table in the background when I resize the window (table resize to the same size as the frame). I don't know why it isn't at the snipped.
EDIT: The problem to change the content has been solved. The source has been updated. But the problem to get the right table size hasent been solved yet. In the source first I use a 3 row array and after that a 2 row array. I want to delete the old table and create a new one, so the row size is right.
Basically I just want to update the table with new content.
- Thank you for Help!