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

  • SEARCH
  • Home
  • 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 9136667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:58:58+00:00 2026-06-17T08:58:58+00:00

JFrame frame = new JFrame(Picture); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); display = new JPanel(); if(event.getSource().equals(birthday)){ background =

  • 0
  JFrame frame = new JFrame("Picture");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  display = new JPanel();      
if(event.getSource().equals(birthday)){
    background = new JLabel(bday);
    display.add(background);

  }
  else if(event.getSource().equals(cake)){
    picture = new JLabel(pastry, SwingConstants.LEFT);
    display.add(picture);
  }
  else if(event.getSource().equals(input)){
    word = new JLabel(text);
    word.setHorizontalTextPosition(SwingConstants.RIGHT);
    word.setVerticalTextPosition(SwingConstants.CENTER);
    display.add(word);
  }
  frame.setPreferredSize (new Dimension(450, 350));
  frame.getContentPane().add(display);
  frame.pack();
  frame.setVisible(true);

This is part of an ActionListener class inside my standalone class. I had a comboBox/cardLayout. So when I clicked a button (cake) from one comboBox label, and another button (birthday) from another another comboxBox label, two frames appeared. I want the stuff to be on the SAME frame, but I couldn’t figure out how to do so.

  • 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-06-17T08:58:59+00:00Added an answer on June 17, 2026 at 8:58 am

    Rather the creating new instance of the frame each time the action perform method is called, you need to create a single shared instance…

    enter image description here

    public class OneFrameToRuleThemAll {
    
        public static void main(String[] args) {
            new OneFrameToRuleThemAll();
        }
    
        public OneFrameToRuleThemAll() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public static class TestPane extends JPanel {
    
            private JFrame frame;
    
            private static final String FRUIT[] = new String[] {
                "Banana",
                "Apple",
                "Manga",
                "Pear"
            };
    
            public TestPane() {
    
                JButton button = new JButton("Fruit");
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
    
                        if (frame == null) {
                            frame = new JFrame("Fruits basket");
                            frame.setSize(100, 200);
                            frame.setLayout(new GridLayout(0, 1));
                            frame.setLocationRelativeTo(TestPane.this);
                            frame.setVisible(true);
                        }
    
                        int index = (int)Math.round(Math.random() * (FRUIT.length - 1));
                        frame.add(new JLabel(FRUIT[index]));
                        frame.getContentPane().validate();
    
                    }
                });
    
                setLayout(new GridBagLayout());
                add(button);
    
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code: JFrame frame = new JFrame(); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel
I have Ex1 below: main(String args[]) { JFrame frame = new JFrame(Title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton
import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; @SuppressWarnings(serial) public class Picture extends JFrame{
public static void main(String[] args) { JFrame frame = new JFrame(test); JPanel panel =
import java.awt.event.*; import javax.swing.*; public class PlannerMain { JFrame frame; JButton makeMap; JPanel panel;
public void run(){ Icon reel = common.ResourcesToAccess.reel; JLabel label = new JLabel(reel); JFrame frame
Every time i make a new JFrame object, the frame appears in the 0,0
My code does not work: JFrame frame = new JFrame(mull); mull panel = new
import javax.swing.*; class Frame extends JFrame{ Frame() { JFrame j = new JFrame(); j.setBounds(100,
I have the following code: JFrame frame = new JFrame(); JScrollPane scrollPane = new

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.