Reputation: 3821
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.
Upvotes: 2
Views: 1595
Reputation: 205765
You might also consider Using Invisible Components as Filler to control the vertical spacing. For example, org.gcs.kinetic.ControlPanel
uses Box.createVerticalStrut()
, while this example uses Box.createVerticalGlue()
.
Upvotes: 4
Reputation: 23950
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.
Upvotes: 4