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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:52:59+00:00 2026-05-25T06:52:59+00:00

I made a JComponent that displays a rectangle of a specified color. (Haven’t found

  • 0

I made a JComponent that displays a rectangle of a specified color. (Haven’t found any other way to achieve this effect). Problem is, it doesn’t follow JFrame.pack() and Layout Managers as expected.

Code:

import java.awt.*;
import javax.swing.*;

public class FooRunnable implements Runnable{

private class ColorSample extends JComponent{

    private Color sampleColor;
    private int width, height;

    public ColorSample(int rgb, int w, int h){
        sampleColor = new Color(rgb);
        width = w;
        height = h;
    }

    public Dimension getSize(){
        return new Dimension(width, height);
    }

    public int getWidth(){
        return width;
    }

    public int getHeight(){
        return height;
    }

    public boolean isDisplayable(){
        return true;
    }

    public void paintComponent(Graphics g){
        g.setColor(sampleColor);
        g.fillRect(0, 0, width, height);
    }

}

public void run(){
    JFrame mainFrame = new JFrame();
    //mainFrame.setSize(500, 300);
    Container mainContent = mainFrame.getContentPane();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainContent.setLayout(new BoxLayout(mainContent, BoxLayout.PAGE_AXIS));

    JPanel specifyFilePanel = new JPanel();
    specifyFilePanel.setLayout(new BoxLayout(specifyFilePanel, BoxLayout.LINE_AXIS));
    JLabel filenameLabel = new JLabel("File:       ");
    JButton browseButton = new JButton("Browse...");
    specifyFilePanel.add(Box.createHorizontalStrut(8));
    specifyFilePanel.add(filenameLabel);
    specifyFilePanel.add(browseButton);
    specifyFilePanel.add(Box.createHorizontalStrut(8));

    JPanel colorStatusPanel = new JPanel();
    colorStatusPanel.setLayout(new BoxLayout(colorStatusPanel, BoxLayout.Y_AXIS));
    JLabel statusLabel = new JLabel("");
    JButton roll = new JButton("Operate");
    colorStatusPanel.add(new ColorSample(Color.red.getRGB(), 50, 100));
    colorStatusPanel.add(statusLabel);
    colorStatusPanel.add(roll);

    mainContent.add(Box.createVerticalStrut(5));
    mainContent.add(specifyFilePanel);
    mainContent.add(Box.createVerticalStrut(10));
    mainContent.add(colorStatusPanel);
    mainContent.add(new JPanel());
    mainFrame.pack();
    mainFrame.setVisible(true);
}

}

I tried experimenting between pack and explicitly specifying the frame’s size. Here are the default appearances of my GUI on various settings:

Plain mainFrame.pack():
mainFrame.pack()

mainFrame.setSize(500, 500):
enter image description here

mainFrame.setSize(500, 300):
mainFrame.setSize(500, 300)

The closest to what I intend to achieve is mainFrame.setSize(500, 500) although, as I plan to add a few more components, I expect it will be fragile. As you see, in the other two, the “Operate” button overlaps with the ColorSample Component—like it’s not following the Layout Manager I set. And then see how pack cuts of the ColorSample Component. Any tips on how I can achieve the effect I want?

  • 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-25T06:52:59+00:00Added an answer on May 25, 2026 at 6:52 am

    LayoutManagers are free to size/position components as they deem appropriate, components cannot force them but only give hints in their getXXSize (XX == min/pref/max) methods. So the best a component implementation can do is

    • implement all getXXSize and return the size they ideally want
    • implement paintComponent to cope with a differing size

    a snippet only

    public class MyBox extends JComponent {
         Dimension boxSize;
    
         public void setBoxSize(Dimension box) {
             this.boxSize = new Dimension(box);
             ...   
         } 
    
         @Override
         public void paintComponent(Graphics g) {
             super.paintComponent(g);
             // position the box in the actual size
             // and paint it 
         }
    
         @Override
         public Dimension getPreferredSize() {
             return getBoxSize();
         }
         @Override // same for min/max
         public Dimension getM...Size( {
             return getBoxSize();
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this component made it with jquery, My problem it's that want to
in an application I made that uses AWT standard classes, a call to Color
I've made a custom TableCellRenderer that displays a JPanel. When the JPanel contains interactive
I have a component that is made up of various components such as a
Made this nice little loop for hiding and showing div's, works as a charm
I made an iphone app to capture image from camera and to set that
I made an application that passes trough an XML file and extracts the entries
I made a little PHP script that checks if an email is valid. The
I made a custom component that extends View and overrides its onMeasure() , the
i have a winsock component made with vb.net in a class library that has

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.