我想更新我的表格,但它不工作。在整個程序調用完成之前,該表不會顯示出來。我該如何改變這一點?打開窗口後,我想填寫JTable
的數據。如果我停止執行代碼,表格中就會填充數據。我需要線程嗎?我將如何正確使用一個?我的代碼如下。如何在JTable中填充數據?
public class TestGUI extends DefaultTableCellRenderer implements TeststepEventListener {
public JFrame frame;
private JTable testcase_table;
private JTable teststep_table;
/**
* Create the application.
*/
public TestGUI() {
initialize();
frame.setVisible(true);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JProgressBar progressBar = new JProgressBar();
progressBar.setBounds(50, 68, 700, 14);
frame.getContentPane().add(progressBar);
JTextPane txtpnAutomotiveHmi = new JTextPane();
txtpnAutomotiveHmi.setText("Automotive HMI");
txtpnAutomotiveHmi.setBounds(362, 21, 205, 20);
frame.getContentPane().add(txtpnAutomotiveHmi);
testcase_table = new JTable();
testcase_table.setBounds(50, 125, 350, 426);
frame.getContentPane().add(testcase_table);
teststep_table = new JTable();
teststep_table.setBounds(399, 125, 350, 426);
frame.getContentPane().add(teststep_table);
}
private boolean testcase = true;
@Override
public void myEventOccurred(TeststepEvent event) {
TeststepData data = event.data();
if (testcase) {
set_values(data.getDoc(), data.getTestcase());
}
testcase = false;
}
private int i = 0;
LinkedList names = new LinkedList();
private void set_values(Document doc, int testcase) {
frame.setTitle("Wuratbrot" + i);
i++;
Element element = doc.getRootElement();
names.clear();
if (element != null) {
List<Element> testCases = element.getChildren();
//testcase_table.removeAll();
//String[] title = {"Testcases"};
for (Element testCase : testCases) {
names.add(testCase.getAttributeValue("name"));
}
DisplayData(names);
}
testcase_table.revalidate();
frame.validate();
}
private void DisplayData(List<String> Testcases) {
DefaultTableModel aModel = new DefaultTableModel() {
//setting the jtable read only
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
//setting the column name
Object[] tableColumnNames = new Object[1];
tableColumnNames[0] = "TestCases";
aModel.setColumnIdentifiers(tableColumnNames);
if (Testcases == null) {
testcase_table.setModel(aModel);
return;
}
Object[] objects = new Object[1];
ListIterator<String> lstrg = Testcases.listIterator();
//populating the tablemodel
while (lstrg.hasNext()) {
String newcus = lstrg.next();
objects[0] = newcus;
aModel.addRow(objects);
}
//binding the jtable to the model
testcase_table.setModel(aModel);
}
}
爲更好的幫助,儘早發佈一個[SSCCE](http://sscce.org/),短的,可運行的,可編譯的,DefaultTableModel的值爲硬編碼的局部變量 – mKorbel