gpt4 book ai didi

java - 将 jbutton 添加到 jtable 的更简单方法

转载 作者:行者123 更新时间:2023-12-02 03:18:59 24 4
gpt4 key购买 nike

我正在制作一个带有表格的系统,我必须在最后一列中放置按钮。我一直在研究,但我看到的所有代码都非常令人困惑。虽然有一个,但仍有一些部分我不明白。这是我得到它的网站 http://www.javaquery.com/2013/05/how-to-implement-jbutton-in-jtable.html

String[] InvoiceArray = new String[20];
//Declare above variable globally. Used by two-three methods. Change variable name as per your need.

/*
* import the ButtonColumn class if you are not working in IDE
* I used formWindowOpened event to load content in Jtable but you can use other event.
* All you need is put the code with in that event.
*/
private void formWindowOpened(java.awt.event.WindowEvent evt) {
Object[][] rowData = new Object[4][2]; // 4: is number of row ; 2: is number of column
Object columnNames[] = {"Invoice No", "View Report"}; // Name of columns
for (int i = 0; i < 4; i++) {
InvoiceArray[i] = i + "-2345";
rowData[i][0] = i + "-2345";
rowData[i][1] = "View Order " + i; // Can change the text of button.
}
DefaultTableModel tm = new DefaultTableModel(rowData, columnNames);
jTable1.setModel(tm);
ButtonColumn buttonColumn = new ButtonColumn(jTable1, showOrder, 1); // 1: is column number. column count starts with 0,1,2...
}

InvoiceArray 有何用途?我应该从最后一行创建 showOrder 吗?而且,我不明白他发布的关于如何创建监听器的代码。这是:

Action showOrder = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//JTable table = (JTable) e.getSource(); // If you have multiple component following the ActionEvent
int modelRow = Integer.valueOf(e.getActionCommand());
if (InvoiceArray[modelRow] != null) {
/* We are placing invoice no in array
* And track the button click index
* And fetch index in invoice no
*/
System.out.println("Your Invoice No:" + InvoiceArray[modelRow]);
} else {
JOptionPane.showMessageDialog(rootPane, "No records found!");
}
}
};

我知道已经有一些解释了。我理解其中一些,但不是全部。只是在 jtable 上添加 jbutton 以及 jbutton 的监听器的更简单方法。非常感谢您

最佳答案

just a simplier way to add jbutton on jtable and also listeners for the jbutton.

没有简单的方法。您需要了解渲染器和编辑器在 JTable 中如何工作。阅读 Swing 教程中关于 Concepts: Renderers and Editors 的部分了解基础知识。

然后您可以查看Table Button Column这为你做了艰苦的工作。您只需提供单击按钮时要调用的Action

what's the InvoiceArray for?

它用于将数据加载到JTable中。这是 JTable 的基本用法,与向表的列添加按钮完全无关。

加载数据后,您应该忘记invoiceArray。您编写的操作应该通过 TableModel 或 JTable 访问数据。

关于java - 将 jbutton 添加到 jtable 的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39877383/

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