Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6091977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:23:24+00:00 2026-05-23T12:23:24+00:00

When I press the button in the program below I want the popup menu

  • 0

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 second 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();
        }
    });
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:23:25+00:00Added an answer on May 23, 2026 at 12:23 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a program that searches for a running program and press a button
I want to be able to press a button on my program and hold
I wrote a program in C# that, when a user press a button, opens
I have two buttons in my program and i want that when i press
I want to pause the program until my button is press I have pause
I'm trying to fill-out a form automatically and press a button on that form
I built a program, and i press a button, the program crashes. Here is
In my program I will press a button, and it will load form2. I
I have a windows forms program with a form MainForm. On a button press
It's a program that reads student data from a text file and displys it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.