After updating JComboBox, how to refresh length of box

人盡茶涼 提交于 2020-01-25 20:09:21

问题


I am trying to create a way to update a JComboBox so that when the user enters something into the text field, some code will process the entry and update the JComboBox accordingly.The one issue that I am having is I can update the JComboBox, but the first time it is opened, the box has not refresh the length of the options in it and as seen in the code below it displays extra white space. I do not know if there is a better different way to do this, but this is what I came up with.

Thanks for the help,

Dan

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:


1) JTextField listening for ENTER key from ActionListener

2) remove FocusListener

3) example about add new Item as last Item from JTextField to the JList, only you have to modify for JComboBox and add method insertItemAt() correctly

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();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/10234297/after-updating-jcombobox-how-to-refresh-length-of-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!