JTable的刷新问题,快疯了~~
发布网友
发布时间:2023-09-20 07:45
我来回答
共2个回答
热心网友
时间:2023-09-20 11:01
我的刷新问题是这样解决的
首先定义一个模式,也就是jTable可以支持刷新的模式(默认情况下不支持刷新)
DefaultTableModel model = new DefaultTableModel("数组名称,为object型", “字列的名称如日期,名称等”);
jtable.setModel(model); //设置这个模式后,可以动态地改变里面的数据
然后再添加刷新:jt.repaint();
我的初始显示部分程序是这样的
public void jButton1_actionPerformed(ActionEvent e) { //显示引水调度图
JFrame jf = new JFrame("水库引水调度计算结果");
JPanel jp3 = new JPanel(new XYLayout());
for (int i = 0; i < 69; i++) {
jc.addItem((1935 + i) + "年~" + (1936 + i) + "年");
}
jf.setBounds(5, 5, 1000, 730);
jf.setLayout(new BorderLayout(10, 10));
Container contenPane = jf.getContentPane();
BorderLayout borderlayout1 = new BorderLayout();
JLabel label = new JLabel("水库引水调度");
label.setFont(new java.awt.Font("黑体", Font.PLAIN, 26));
JLabel label2 = new JLabel("引水调度数据表", JLabel.CENTER);
label2.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
Object[][] allResult = new Object[37][7];
for (int i = 0; i < 36; i++) { //将流量放入object数组中
allResult[i][0] = aga.yearAGA[i];
allResult[i][1] = aga.inflowAGA[i];
allResult[i][2] = (float) ((int) (aga.bestOutFlow[i] * 100) /
100.0f);
allResult[i][3] = (float) ((int) (aga.sumAGA[i] * 100) / 100.0f);
allResult[i][4] = (float) ((int) (aga.bestQR[0][i] * 100) / 100.0f);
allResult[i][5] = (float) ((int) (aga.wasteWater[i] * 100) / 100.0f);
allResult[i][6] = (float) ((int) (aga.bestWaterEvel[i] * 100) /
100.0f);
}
String[] Name = {"日期", "天然来水量(10**6)", "实际供水量(10**6)", "设计供水量(10**6)",
"实际引水量(10**6)", "弃水量(10**6)", "末水位(m)"};
jt = new JTable(allResult, Name);
jp1.setSize(new Dimension(500, 470));
jP2.setLayout(new BorderLayout());
jP2.setBackground(SystemColor.window);
jt.setPreferredScrollableViewportSize(new Dimension(500, 440));
jP2.add(label2, BorderLayout.NORTH);
jP2.add(new JScrollPane(jt), BorderLayout.CENTER);
AllFlow c = new AllFlow(500, 630, 36, 63, 0); //画折线图类
c.setBackground(SystemColor.window);
jp1.setLayout(borderlayout1);
jp1.add(new JScrollPane(c), borderlayout1.CENTER);
jp1.setBackground(SystemColor.window);
js1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false,
jp1, jP2);
js1.setDividerLocation(500); //设置splitPane1的分隔线位置
js1.setOneTouchExpandable(true);
js1.setDividerSize(10);
jp3.add(label, new XYConstraints(350, 18, 450, 35));
jp3.add(jc, new XYConstraints(600, 18, 120, 25));
contenPane.add(jp3, BorderLayout.NORTH);
contenPane.add(js1, BorderLayout.CENTER);
jt.repaint();
jP2.repaint();
jp1.validate();
jc.setVisible(false);
jf.setVisible(true);
}
//然后再点下拉框的程序,可以从数据库中读取数据动态更新
public void jc_itemStateChanged(ItemEvent e) { //不同的年份,操作流量图的
for (int i = 0; i < 69; i++) {
if (((1935 + i) + "年~" + (1936 + i) +
"年").equals(jc.getSelectedItem())) {
Object[][] allResult = new Object[37][7];
for (int j = i * 36; j < (i + 1) * 36; j++) { //将流量放入object数组中
allResult[j % 36][0] = aga.yearAGA[j];
allResult[j % 36][1] = aga.inflowAGA[j];
allResult[j %
36][2] = (float) ((int) (aga.bestOutFlow[j] * 100) /
100.0f);
allResult[j %
36][3] = (float) ((int) (aga.sumAGA[j] * 100) /
100.0f);
allResult[j %
36][4] = (float) ((int) (aga.bestQR[0][j] * 100) /
100.0f);
allResult[j %
36][5] = (float) ((int) (aga.wasteWater[j] * 100) /
100.0f);
allResult[j %
36][6] = (float) ((int) (aga.bestWaterEvel[j] * 100) /
100.0f);
}
String[] Name = {"日期", "天然来水量(10**6)", "实际供水量(10**6)",
"设计供水量(10**6)", "实际引水量(10**6)", "弃水量(10**6)",
"末水位(m)"};
DefaultTableModel model = new DefaultTableModel(allResult, Name);
jt.setModel(model);
jp1.removeAll();
AllFlow c = new AllFlow(500, 630, 36, 63, i); //画折线图类
c.setBackground(SystemColor.window);
jp1.setLayout(new BorderLayout());
jp1.add(new JScrollPane(c), new BorderLayout().CENTER);
jp1.setBackground(SystemColor.window);
jt.repaint();
jP2.repaint();
jp1.validate();
}
}
}
热心网友
时间:2023-09-20 11:02
发代码看看!