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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:12:35+00:00 2026-06-13T13:12:35+00:00

I am working on a project where I need to have a buttonPanel in

  • 0

I am working on a project where I need to have a buttonPanel in the middle of two other JPanels which hold canvases(the canvases draw rectangles). I am stuck on how to do this.

My GUI Class:

import javax.swing.*; 
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUI extends JFrame implements ActionListener 
{
private static final Dimension PREF_SIZE = new Dimension(1350, 450);

MyCanvas leftCanvas = new MyCanvas();
MyCanvas rightCanvas = new MyCanvas();
ArrayList<JButton> buttonList = new ArrayList<JButton>(); 
JPanel buttonPanel, leftPanel, rightPanel;

public GUI()
{
  super("Elevators");
    //setSize(800,800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //setVisible(true);

    JPanel mainPanel = new JPanel();

    mainPanel.setLayout(new GridLayout(1,3));

    leftPanel = new JPanel(); 
    leftPanel.add(leftCanvas);
    rightPanel = new JPanel();    
    rightPanel.add(rightCanvas);

    buttonPanel = new JPanel();     
    buttonPanel.setLayout(new GridLayout(12,1));   
    buttonPanel.setSize(450,450);




    add(mainPanel); 

    for(int i=0; i<12; i++)
    {
        buttonList.add(new JButton(""+i));
        JButton btn = buttonList.get(i);
        buttonPanel.add(btn);
        l
    }
    mainPanel.add(buttonPanel, BorderLayout.CENTER);
    mainPanel.add(leftPanel, BorderLayout.EAST);
    mainPanel.add(rightPanel, BorderLayout.WEST);

    createAndShowGui();
}
@Override
public Dimension getPreferredSize() {
    return PREF_SIZE;
}
private static void createAndShowGui() {
  UI frame = new UI();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
 // frame.setLocationByPlatform(true);
  frame.setVisible(true);
}


public void actionPerformed(ActionEvent e)
{

}

}    

My Canvas Class (does not have much in it yet):

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;

public class MyCanvas extends Canvas 
{
private Elevator e;

int xPos =0;
int yPos=0;

public MyCanvas()
{
   setSize(600,600);
   repaint();
}


public void paint(Graphics g)
{
   g.setColor(Color.BLACK);
   g.fillRect(xPos,yPos,100, 100);


}

public void actionPerformed(ActionEvent e)
{
    repaint();
}
public void setElevator(Elevator ev)
{
    e = ev;
}
}

Want to look like this
Want to look like this

What it looks like right now
What it looks like right now

  • 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-13T13:12:37+00:00Added an answer on June 13, 2026 at 1:12 pm

    You’re fighting against the layout manager. In the end, the layout manager will win.

    // You really shouldn't be doing this, mixing heavy and light weight components is
    // simply asking for trouble.
    //public class MyCanvas extends Canvas 
    public class MyCanvas extends JPanel
    {
        private Elevator e;
    
        int xPos =0;
        int yPos=0;
    
        public MyCanvas()
        {
           // Don't do this, the layout manager will overwrite the values
           //setSize(600,600);
           // Don't do this, it doesn't actually do anything as you ain't in
           // in position to paint yet...
           //repaint();
        }
    
        // Do this instead...
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }
    
    
        public void paint(Graphics g)
        {
            // You MUST do this...
            super.paint(g);
            g.setColor(Color.BLACK);
            g.fillRect(xPos,yPos,100, 100);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            repaint();
        }
    
        public void setElevator(Elevator ev)
        {
            e = ev;
        }
    }
    

    Take some time to read through:

    • Using Layout Managers
    • Performing Custom Painting

    For some useful information that will help you going forward.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project I need to be working on from two different computers,
I am working on a project in which I need to have the application
I am working on a Java project and need to have a keypress simulate
I need to have an at-home project now that I'm working on Python/Django at
I have been working on a C# 4.0 WPF project and need to figure
I have a MVC 4 project I am currently working on and need some
I'm working on a (c++, opengl) project where I need to have lots of
I am currently working on a project where i need to have an in
I am working on a project where I need to have data inherit at
I'm working on a project where I need to have both an executable so

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.