JTable 不显示值
JTable does not display values
我正在使用 java 在 eclipse IDE 中实现一个 GUI。我想显示一个table。这就是我实现程序的方式
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.text.TabableView;
import com.model.FloorDetails;
public class ClientGUI {
private JFrame frame;
private ClientMain clientMain = new ClientMain();
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClientGUI window = new ClientGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ClientGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
table = new JTable();
table.setBounds(67, 146, 1, 1);
frame.getContentPane().add(table);
executeTable();
}
public void executeTable() {
Object[] columns = new String[] {
"ID", "Room No", "Floor No", "CO2 Level", "Smoke Level", "Status"
};
ArrayList<FloorDetails> arrayList = clientMain.getSensors();
Object[][] data = new Object[arrayList.size()][6];
for(int i = 0; i < arrayList.size(); i++) {
data[i][0] = arrayList.get(i).getId();
data[i][1] = arrayList.get(i).getRoomNo();
data[i][2] = arrayList.get(i).getFloorNo();
data[i][3] = arrayList.get(i).getCo2Level();
data[i][4] = arrayList.get(i).getSmokeLevel();
data[i][5] = arrayList.get(i).getStatus();
}
table = new JTable(data,columns);
frame.setTitle("Sensor Details");
}
}
clientMain.getSensors()
方法按预期检索所有数据(我尝试在控制台上打印并打印所有内容)。但是当我 运行 程序时,它显示一个空的 window。
我这样试过只是为了看看我在为二维数组赋值时是否犯了错误,但没有任何改变
Object[][] data = {
{"a", "b", "c", "d", "e", "f"},
{"g", "h", "i", "j", "k", "l"}
};
这个程序哪里做错了?提前致谢!
嗯,您的代码中的一个问题是,您正在填充一个 table 并向框架添加另一个 table。解决此问题的一种方法是
public JTable executeTable( ) { // Make this method return a JTable
Object[] columns = new String[] {
"ID", "Room No", "Floor No", "CO2 Level", "Smoke Level", "Status"
};
ArrayList<FloorDetails> arrayList = clientMain.getSensors();
Object[][] data = new Object[arrayList.size()][6];
for(int i = 0; i < arrayList.size(); i++) {
data[i][0] = arrayList.get(i).getId();
data[i][1] = arrayList.get(i).getRoomNo();
data[i][2] = arrayList.get(i).getFloorNo();
data[i][3] = arrayList.get(i).getCo2Level();
data[i][4] = arrayList.get(i).getSmokeLevel();
data[i][5] = arrayList.get(i).getStatus();
}
table = new JTable( data, columns);
return table;
}
然后更改初始化方法以使用返回的 table
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table = executeTable();
frame.add(table.getTableHeader(), BorderLayout.PAGE_START);
frame.add(table, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setTitle("Sensor Details");
frame.setVisible(true);
}
在这种方法中 main
方法不需要 window.frame.setVisible(true);
我正在使用 java 在 eclipse IDE 中实现一个 GUI。我想显示一个table。这就是我实现程序的方式
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.text.TabableView;
import com.model.FloorDetails;
public class ClientGUI {
private JFrame frame;
private ClientMain clientMain = new ClientMain();
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClientGUI window = new ClientGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ClientGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
table = new JTable();
table.setBounds(67, 146, 1, 1);
frame.getContentPane().add(table);
executeTable();
}
public void executeTable() {
Object[] columns = new String[] {
"ID", "Room No", "Floor No", "CO2 Level", "Smoke Level", "Status"
};
ArrayList<FloorDetails> arrayList = clientMain.getSensors();
Object[][] data = new Object[arrayList.size()][6];
for(int i = 0; i < arrayList.size(); i++) {
data[i][0] = arrayList.get(i).getId();
data[i][1] = arrayList.get(i).getRoomNo();
data[i][2] = arrayList.get(i).getFloorNo();
data[i][3] = arrayList.get(i).getCo2Level();
data[i][4] = arrayList.get(i).getSmokeLevel();
data[i][5] = arrayList.get(i).getStatus();
}
table = new JTable(data,columns);
frame.setTitle("Sensor Details");
}
}
clientMain.getSensors()
方法按预期检索所有数据(我尝试在控制台上打印并打印所有内容)。但是当我 运行 程序时,它显示一个空的 window。
我这样试过只是为了看看我在为二维数组赋值时是否犯了错误,但没有任何改变
Object[][] data = {
{"a", "b", "c", "d", "e", "f"},
{"g", "h", "i", "j", "k", "l"}
};
这个程序哪里做错了?提前致谢!
嗯,您的代码中的一个问题是,您正在填充一个 table 并向框架添加另一个 table。解决此问题的一种方法是
public JTable executeTable( ) { // Make this method return a JTable
Object[] columns = new String[] {
"ID", "Room No", "Floor No", "CO2 Level", "Smoke Level", "Status"
};
ArrayList<FloorDetails> arrayList = clientMain.getSensors();
Object[][] data = new Object[arrayList.size()][6];
for(int i = 0; i < arrayList.size(); i++) {
data[i][0] = arrayList.get(i).getId();
data[i][1] = arrayList.get(i).getRoomNo();
data[i][2] = arrayList.get(i).getFloorNo();
data[i][3] = arrayList.get(i).getCo2Level();
data[i][4] = arrayList.get(i).getSmokeLevel();
data[i][5] = arrayList.get(i).getStatus();
}
table = new JTable( data, columns);
return table;
}
然后更改初始化方法以使用返回的 table
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table = executeTable();
frame.add(table.getTableHeader(), BorderLayout.PAGE_START);
frame.add(table, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setTitle("Sensor Details");
frame.setVisible(true);
}
在这种方法中 main
方法不需要 window.frame.setVisible(true);