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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:39:22+00:00 2026-05-27T18:39:22+00:00

I have three JPanels inside a main Frame. Clockwise from the left, in the

  • 0

I have three JPanels inside a main Frame. Clockwise from the left, in the first panel I plan to have some controls which dictate the drawing on the panel 2. The third bottom panel will show some informations.

What I understand is, I have to override paintComponent so that I can achieve the desired effect on the second panel. Right now I just want to test it, whether I can draw simple texts on this panel.

But in fact, I am having problem to draw anything in any of the three panels.

The code is given below, I dont know what is the problem.

    public class Demo extends JFrame{
    private JSplitPane splitPaneV;
    private JSplitPane splitPaneH;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;

    public Demo(String string) {
        // TODO Auto-generated constructor stub
        setTitle(string);
        setBackground( Color.gray );

        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

        // Create the panels
        new MyJPanel1();
        new MyJPanel2();
        new MyJPanel3();


        // Create a splitter pane
        splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
        topPanel.add( splitPaneV, BorderLayout.CENTER );

        splitPaneH = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );
        splitPaneH.setLeftComponent( panel1 );
        splitPaneH.setRightComponent( panel2 );

        splitPaneV.setLeftComponent( splitPaneH );
        splitPaneV.setRightComponent( panel3 );
        /*setContentPane(new MyJPanel1());
        setContentPane(new MyJPanel2());
        setContentPane(new MyJPanel3());*/

    }

    private class MyJPanel1 extends JPanel{

        public MyJPanel1(){
            panel1 = new JPanel();
            panel1.setLayout( new BorderLayout() );
            panel1.setPreferredSize(new Dimension(200,500));
        }

        @Override 
        public void paintComponent(Graphics g) { 
            //super.paintComponent(g); 

            g.drawString("BLAH1", 20, 20); 
            g.drawRect(200, 200, 200, 200); 
            //setVisible(true);
        } 


    }

    private class MyJPanel2 extends JPanel{

        public MyJPanel2(){
            panel2 = new JPanel();
            panel2.setLayout( new FlowLayout() );
            panel2.setPreferredSize(new Dimension(1000,500));
        }

        @Override 
        public void paintComponent(Graphics g) {
            super.paintComponent(g); 

            g.drawString("BLAH2", 20, 20); 
            g.drawRect(200, 200, 200, 200); 
    } 


    }


    private class MyJPanel3 extends JPanel{

        public MyJPanel3(){
            panel3 = new JPanel();
            panel3.setLayout( new BorderLayout() );
            panel3.setPreferredSize( new Dimension( 400, 100 ) );
            panel3.setMinimumSize( new Dimension( 100, 50 ) );

        }

        @Override 
        public void paintComponent(Graphics g) { 
            //super.paintComponent(g); 

            g.drawString("BLAH3", 20, 20); 
            g.drawRect(200, 200, 200, 200); 
        } 


    }




    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        Demo frame = new Demo("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(50, 50, 1200, 700);
        frame.pack();
        frame.setVisible(true);


    }
}
  • 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-27T18:39:22+00:00Added an answer on May 27, 2026 at 6:39 pm

    All your JPanel variables refer to plain vanilla JPanels, not to the overridden classes.

    so consider changing this:

    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    
    public Demo(String string) {
        // ...
    
        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );
    
        // Create the panels
        new MyJPanel1();
        new MyJPanel2();
        new MyJPanel3();
    
    
        // ...
    }
    
    private class MyJPanel1 extends JPanel{
    
        public MyJPanel1(){
            panel1 = new JPanel();
            panel1.setLayout( new BorderLayout() );
            panel1.setPreferredSize(new Dimension(200,500));
        }
    
        @Override 
        public void paintComponent(Graphics g) { 
            //super.paintComponent(g); 
    
            g.drawString("BLAH1", 20, 20); 
            g.drawRect(200, 200, 200, 200); 
            //setVisible(true);
        } 
    
    
    }
    //.... etc
    

    to this:

    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    
    public Demo(String string) {
        // ...
    
        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );
    
        // Create the panels
        panel1 = new MyJPanel1();
        panel2 = new MyJPanel2();
        panel3 = new MyJPanel3();
    
    
        // ...
    }
    
    private class MyJPanel1 extends JPanel{
    
        public MyJPanel1(){
            // panel1 = new JPanel();
            setLayout( new BorderLayout() );
            setPreferredSize(new Dimension(200,500));
        }
    
        @Override 
        public void paintComponent(Graphics g) { 
            //super.paintComponent(g); 
    
            g.drawString("BLAH1", 20, 20); 
            g.drawRect(200, 200, 200, 200); 
            //setVisible(true);
        } 
    
    
    }
    //.... do the same with the other JPanel classes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a java application which has a JTabbedPane that contains three JPanels.
I have three TextBox controls on the page <asp:TextBox ID=TextBox1 runat=server AutoPostBack=True OnTextChanged=TextBox_TextChanged TabIndex=1>
I have a custom component subclassed from JPanel, with a keyboard handler. My main
I have a JPanel panel with a bill designed and populated inside it. I
I have a JFrame which has 3 JPanels in GridBagLayout .. Now, when I
I have a Java swing application with a panel that contains three JComboBoxe s
I have a JPanel inside a frame. The contents of the JPanel are supposed
I have a JFrame with four components: three JPanels and a JTabbedPane. Two panels
I would like to have a JRadioPanel with three options. The first two are
I have a JFrame which contains a JPanel . The frame can be resized

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.