gpt4 book ai didi

java - 单击按钮时显示 jtable

转载 作者:行者123 更新时间:2023-12-02 06:39:11 25 4
gpt4 key购买 nike

我正在尝试显示表格,在 for 循环中动态填充行数据并显示,这在单击按钮时进行。这是我的代码,请有人帮助我。

public class GetQuotes {

private JTextField wtField = new JTextField(10);
private JTextField htField = new JTextField(10);
private JTextField lnField = new JTextField(10);
private JTextField wiField = new JTextField(10);
final JPanel gui = new JPanel(new BorderLayout(2,3));
JPanel labelFields = new JPanel(new BorderLayout(2,2));
JTable table;
JPanel guiCenter = new JPanel(new BorderLayout(2,3));
//default constructor
public GetQuotes(){

labelFields.setBorder(new TitledBorder("Enter Inputs"));

JPanel labels = new JPanel(new GridLayout(0,1,1,1));
//labels.setBorder(new TitledBorder("GridLayout"));
JPanel fields = new JPanel(new GridLayout(0,1,1,1));

labels.add(new JLabel("Weight"));
labels.add(new JLabel("Length"));
labels.add(new JLabel("Width"));
labels.add(new JLabel("Height"));

fields.add(wtField);
fields.add(lnField);
fields.add(wiField);
fields.add(htField);

labelFields.add(labels,BorderLayout.CENTER);
labelFields.add(fields,BorderLayout.EAST);


JPanel buttonConstrsint = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton getQuotesButton = new JButton("Get Quotes..");

getQuotesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

Quote[] data = getData();

table = new JTable(generateTableModel(data));
table.setModel(generateTableModel(data));

JScrollPane pane = new JScrollPane(table);
guiCenter.add(pane, BorderLayout.SOUTH);

gui.add(guiCenter,BorderLayout.CENTER);


}

}
});

buttonConstrsint.add(getQuotesButton);
guiCenter.add(buttonConstrsint,BorderLayout.NORTH);

gui.add(labelFields,BorderLayout.NORTH);
gui.add(guiCenter,BorderLayout.CENTER);

JOptionPane.showMessageDialog(null, gui);


}

private Quote[] getData(){

//this function returns the needed data

}

//test the class
public static void main(String[]args) throws Exception{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new GetQuotes();
}
});

}

public static TableModel generateTableModel(Quote[] dataa) {

String[][] data = new String[100][2] ;
String[] colNames = {"col1","col2"};

//here how to add another column data dataa[j].getPrice
for(int i=0; i<colNames.length; i++){
for(int j=0; j<dataa.length; j++){
data[j][i] = dataa[j].getServiceName();

}
}

return new DefaultTableModel(data, colNames);
}
}

1) 我需要帮助在generateTableModel 函数的for 循环中填充rowData。目前我可以填充单列数据,如何构建第二列数据。

2) 一旦表在此处构建并返回到上面的 actionperformed 函数,我将表添加到 jscrollpanel 和 guicenterpanel。但不知怎的,这并没有显示表格。

有人可以帮我吗?

最佳答案

部分答案:

table = new JTable(generateTableModel(data));
// table.setModel(generateTableModel(data)); // *** this is redundant

JScrollPane pane = new JScrollPane(table);
guiCenter.add(pane, BorderLayout.SOUTH);

gui.add(guiCenter,BorderLayout.CENTER);

// don't forget to revalidate and repaint your container:
gui.revalidate();
gui.repaint();

摆脱多余的代码行,并且不要忘记在添加或删除组件后重新验证和重新绘制容器。

<小时/>

关于:

1) I need help in filling rowData in for loop in generateTableModel function. At the moment I can fill single column data, how to build the second column data.

我不确定我们是否有足够的信息来回答这个问题。您当前的代码是做什么的?它会创建一个两列 JTable 吗?里面装的是什么?你调试过你的代码吗?

关于java - 单击按钮时显示 jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19334861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com