0

我正在尝试创建一种更新 JComboBox 的方法,以便当用户在文本字段中输入内容时,一些代码将处理该条目并相应地更新 JComboBox。我遇到的一个问题是我可以更新 JComboBox,但是第一次打开时,该框没有刷新其中选项的长度,如下面的代码所示,它显示了额外的空白。我不知道是否有更好的不同方法来做到这一点,但这就是我想出的。

谢谢您的帮助,

import java.awt.event.*;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Catch{
public static JComboBox dropDown;
public static String dropDownOptions[] = {
         "Choose",
         "1",
         "2",
         "3"};
 public static  void main(String[] args) {
     dropDown = new JComboBox(dropDownOptions);
     final JTextField Update = new JTextField("Update", 10);
     final JFrame frame = new JFrame("Subnet Calculator");
     final JPanel panel = new JPanel();
     frame.setSize(315,430);
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     Update.addFocusListener(new FocusListener(){
    public void focusGained(FocusEvent arg0) {  
            }
            public void focusLost(FocusEvent arg0) {
                dropDown.removeAllItems();
                dropDown.insertItemAt("0", 0);
                dropDown.insertItemAt("1", 1);
                dropDown.setSelectedIndex(0);
            }
              });
        panel.add(Update);
        panel.add(dropDown);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
        Update.requestFocus();
        Update.selectAll();
    }
}
4

1 回答 1

3

1 )JTextField听从ENTER keyActionListener

2) 删除FocusListener

3)关于将新Item的作为最后一个添加ItemJTextField的示例JList,只有您必须正确修改JComboBox和添加方法insertItemAt()

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ListBottom2 {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);
    private JTextField textField = new JTextField("Use Enter to Add");
    private JPanel panel = new JPanel(new BorderLayout());

    public ListBottom2() {
        model.addElement("First");
        list.setVisibleRowCount(5);
        panel.setBackground(list.getBackground());
        panel.add(list, BorderLayout.SOUTH);
        JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.setPreferredSize(new Dimension(200, 100));
        frame.add(scrollPane);
        frame.add(textField, BorderLayout.NORTH);
        textField.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JTextField textField = (JTextField) e.getSource();
                DefaultListModel model = (DefaultListModel) list.getModel();
                model.addElement(textField.getText());
                int size = model.getSize() - 1;
                list.scrollRectToVisible(list.getCellBounds(size, size));
                textField.setText("");
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ListBottom2 frame = new ListBottom2();
            }
        });
    }
}
于 2012-04-19T18:52:59.623 回答