更新JComboBox后,如何刷新盒子的长度



我正在尝试创建一种更新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();
    }
}

1) JTextField侦听来自ActionListenerENTER key

2) 去除FocusListener

3)关于添加新Item作为上次ItemJTextFieldJList的示例,只需要修改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();
            }
        });
    }
}

最新更新