在 JOptionPane 中设置 JTextFields 的文本
Setting the text of JTextFields in a JOptionPane
我有一个 JTable,它存储有关菜肴的数据。当用户尝试添加一道新菜时,他必须在四个字段中输入值。尽管默认情况下 JTable 是可编辑的,但我想为编辑行创建自己的实现。我有一个生成自定义对话框的方法和一个存储对文本字段的引用的数组列表。我的目标是将所有文本字段的文本设置为行中相应的文本,然后显示对话框。这是我到目前为止尝试过的。
edit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
if (dishes.getSelectionModel().isSelectionEmpty())
{
JOptionPane.showMessageDialog(null, "You need to select a dish in order to edit it",
"No element selected", JOptionPane.INFORMATION_MESSAGE);
}
else
{
String[] labels = {"Name:", "Description:", "Price:", "Restock Level:"};
int fields = 4
;
JOptionPane optionPane = new JOptionPane();
optionPane.setVisible(false);
optionPane.showConfirmDialog(null, createInputDialog(labels,fields),
"New Dish", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
for(int i = 0; i < textFields.size(); i++)
{
textFields.get(i).setText(dishes.getValueAt(dishes.getSelectedRow(), i).toString());
}
optionPane.setVisible(true);
}
}
});
下面是创建对话框中使用的面板的代码
//Creates an input dialog with the specified number of fields and text for the labels
public JPanel createInputDialog(String[] labels, int numFields)
{
JPanel input = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
textFields = new ArrayList<JTextField>();
for(int i = 0; i < numFields; i++)
{
gbc.gridx = 2;
gbc.gridy = i;
gbc.anchor = GridBagConstraints.WEST;
JLabel label = new JLabel(labels[i]);
label.setFont(font);
input.add(label, gbc);
gbc.gridx = 4;
JTextField field = new JTextField(10);
field.setFont(font);
input.add(field, gbc);
textFields.add(field);
}
error = new JLabel("");
error.setForeground(Color.RED);
gbc.gridx = 2;
gbc.gridy = numFields + 1;
gbc.gridwidth = 2;
input.add(error, gbc);
return input;
}
基本思路是
- 使用
JTable#getSelectedRow
获取选中的行索引(不选中为-1
)。
- 使用
JTable#getValueAt
获取列值。
- 将这些值传递给
createInputDialog
方法,以便它填充文本字段
也许为您的 JTextFields 使用数组列表可能是一种更简单的方法?
ArrayList<JTextField> textFields = new ArrayList<JTextField>();
然后你可以遍历你的arraylist。
for (JTextField txtField : textFields) {
txtField.setText(dishes.getValueAt(dishes.getSelectedRow(), i).toString());
}
虽然我不确定我是否完全理解这个问题。
我有一个 JTable,它存储有关菜肴的数据。当用户尝试添加一道新菜时,他必须在四个字段中输入值。尽管默认情况下 JTable 是可编辑的,但我想为编辑行创建自己的实现。我有一个生成自定义对话框的方法和一个存储对文本字段的引用的数组列表。我的目标是将所有文本字段的文本设置为行中相应的文本,然后显示对话框。这是我到目前为止尝试过的。
edit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
if (dishes.getSelectionModel().isSelectionEmpty())
{
JOptionPane.showMessageDialog(null, "You need to select a dish in order to edit it",
"No element selected", JOptionPane.INFORMATION_MESSAGE);
}
else
{
String[] labels = {"Name:", "Description:", "Price:", "Restock Level:"};
int fields = 4
;
JOptionPane optionPane = new JOptionPane();
optionPane.setVisible(false);
optionPane.showConfirmDialog(null, createInputDialog(labels,fields),
"New Dish", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
for(int i = 0; i < textFields.size(); i++)
{
textFields.get(i).setText(dishes.getValueAt(dishes.getSelectedRow(), i).toString());
}
optionPane.setVisible(true);
}
}
});
下面是创建对话框中使用的面板的代码
//Creates an input dialog with the specified number of fields and text for the labels
public JPanel createInputDialog(String[] labels, int numFields)
{
JPanel input = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
textFields = new ArrayList<JTextField>();
for(int i = 0; i < numFields; i++)
{
gbc.gridx = 2;
gbc.gridy = i;
gbc.anchor = GridBagConstraints.WEST;
JLabel label = new JLabel(labels[i]);
label.setFont(font);
input.add(label, gbc);
gbc.gridx = 4;
JTextField field = new JTextField(10);
field.setFont(font);
input.add(field, gbc);
textFields.add(field);
}
error = new JLabel("");
error.setForeground(Color.RED);
gbc.gridx = 2;
gbc.gridy = numFields + 1;
gbc.gridwidth = 2;
input.add(error, gbc);
return input;
}
基本思路是
- 使用
JTable#getSelectedRow
获取选中的行索引(不选中为-1
)。 - 使用
JTable#getValueAt
获取列值。 - 将这些值传递给
createInputDialog
方法,以便它填充文本字段
也许为您的 JTextFields 使用数组列表可能是一种更简单的方法?
ArrayList<JTextField> textFields = new ArrayList<JTextField>();
然后你可以遍历你的arraylist。
for (JTextField txtField : textFields) {
txtField.setText(dishes.getValueAt(dishes.getSelectedRow(), i).toString());
}
虽然我不确定我是否完全理解这个问题。