开发者

setPopupVisible is canceled if it follows on setvisible true

开发者 https://www.devze.com 2023-03-16 23:21 出处:网络
When I press the button in the program below I want the popup menu from the JComboBox to appear and remain up.

When I press the button in the program below I want the popup menu from the JComboBox to appear and remain up.

However it only does that when on the seco开发者_StackOverflow中文版nd push when the Combobox is already visible. Is there any way I can make it appear on the first push?

public class Problem extends JPanel 
implements ActionListener{

private static String SEARCH = "start search";
private static String SELECTED = "Selected";

private JTextField field2;
private JTextField field1;

private JComboBox list = new JComboBox();

public Problem() {
    super(new GridBagLayout());

    //Construct the panel
    field2=new JTextField("");
    field1=new JTextField("7564");
    JButton search=new JButton("Search");
    search.addActionListener(this);
    search.setActionCommand(SEARCH);

    list.addActionListener(this);
    list.setActionCommand(SELECTED);


    //Add everything to this panel.
    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 8;
    c.gridheight = 3;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.anchor = GridBagConstraints.NORTH;

    add(field2,c);

    list.setVisible(false);
    add(list,c);

    c.gridx = 8;
    c.gridy = 0;
    c.gridwidth = 8;
    c.gridheight = 3;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.anchor = GridBagConstraints.NORTH;

    add(field1,c);

    c.gridx = 16;
    c.gridy = 0;
    c.gridwidth = 4;
    c.gridheight = 3;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.anchor = GridBagConstraints.NORTH;

    add(search,c);


}

public void actionPerformed(ActionEvent ae) {



    if (ae.getActionCommand()==SEARCH){
        String f2=field2.getText();
        String f1=field1.getText();
        if(f2.equals("")){

            f2=selection(f1);
            f2=null;
        }
        if(f2!=null){
            field2.setText(f2);
        }
        else{
            System.out.println("setPopupVisible runs");
            field2.setVisible(false);
            list.setVisible(true);
            field2.setText("");
            list.setPopupVisible(true);
        }
    }
    else if(ae.getActionCommand().equals(SELECTED)){
        System.out.println("select");
        String listtext=(String) list.getSelectedItem();
        list.removeAllItems();
        if(listtext==null||!listtext.contains(": ")){
            return;
        }
        String f2=listtext.split(": ")[0];
        list.setVisible(false);
        field2.setVisible(true);
        field2.setText(f2);
    }
}

private String selection(String sn) {
    System.out.println("selection runs");

    String name="7";
    list.removeActionListener(this);
    list.removeAllItems();
        list.addItem(name);
    list.addActionListener(this);
    return null;
}

private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Stuff");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    Problem newContentPane = new Problem();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    frame.pack();
    frame.setVisible(true);
    frame.setSize(800, 600);
}

public static void main(String[] args) throws FileNotFoundException {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            createAndShowGUI();
        }
    });
}
}


When toggling visibility of components, make sure the component has completed its internal update before doing custom stuff, that is f.i. delay the call to showing a combo's popup relative to showing the combo itself:

            list.setVisible(true);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    list.setPopupVisible(true);
                }
            });
0

精彩评论

暂无评论...
验证码 换一张
取 消