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 8554171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:49:12+00:00 2026-06-11T14:49:12+00:00

I just started to learn swing by myself, I’m little bit confused why my

  • 0

I just started to learn swing by myself, I’m little bit confused why my event does not work here:

1.I’m trying to delete everything from my panel if the user click menu bar -> load but it force me to change the panel to final because i’m using it inside the event!

2.I have defined new panel in my event and defined two more container to add to that panel and then add it to the main frame but it seems nothing happening!

Please help me if you can find out what is wrong.
Sorry in advance for messy code.
I appreciate any hints.

public class SimpleBorder {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                myFrame frame = new myFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true); 
            }
        });
    }
}

class MyFrame extends JFrame {

    public MyFrame()
    {

        setSize(500,500);   
        JPanel panel = new JPanel();

        panel.setLayout(null);
        JLabel label = new JLabel("my name is bernard...");
        Color myColor = new Color(10, 150, 80);
        panel.setBackground(myColor);
        label.setFont(new Font("Serif", Font.PLAIN, 25));
        Dimension size = label.getPreferredSize();
        Insets insets = label.getInsets();
        label.setBounds(85+insets.left, 120+insets.top , size.width, size.height);

        panel.add(label);

        JMenuBar menu = new JMenuBar();
        setJMenuBar(menu);


        JMenu col = new JMenu("Collection");
        menu.add(col);

        JMenu help = new JMenu("Help");
        menu.add(help);

        Action loadAction = new AbstractAction("Load")//menu item exit goes here
        {
            private static final long serialVersionUID = 1L;
            public void actionPerformed(ActionEvent event)
            {
                JTextArea text = new JTextArea(10, 40);
                JScrollPane scrol1 = new JScrollPane(text);
                String[] items = {"A", "B", "C", "D"};          
                JList list = new JList(items);
                JScrollPane scrol2 = new JScrollPane(list);
                JPanel panel2 = new JPanel(new BorderLayout());
                panel2 = new JPanel(new GridLayout(1, 2 ));
                panel2.add(scrol1,BorderLayout.WEST);
                panel2.add(scrol2,BorderLayout.EAST);
                add(panel2);        
            }
        };
        JMenuItem load = new JMenuItem(loadAction);
        col.add(load);
        add(panel);

    }

}
  • 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-11T14:49:13+00:00Added an answer on June 11, 2026 at 2:49 pm

    Call revalidate()/repaint() on your JFrame instance after adding the new panel:

    JPanel panel2 = new JPanel(new BorderLayout());
    // panel2 = new JPanel(new GridLayout(1, 2 ));//why this it will overwrite the above layout
    panel2.add(scrol1,BorderLayout.WEST);
    panel2.add(scrol2,BorderLayout.EAST);
    add(panel2);  
    revalidate();
    repaint();
    

    Also call pack() on you JFrame instance so all components are spaced by the layoutmanager. As said in a comment dont extend the JFrame class, create a variable of the frame and initiate all that you need on the frames instance, and dont set a layout to null, unless you love hard work 😛

    Alternatively as mentioned by mKorbel, a CardLayout may be more what you want, it will allow you to use a single JPanel and switch between others/new ones:

    JPanel cards;
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";
    
    //Where the components controlled by the CardLayout are initialized:
    //Create the "cards".
    JPanel card1 = new JPanel();
    ...
    JPanel card2 = new JPanel();
    ...
    
    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    cards.add(card1, BUTTONPANEL);
    cards.add(card2, TEXTPANEL);
    
    //add card panel to frame
    frame.add(cards);
    
    //swap cards
    CardLayout cl = (CardLayout)(cards.getLayout());//get layout of cards from card panel
    cl.show(cards, TEXTPANEL);//show another card
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started to learn C# and I'm a little bit confused about
I just started to learn a little bit about canvas! I want to draw
Well, I've just started to learn Exceptions at college, here is a code that
I just started to learn latex and now I'm trying to create a table.
I just started to learn MVC and am trying to understand how it works.
I just started to learn Android, I'm trying to write a widget which is
I've just started to learn Haskell and I am trying to write a simple
Well, I got problem here with opengl ES stuff (just started to learn about
I just started learning C++(coming from Java & Python) and am trying to learn
I just started using PDO (loving it!). Still trying to learn the basics. Currently

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.