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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:16:55+00:00 2026-05-23T03:16:55+00:00

I’ve tried working with swing (for the first time) a few weeks ago and

  • 0

I’ve tried working with swing (for the first time) a few weeks ago and I ventured on my own for a bit to get a better feel for it. I seem to be running into a problem with vertical alignment.

Here is the situation: i have a boxlayout(Y_AXiS) JPanel in the West part of a borderlayout. Inside the JPanel w/ boxlayout, I have two other JPanels which I want to be vertically aligned (pushed towards the top of the screen no matter the dimensions of the frame). However, the layout manager seems to put a big vertical space between the two JPanels. I was thinking maybe a gridlayout would be better suited for this, but am unsure. I’ve tried numerous times to google for an answer and to correct the problem with no luck. Help Please!

package com.granet;

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


public class CustomerOrganizer  extends JFrame
{
    public CustomerOrganizer()
    {
        JPanel topPanel = new JPanel();
        topPanel.setBackground(Color.darkGray);

        JLabel customerSlogan = new JLabel("Customer Organizer");

        topPanel.add(customerSlogan);



        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.darkGray);
        leftPanel.setBorder(new EmptyBorder(10,100,00,0));


        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));

        DrawPanel firstTab = new DrawPanel(Color.white, 350, 50);
        DrawPanel secondTab = new DrawPanel(Color.white, 350, 50);

        JLabel firstTabText = new JLabel("First Tab");
        JLabel secondTabText = new JLabel("Second Tab");

        firstTab.setBorder(new EmptyBorder(0,60,60,0));
        secondTab.setBorder(new EmptyBorder(0,60,60,0));

        firstTab.add(firstTabText);
        secondTab.add(secondTabText);


        leftPanel.add(firstTab);
        leftPanel.add(secondTab);

        firstTab.setAlignmentX(Component.LEFT_ALIGNMENT);
        secondTab.setAlignmentX(Component.LEFT_ALIGNMENT);

/*      DOESN'T WORK, I'm pretty sure this changes the point on the jpanel which used for alignment (top, bottom, left or right) 
        firstTab.setAlignmentY(Component.TOP_ALIGNMENT);
        secondTab.setAlignmentY(Component.TOP_ALIGNMENT);
*/      


        add(topPanel, BorderLayout.NORTH);
        add(leftPanel, BorderLayout.WEST);

        pack();

    }

    public static void main(String[] args)
    {
        CustomerOrganizer frame = new CustomerOrganizer();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700,400);
        frame.setVisible(true);

    }
}

DrawPanel.java

package com.granet;

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

public class DrawPanel extends JPanel
{
    Color color;
    int width;
    int height;

    public DrawPanel(Color color, int width, int height)
    {
        this.color=color;
        this.width=width;
        this.height=height;
    }

    public DrawPanel()
    {
        this.color = Color.darkGray;
        this.width=this.getWidth();
        this.height=this.getHeight();
    }

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

and this is what I see when I run it:
http://concertforhope.net/citeforme/javabug.png

Notice that the bottom tab is not pushed up against the top tab.

NVM, I see why. The tabs weren’t that small, I was filling in the background the wrong way (the white boxes didn’t represent the actual tabs). Thanks for the help.

  • 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-23T03:16:55+00:00Added an answer on May 23, 2026 at 3:16 am

    This is not a bug. A boxlayout is supposed to divide the entire space equally between the components.

    You’d probably want a flowlayout vertically or something like that.

    I’d really suggest you get Google WindowBuilder (free) and play a bit with layouts in the GUI.

    In your special case you could also consider JTabbedPane with a tab placement on the right.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace

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.