gpt4 book ai didi

java - Swing 图形用户界面 : JList with jtextFields on the rows

转载 作者:行者123 更新时间:2023-11-29 03:33:35 25 4
gpt4 key购买 nike

我的问题是:

我有一个按钮,单击它时会将组件添加到 JList。列表的每一行都由两个 jtextField 组成。第一个是行的 ID。每次我按下“添加按钮”时,都会出现一个新行,ID 会增加,使用该应用程序的人会在第二个 jTextField 上写任何他想要的东西(转发 ID 字段)。

当超过 4 行时,它将有一个滚动 Pane 。我也想要一个删除按钮。可以删除一些行。

你能帮我解决这个问题吗?我不知道如何创建这样的列表...

谢谢!

最佳答案

只需将 JTable 与两列一起使用。这将允许您建立两列,一列用于 ID,一列用于值。这将使您可以编辑第二列。

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;

public class TestTable04 {

public static void main(String[] args) {
new TestTable04();
}

private int id = 0;

public TestTable04() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

final RecordTableModel model = new RecordTableModel();
JTable table = new JTable(model);

JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.add(new Record(++id));
}
});

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(add, BorderLayout.SOUTH);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

});
}

public class Record {

private int id;
private String value;

public Record(int id) {
this.id = id;
}

public int getID() {
return id;
}

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}

}

public class RecordTableModel extends AbstractTableModel {

private List<Record> lstRecords;

public RecordTableModel() {
lstRecords = new ArrayList<>(24);
}

public void add(Record record) {
lstRecords.add(record);
fireTableRowsInserted(lstRecords.size() - 1, lstRecords.size() - 1);
}

public void remove(Record record) {
if (lstRecords.contains(record)) {
int index = lstRecords.indexOf(record);
remove(index);
}
}

public void remove(int index) {
lstRecords.remove(index);
fireTableRowsDeleted(index, index);
}

@Override
public int getRowCount() {
return lstRecords.size();
}

@Override
public int getColumnCount() {
return 2;
}

@Override
public Class<?> getColumnClass(int columnIndex) {
Class clazz = String.class;
switch (columnIndex) {
case 0:
clazz = Integer.class;
break;
}
return clazz;
}

@Override
public String getColumnName(int column) {
String name = null;
switch (column) {
case 0:
name = "ID";
break;
case 1:
name = "Value";
break;
}
return name;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Record record = lstRecords.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = record.getID();
break;
case 1:
value = record.getValue();
break;
}
return value;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Record record = lstRecords.get(rowIndex);
switch (columnIndex) {
case 1:
record.setValue(aValue == null ? null : aValue.toString());
fireTableCellUpdated(rowIndex, columnIndex);
break;
}
}
}
}

查看 How to use tables了解更多详情

关于java - Swing 图形用户界面 : JList with jtextFields on the rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16688726/

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