我从主窗口打开JDialog以显示根据参数和模式检索的数据集。 (在这个简化的版本参数并不重要,只是"模式",实际上我从记录集中填充它。)我想要
1.最初填写对话窗口中的表
可选择使用按钮显示另一组数据,使用不同模式检索
第一个任务是好的。创建表和填充按程序组织 从对话框构造函数正常工作。 但是当我从按钮的ActionListener运行这个过程时 - 我在同一个窗口中得到另一个表副本 - 这是有意义的,因为我再次创建它作为" new"。我找不到可以在同一张桌子上重新填充数据的方法!网上的所有示例都是关于创建一个新的
这是调用部分
btnFreqHistory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Running history");
DlgFreqHistory FreqHist= new DlgFreqHistory(1000,2,1 );
FreqHist.setDefaultCloseOperation(FreqHist.DISPOSE_ON_CLOSE);
FreqHist.setVisible(true);
}
});
这是JDialog部分:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import java.sql.ResultSet;
public class DlgFreqHistory extends JDialog {
private final JPanel contentPanel = new JPanel();
static int Freq;
static int Side;
static int Mode; //1- last 10 results, 2 - best results
/**
* Launch the application.
*/
public static void main(String[] args) {
}
/**
* Create the dialog.
*/
public DlgFreqHistory(int iFreq, int iSide, int iMode) {
Freq = iFreq;
Side = iSide;
Mode = iMode;
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
this.setTitle("Last results for " + iFreq );
**// !! This call works fine**
GetHistoryByMode(iFreq, iSide, 2);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
JButton btn_getData = new JButton("Get data");
btn_getData.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("in action performed");
***// !! This call creates another table***
GetHistoryByMode(Freq, Side, 1);
}
} );
buttonPane.add(btn_getData);
getRootPane().setDefaultButton(btn_getData);
}
private void GetHistoryByMode(int Freq, int iSide, int Mode)
{
System.out.println("in GetHistoryByMode " + Freq + " " + iSide + " " + Mode);
String HistColumns[] = new String[2];
String HistData[][] = new String[10][2];
if (Mode==1) {
HistColumns[0] = "X1";
HistColumns[1] = "Result";
HistData[0][0] = "xx1";
HistData[0][1] = "15";
HistData[1][0] = "xx2";
HistData[1][1] = "20";
}
if (Mode==2) {
HistColumns[0] = "Y1";
HistColumns[1] = "Result";
HistData[0][0] = "yy1";
HistData[0][1] = "15";
HistData[1][0] = "yy2";
HistData[1][1] = "23";
}
JTable FreqHistory = new JTable(HistData, HistColumns);
contentPanel.add(FreqHistory);
JScrollPane scrollPane;
scrollPane = new JScrollPane( FreqHistory );
contentPanel.add( scrollPane, BorderLayout.CENTER );
}
}
答案 0 :(得分:2)
不要在JTable
方法中创建GetHistoryByMode
,而是在构造函数中创建它并将其添加到UI中。然后在GetHistoryByMode
方法中,创建一个新的TableModel
并将其应用(setModel
)到现有的JTable
请查看How to Use Tables了解详情
您可能还希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码