我试图将数据库的数据填充到jtable中,但是当我运行代码时,数据显示在控制台上而不是在Jframe上。这是控制台输出:
93203 , KERN , ARVIN , CA , 2012 , 12 , All Ages , 83 , 59.4 , 41.5 , 50.5
93203 , KERN , ARVIN , CA , 2012 , 12 , Adults (18+) , 29 , 59.4 , 41.5 , 50.5
93210 , FRESNO , COALINGA , CA , 2012 , 1 , Children (0-17) , 58 , 64.9 , 36.1 , 50.5
93210 , FRESNO , COALINGA , CA , 2012 , 1 , All Ages , 99 , 64.9 , 36.1 , 50.5
93210 , FRESNO , COALINGA , CA , 2012 , 1 , Adults (18+) , 41 , 64.9 , 36.1 , 50.5
93210 , FRESNO , COALINGA , CA , 2012 , 2 , Children (0-17) , 58 , 66.9 , 38.4 , 52.7
93210 , FRESNO , COALINGA , CA , 2012 , 2 , All Ages , 99 , 66.9 , 38.4 , 52.7
93210 , FRESNO , COALINGA , CA , 2012 , 2 , Adults (18+) , 41 , 66.9 , 38.4 , 52.7
93210 , FRESNO , COALINGA , CA , 2012 , 3 , Adults (18+) , 41 , 68.6 , 42.2 , 55.4
93210 , FRESNO , COALINGA , CA , 2012 , 3 , Children (0-17) , 58 , 68.6 , 42.2 , 55.4
93210 , FRESNO , COALINGA , CA , 2012 , 3 , All Ages , 99 , 68.6 , 42.2 , 55.4
这是GUI的代码
public class WeatherFrame extends JFrame {
private JPanel contentPane;
private JTable table;
HealthData health = new HealthData();
private DefaultTableModel model;
String[] columnNames = {"zipcode", "county", "city", "state", "year", "month","ageGroup",
"numOfVisits", "MonthlyMax", "MonthlyMin", "MonthlyNor"};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WeatherFrame frame = new WeatherFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public WeatherFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 300);
contentPane = new JPanel();
contentPane.setBounds(100, 100,750, 200);
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(6, 25, 788, 180);
contentPane.add(scrollPane);
model = new DefaultTableModel();
populateTable();
table = new JTable(model);
scrollPane.setViewportView(table);
JButton btnInsert = new JButton("insert");
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnInsert.setBounds(315, 217, 117, 29);
contentPane.add(btnInsert);
JButton btnDelete = new JButton("delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnDelete.setBounds(198, 243, 117, 29);
contentPane.add(btnDelete);
JButton btnSearch = new JButton("search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSearch.setBounds(81, 243, 117, 29);
contentPane.add(btnSearch);
JLabel lblWeatherTable = new JLabel("Weather Table");
lblWeatherTable.setBounds(149, 6, 107, 16);
contentPane.add(lblWeatherTable);
JButton btnNext = new JButton("update");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNext.setBounds(198, 217, 117, 29);
contentPane.add(btnNext);
JButton btnRefresh = new JButton("refresh");
btnRefresh.setBounds(81, 217, 117, 29);
contentPane.add(btnRefresh);
}
public void populateTable() {
for(String name: columnNames)
model.addColumn(name);
for (int i = 0; i < health.showAllData().size(); i++) {
int zipcode = health.showAllData().get(i).getZipCode();
String county = health.showAllData().get(i).getCounty();
String city = health.showAllData().get(i).getCity();
String state = health.showAllData().get(i).getState();
int year = health.showAllData().get(i).getYear();
int month = health.showAllData().get(i).getMonth();
String ageGroup = health.showAllData().get(i).getAgeGroup();
int numVisit = health.showAllData().get(i).getNumOfVisits();
float max = health.showAllData().get(i).getMMax();
float min = health.showAllData().get(i).getMMin();
float nor = health.showAllData().get(i).getMNor();
Object[] data = {zipcode, county, city, state, year, month, ageGroup, numVisit, max, min, nor};
model.addRow(data);
}
}
}
我不确定为什么数据不会在Jframe上输出,有人可以建议一种方法来解决这个问题。先感谢您。 我认为错误在populateTable方法中,但是我在for循环中测试变量并且正确的数据被传递到那些局部变量,所以我有点坚持找出逻辑错误的位置。当我执行此代码时填充数据进入Jtable,java应用程序出现在任务栏上,但没有框架。
我使用了几个println来检查数据是否在populateTable方法中的第二个for循环中传递给局部变量,并且它们都被传入。对于向模型添加数据的语句似乎没有任何问题,所以我无法弄清楚为什么它不起作用。数据可以溢出表大小吗?