添加新项目到 JComboBox 不显示
Adding New Item to JComboBox Does Not Display
我有一个可编辑的 JComboBox。当用户输入新项目时,我希望将其添加到列表中并将其显示为 selected 项目。我可以将它添加到列表中,但我似乎无法将其显示为 selected 项目。默认情况下,我显示一个空字符串 (""),这是用户将要编辑的内容以添加新项目。
public class EventComboBoxListener implements ActionListener {
private JComboBox<String> eventBox=null;
public EventComboBoxListener(JComboBox<String> event_) {
eventBox=event_;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Selected: " + eventBox.getSelectedItem());
System.out.println(", Position: " + eventBox.getSelectedIndex());
if (eventBox.getSelectedIndex() < 0) {
eventBox.addItem(eventBox.getSelectedItem().toString());
eventBox.setSelectedItem(eventBox.getSelectedItem().toString());
}
}
}
我必须将 setSelectedItem
与 getSelectedItem
一起使用,这对我来说没有意义。它不起作用并不奇怪,但我不知道还能做什么。新添加的项目按应有的方式显示在列表中,但如何使它同时成为显示中的 selected 项目?之后我可以 select 但这不是必需的。
已添加 MVCE:
主要
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
String[] list= {"","A","B","C"};
TestTableModel model=new TestTableModel(null,new String[] {"col1","col2"});
JTable table=new JTable(model);
JDialog dialog=new JDialog();
JScrollPane scroller=new JScrollPane(table);
JComboBox<String> box=new JComboBox<String>(list);
box.setEditable(true);
box.setSelectedIndex(0);
box.addActionListener(new EventComboBoxListener(box));
JTextField field=new JTextField();
field.setPreferredSize(new Dimension(75,30));
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLayout(new FlowLayout());
dialog.setSize(new Dimension(400,100));
dialog.add(scroller);
dialog.pack();
dialog.setVisible(true);
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(box));
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(field));
model.insertRow(0,new Object[] {"","placeholder"});
}
}
测试表模型class
import javax.swing.table.DefaultTableModel;
public class TestTableModel extends DefaultTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
public TestTableModel(Object[][] data_,String[] columnNames_) {
super(data_,columnNames_);
}
}
首先是关于 MCVE 的一些评论(因为以后每个问题都会包含一个评论)。
我们希望代码位于单个源文件中,以便我们可以轻松地 copy/paste 编译和测试。我们不希望我们的机器上有 3 个文件需要在测试后清理。
只应包括与问题直接相关的相关代码。为什么你有 TestTableModel class。 "column names" 与问题相关吗?重点是尽可能使用标准 JDK classes 测试您的 MCVE。
关于 EventComboListener class。同样,可以使用 and annoymouse inner class 或 lambda 将其添加到组合框。这将代码保存在单个 class.
中
The newly added item shows up in the list as it should but how do I make it the selected item in the display at the same time?
我发现玩你的 MCVE 组合框的 ActionListener 在不同的时间被调用。
所以我的建议是在组合框的编辑器中添加ActionListener。然后我们确定只有在您按下 Enter 键时才会调用 ActionListener。按下 Enter 键后,编辑器将停止,并将值保存到模型中。
所以逻辑是这样的:
//box.addActionListener(new EventComboBoxListener(box));
ComboBoxEditor editor = box.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String item = textField.getText();
DefaultComboBoxModel model = (DefaultComboBoxModel)box.getModel();
if (model.getIndexOf(item) == -1)
{
box.addItem(item);
box.setSelectedIndex( box.getItemCount() - 1 );
}
}
});
所以诀窍是设置 select 索引(而不是 selected 项目)。但首先逻辑检查以确保该项目尚未添加到组合框中。
我有一个可编辑的 JComboBox。当用户输入新项目时,我希望将其添加到列表中并将其显示为 selected 项目。我可以将它添加到列表中,但我似乎无法将其显示为 selected 项目。默认情况下,我显示一个空字符串 (""),这是用户将要编辑的内容以添加新项目。
public class EventComboBoxListener implements ActionListener {
private JComboBox<String> eventBox=null;
public EventComboBoxListener(JComboBox<String> event_) {
eventBox=event_;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Selected: " + eventBox.getSelectedItem());
System.out.println(", Position: " + eventBox.getSelectedIndex());
if (eventBox.getSelectedIndex() < 0) {
eventBox.addItem(eventBox.getSelectedItem().toString());
eventBox.setSelectedItem(eventBox.getSelectedItem().toString());
}
}
}
我必须将 setSelectedItem
与 getSelectedItem
一起使用,这对我来说没有意义。它不起作用并不奇怪,但我不知道还能做什么。新添加的项目按应有的方式显示在列表中,但如何使它同时成为显示中的 selected 项目?之后我可以 select 但这不是必需的。
已添加 MVCE:
主要
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
String[] list= {"","A","B","C"};
TestTableModel model=new TestTableModel(null,new String[] {"col1","col2"});
JTable table=new JTable(model);
JDialog dialog=new JDialog();
JScrollPane scroller=new JScrollPane(table);
JComboBox<String> box=new JComboBox<String>(list);
box.setEditable(true);
box.setSelectedIndex(0);
box.addActionListener(new EventComboBoxListener(box));
JTextField field=new JTextField();
field.setPreferredSize(new Dimension(75,30));
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLayout(new FlowLayout());
dialog.setSize(new Dimension(400,100));
dialog.add(scroller);
dialog.pack();
dialog.setVisible(true);
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(box));
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(field));
model.insertRow(0,new Object[] {"","placeholder"});
}
}
测试表模型class
import javax.swing.table.DefaultTableModel;
public class TestTableModel extends DefaultTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
public TestTableModel(Object[][] data_,String[] columnNames_) {
super(data_,columnNames_);
}
}
首先是关于 MCVE 的一些评论(因为以后每个问题都会包含一个评论)。
我们希望代码位于单个源文件中,以便我们可以轻松地 copy/paste 编译和测试。我们不希望我们的机器上有 3 个文件需要在测试后清理。
只应包括与问题直接相关的相关代码。为什么你有 TestTableModel class。 "column names" 与问题相关吗?重点是尽可能使用标准 JDK classes 测试您的 MCVE。
关于 EventComboListener class。同样,可以使用 and annoymouse inner class 或 lambda 将其添加到组合框。这将代码保存在单个 class.
中The newly added item shows up in the list as it should but how do I make it the selected item in the display at the same time?
我发现玩你的 MCVE 组合框的 ActionListener 在不同的时间被调用。
所以我的建议是在组合框的编辑器中添加ActionListener。然后我们确定只有在您按下 Enter 键时才会调用 ActionListener。按下 Enter 键后,编辑器将停止,并将值保存到模型中。
所以逻辑是这样的:
//box.addActionListener(new EventComboBoxListener(box));
ComboBoxEditor editor = box.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String item = textField.getText();
DefaultComboBoxModel model = (DefaultComboBoxModel)box.getModel();
if (model.getIndexOf(item) == -1)
{
box.addItem(item);
box.setSelectedIndex( box.getItemCount() - 1 );
}
}
});
所以诀窍是设置 select 索引(而不是 selected 项目)。但首先逻辑检查以确保该项目尚未添加到组合框中。