I have splitted frames into three parts such as horizontal part in pink background and vertical part in yellow and blue background like in the image using GridBagConstraints,enter image description here
I'm using the below code to do this,
public Main()
{
JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
maFrame.setSize(1000, 700);
Container container = maFrame.getContentPane();
container.setLayout(new GridBagLayout()); //setting layout of main frame
GridBagConstraints cns = new GridBagConstraints(); //creating constraint
JPanel headPanel = new JPanel(); //creating the header panel
cns.gridx = 0;
cns.gridy = 1;
cns.weightx = 0.3;
cns.weighty = 0.7;
cns.anchor = GridBagConstraints.FIRST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
maFrame.setLocationRelativeTo(null); //centering frame
headPanel.setBackground(Color.YELLOW);
container.add(headPanel, cns);
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
cns.gridx = 1;
cns.gridy = 1;
cns.weightx = 0.7;
cns.weighty = 0.7;
cns.anchor = GridBagConstraints.PAGE_START;
cns.fill = GridBagConstraints.BOTH;
container.add(panel, cns);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.PINK);
cns.gridx = 0;
cns.gridy = 0;
cns.gridwidth = 2;
cns.weightx = 1.0;
cns.weighty = 0.3;
cns.anchor = GridBagConstraints.LAST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
container.add(panel1, cns);
maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
maFrame.pack();
maFrame.setVisible(true); //making the frame visible
}
I want to split the pink background parts into 3 parts and yellow background into two parts. I have tried to do this. But its not working for me. I do not want to use splitpane to do this. Is it possible to achieve it using GridBagConstraints? Could you please suggest me an idea to do this? thanks in advance.
Simple way to do this using GridLayout and separate the contentPane into two row and then bottom row which is divided into two columns.
import javax.swing.*;
import java.awt.*;
class JpanelSplit {
JFrame frame;
JPanel contentPane;
JPanel pinkPanel;
JPanel yellowPanel;
JPanel bluePanel;
JPanel twoPanelContainer;
public JpanelSplit() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel(new GridLayout(2,1));
pinkPanel = new JPanel();
pinkPanel.setBackground(Color.PINK);
yellowPanel = new JPanel();
yellowPanel.setBackground(Color.YELLOW);
bluePanel = new JPanel();
bluePanel.setBackground(Color.BLUE);
twoPanelContainer = new JPanel(new GridLayout(1,2));
twoPanelContainer.add(yellowPanel);
twoPanelContainer.add(bluePanel);
contentPane.add(pinkPanel);
contentPane.add(twoPanelContainer);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new JpanelSplit();
}
}
Related
I have a JPanel with a textfield (textPanel). I added the textPanel for some reasons to another panel (contentPanel) and that to the JFrame. I have added a MouseListener to the textPanel, so that I can drag it around. The new Position is set by the methodcall setLocation (...)
Everytime I start editing the textfield, the textpanel jumps back from its actual location to the center of the JFrame. – Why? What do I have to change?
public void run() {
area1 = new JTextArea();
area1.setText("Hello!");
GridBagLayout layout = new GridBagLayout();
GridBagConstraints cc = new GridBagConstraints();
cc.fill = GridBagConstraints.NONE;
textPanel = new JPanel(layout);
cc.anchor = GridBagConstraints.PAGE_START;
cc.gridx = 0;
textPanel.add(area1, cc);
JPanel contentPanel = new JPanel(new GridBagLayout());
cc = new GridBagConstraints();
cc.anchor = GridBagConstraints.FIRST_LINE_START;
cc.gridx = 0;
cc.gridy = 0;
cc.weightx = 1;
cc.weighty = 1;
MyMouseAdapter mh = new MyMouseAdapter();
textPanel.addMouseListener(mh);
textPanel.addMouseMotionListener(mh);
contentPanel.add(textPanel, cc);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(contentPanel);
frame.setSize(800, 600);
frame.setVisible(true);
}
I have JTextFields and JLabels added to my JPanel (from left to right) every time the JButton is pressed. However, every new JTextField and JLabelthat is added becomes smaller and smaller. How do I fix this?
Also I would like to add a JScrollPane to the JPanel but having problems doing so.
public class MyExample
{
// Field members
static JPanel panel = new JPanel();
static Integer indexer = 1;
static List<JLabel> listOfLabels = new ArrayList<JLabel>();
static List<JTextField> listOfTextFields = new ArrayList<JTextField>();
static JScrollPane scrollPane = new JScrollPane( panel );
public static void main(String[] args)
{
// Construct frame
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
frame.setPreferredSize(new Dimension(2000, 2000));
frame.setTitle("My Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollPane);
// Frame constraints
GridBagConstraints frameConstraints = new GridBagConstraints();
// Construct button
JButton addButton = new JButton("Add");
addButton.addActionListener(new ButtonListener());
// Add button to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 0;
frame.add(addButton, frameConstraints);
// Construct panel
panel.setPreferredSize(new Dimension(1000, 1000));
panel.setLayout(new GridBagLayout());
panel.setBorder(LineBorder.createBlackLineBorder());
// Add panel to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 1;
frameConstraints.weighty = 1;
frame.add(panel, frameConstraints);
// Pack frame
frame.pack();
// Make frame visible
frame.setVisible(true);
}
static class ButtonListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent arg0)
{
// Clear panel
panel.removeAll();
// Create label and text field
JTextField jTextField = new JTextField();
jTextField.setSize(100, 200);
listOfTextFields.add(jTextField);
listOfLabels.add(new JLabel("Label " + indexer));
// Create constraints
GridBagConstraints textFieldConstraints = new GridBagConstraints();
GridBagConstraints labelConstraints = new GridBagConstraints();
// Add labels and text fields
for(int i = 0; i < indexer; i++)
{
// Text field constraints
textFieldConstraints.gridx = i;
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
textFieldConstraints.weightx = 0.5;
textFieldConstraints.insets = new Insets(10, 10, 10, 10);
textFieldConstraints.gridy = 1;
// Label constraints
labelConstraints.gridx = i;
labelConstraints.gridy = 0;
labelConstraints.insets = new Insets(10, 10, 10, 10);
// Add them to panel
panel.add(listOfLabels.get(i), labelConstraints);
panel.add(listOfTextFields.get(i), textFieldConstraints);
}
// Align components
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = indexer;
c.weighty = 1;
panel.add(new JLabel(), c);
// Increment indexer
indexer++;
panel.updateUI();
}
}
}
However, every new JTextField and JLabelthat is added becomes smaller and smaller. How do I fix this?
panel.setPreferredSize(new Dimension(1000, 1000));
Don't set the preferred size. If the size is fixed then as you add more components they need to shrink to fit in the allowed space.
Don't attempt to set the size of any component. Let the layout manager do its job and determine the preferred size of the panel.
When creating a JTextField the code should be something like:
//JTextField jTextField = new JTextField();
JTextField jTextField = new JTextField(10);
This will allow the text field to determine its own preferred size to dispaly about 10 characters.
panel.updateUI();
Don't use updateUI(). That is used internally by Swing when you change the LAF. When you remove/add components you should be using:
panel.revalidate();
panel.repaint();
As for the JScrollPane with panel as a viewport, you should not be adding the panel to the frame at all - setting it as the viewport (like you're doing in the JScrollPane constructor) and adding the JScrollPane is sufficient. Adding the panel itself may be the cause of your problem.
As for the shrinking problem, I am still trying to understand your layout code - your use of GridBagLayout seems a tad overcomplicated to me. Maybe you can draw a simple sketch of how you would like the layout look?
I've attached a screenshot for which the following Border legend applies:
Yellow = JPanel with BorderLayout
Blue = JPanel with GridBagLayout
Fuchsia = JPanel with FlowLayout
There are two panels not blocked out in colors that warrant mentioning:
1) The title panel where the word "Primary" is displayed; this panel is at BorderLayout.NORTH in "Yellow" panel.
2) The image panel where the image of the device is located; this panel is a sibling to "Fuchsia"
"Blue" is at BorderLayout.CENTER in "Yellow" while "Fuchsia" and the image panel are given the following constraints:
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.insets = new Insets(0, 10, 0, 0);
c.fill = GridBagConstraints.BOTH;
//"Blue".add(imagePanel, c);
c.weighty = 0.80;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
//"Blue".add("Fuchsia", c);
As you can probably tell from the image, I'm trying to get rid of the "wasted" space in "Blue" right below "Fuchsia". I don't seem to be able to do it with GridBagConstraints, so am I just using the wrong LayoutManager? It looks to me like "Blue", who is at CENTER in the BorderLayout is just giving each child JPanel half of the available space and reserving the remainder space instead of contracting upward. What am I missing here? Is this simply a matter of setting a preferred or maximum size on "Fuchsia"? it doesn't seem like that will get me where I want to be, since the border around "Fuchsia" (which is covered by my color coding) is where I want the end of the component to be.
Have a look at this output, from this code example :
import java.awt.*;
import javax.swing.*;
public class LayoutTest
{
private void displayGUI()
{
JFrame frame = new JFrame("Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.YELLOW);
contentPane.setLayout(new BorderLayout(2, 2));
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel headingLabel = new JLabel("Primary");
topPanel.add(headingLabel);
contentPane.add(topPanel, BorderLayout.PAGE_START);
JPanel centerPanel = new JPanel();
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.BLUE);
centerPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 0.2;
gbc.gridx = 0;
gbc.gridy = 0;
JPanel imagePanel = new JPanel();
JLabel imageLabel = null;
try
{
imageLabel = new JLabel(
new ImageIcon(
new java.net.URL(
"http://pscode.org/"
+ "tame/screenshot/"
+ "landscape/slider1.gif")));
}
catch(Exception e)
{
e.printStackTrace();
}
imagePanel.add(imageLabel);
centerPanel.add(imagePanel, gbc);
JPanel detailsPanel = new JPanel();
detailsPanel.setOpaque(true);
detailsPanel.setBackground(Color.WHITE);
detailsPanel.setBorder(
BorderFactory.createEmptyBorder(
5, 5, 5, 5));
detailsPanel.setLayout(new GridLayout(0, 1, 5, 5));
JLabel statusLabel = new JLabel("Chassis Status : ");
JLabel usageLabel = new JLabel("Bandwidth Usage : ");
JLabel fanLabel = new JLabel("Fan Status : ");
detailsPanel.add(statusLabel);
detailsPanel.add(usageLabel);
detailsPanel.add(fanLabel);
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 0.8;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 3;
centerPanel.add(detailsPanel, gbc);
contentPane.add(centerPanel, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new LayoutTest().displayGUI();
}
});
}
}
Without using GridBagLayout could be
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class NestedLayout {
private JFrame frame = new JFrame();
private JPanel yellowNorthPanel = new JPanel();
private JPanel yellowPanel = new JPanel();
private JPanel bluePanel = new JPanel();
private JPanel fuchsiaTopPanel = new JPanel();
private JPanel fuchsiaBottonPanel = new JPanel();
public NestedLayout() {
yellowNorthPanel.setBorder(new LineBorder(Color.yellow, 5));
yellowPanel.setLayout(new BorderLayout());
yellowPanel.setBorder(new LineBorder(Color.yellow, 5));
bluePanel.setLayout(new BorderLayout(5, 5));
bluePanel.setBorder(new LineBorder(Color.blue, 5));
fuchsiaTopPanel.setBorder(new LineBorder(Color.cyan, 5));
fuchsiaBottonPanel.setBorder(new LineBorder(Color.cyan, 5));
bluePanel.add(fuchsiaTopPanel, BorderLayout.NORTH);
bluePanel.add(fuchsiaBottonPanel);
yellowPanel.add(bluePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(yellowNorthPanel, BorderLayout.NORTH);
frame.add(yellowPanel);
//frame.pack();
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new NestedLayout();
}
});
}
}
the program below is to position a jpanel at the top left conner of jframe with gridbaglayout but instead a very small box is displayed in center of jframe. when I set the layout of jframe to null, the jpanel displays fine. can someone tell me why the jpanel is compressed to the center of frame with gridbaglayout? i really need to use gridbag. please help
import java.awt.*;
import javax.swing.*; //swing package
public class Major {
//defining the constructor
public Major() {
JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
JPanel headPanel = new JPanel(); //creating the header panel
maFrame.setSize(900, 700); //setting size
maFrame.setBackground(Color.LIGHT_GRAY); //setting color of frame
Container container = maFrame.getContentPane();
container.setLayout(new GridBagLayout()); //setting layout of main frame
GridBagConstraints cns = new GridBagConstraints(); //creating constraint
cns.gridx = 0;
cns.gridy = 0;
maFrame.setLocationRelativeTo(null); //centering frame
headPanel.setBackground(Color.WHITE);
headPanel.setSize(200, 150);
container.add(headPanel, cns);
maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
maFrame.setVisible(true); //making the frame visible
}
//defining the main method
public static void main(String[] args) {
new Major(); //instantiating the class
}
}
Seems like you forgot to provide, weightx and weighty constraints to your GridBagConstraints, As you provide them, you will see your JPanel.
Here I had modified your code with those Constraints.
And never use this line, headPanel.setSize(200, 150);, as I had commented it out, since the constraints I had mentioned will sort this out for you.
Adding a new Code with image :
import java.awt.*;
import javax.swing.*; //swing package
public class Major
{
//defining the constructor
public Major()
{
JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
JPanel headPanel = new JPanel(); //creating the header panel
maFrame.setBackground(Color.LIGHT_GRAY); //setting color of frame
Container container = maFrame.getContentPane();
container.setLayout(new GridBagLayout()); //setting layout of main frame
GridBagConstraints cns = new GridBagConstraints(); //creating constraint
cns.gridx = 0;
cns.gridy = 0;
//cns.gridwidth = 3;
//cns.gridheight = 4;
cns.weightx = 0.3;
cns.weighty = 0.7;
cns.anchor = GridBagConstraints.FIRST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
maFrame.setLocationRelativeTo(null); //centering frame
headPanel.setBackground(Color.WHITE);
container.add(headPanel, cns);
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
cns.gridx = 1;
cns.gridy = 0;
//cns.gridwidth = 7;
//cns.gridheight = 4;
cns.weightx = 0.7;
cns.weighty = 0.7;
cns.anchor = GridBagConstraints.PAGE_START;
cns.fill = GridBagConstraints.BOTH;
container.add(panel, cns);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.DARK_GRAY);
cns.gridx = 0;
cns.gridy = 1;
cns.gridwidth = 2;
//cns.gridheight = 4;
cns.weightx = 1.0;
cns.weighty = 0.3;
cns.anchor = GridBagConstraints.LAST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
container.add(panel1, cns);
//JButton button = new JButton("BUTTON");
//headPanel.add(button);
maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
maFrame.pack();
maFrame.setVisible(true); //making the frame visible
}
//defining the main method
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
public void run()
{
new Major(); //instantiating the class
}
};
SwingUtilities.invokeLater(runnable);
}
}
here is the output :
You must set the weightx and weighty of at least one GridBagConstraint to some value greater than 0.0!
The weight attributes are are used to indicate what happens with extra space if the whole layout is smaller than the available space. If all weights (for one direction) are zero, the default value, the whole layout is centered. If at least one weight is greater than zero, the extra space is distributed to the columns or rows in proportion to its weight, so the layout will occupy all available space.
Same Question, different context
It seems I was too hasty in my accepting before, since the problem is still there. The problem? JLabel takes the liberty of expanding its parent panel when content is added to it.
It's time for reproducing it per "Hovercraft full of eels"-ses suggestion, and here it is:
import java.awt.*;
import javax.swing.*;
public class TestLabel {
public static void main(String[] args) {
// Var inits
JFrame frame;
JPanel panel;
JLabel label;
Container pane;
GridBagConstraints gbc = new GridBagConstraints();
// Frame, content pane, layout inits
frame = new JFrame("Label Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = frame.getContentPane();
pane.setLayout(new GridBagLayout());
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
// Add panels (note gbc weighty and fill carries over all instances)
gbc.weightx = 0.3;
gbc.gridx = 0;
gbc.gridy = 0;
panel = new JPanel();
panel.setBackground(Color.GREEN);
frame.add(panel,gbc);
label = new JLabel("THE PANEL IS NOW DISTORTED TO FIT THIS LABEL WHY IS THIS HAPPENING");
//label = new JLabel("");
label.setOpaque(true);
label.setBackground(Color.WHITE);
panel.add(label);
gbc.weightx = 0.7;
gbc.gridx = 1;
gbc.gridy = 0;
panel = new JPanel();
panel.setBackground(Color.RED);
frame.add(panel,gbc);
gbc.weightx = 0.3;
gbc.gridx = 0;
gbc.gridy = 1;
panel = new JPanel();
panel.setBackground(Color.BLUE);
frame.add(panel,gbc);
gbc.weightx = 0.7;
gbc.gridx = 1;
gbc.gridy = 1;
panel = new JPanel();
panel.setBackground(Color.YELLOW);
frame.add(panel,gbc);
frame.pack();
frame.setSize(800,600);
frame.setVisible(true);
}
}
Results:
As you can see, the green panel is forced wider and throws off my whole layout when text (or, in the original question, and icon) is added to it. I want my layout to remain the same weights, regardless of the content. The reason this came up is because I'm trying to add a scaled image as an icon to the label, as seen in the original question.
Incidentally, setPreferredSize() doesn't seem to work.
Is there a way to fix this?
Original Question
My JLabel element expands dramatically when I add an Icon to it. Why is this happening? Here's the applicable portion of the code:
// Show label and BG color
redLabel.setBackground(Color.RED);
redLabel.setOpaque(true);
// Grab stretched image (already loaded elsewhere in the code) and turn to icon
Img = Img.getScaledInstance(redLabel.getWidth(),12,Image.SCALE_REPLICATE);
ImageIcon icon = new ImageIcon(Img);
// This line throws everything off!
//It's commented out in the first pic, and included in the second.
redLabel.setIcon(icon);
As you can see from the first pic, I've got a label (in red) of width W. What I'm trying to do is stretch my icon to width W and put it in the label.
When I do this, the label expands (by exactly 50 pixels, I think) and also squeezes over the left edge (green). Does anyone have any idea why this is happening?
I've tried several things that are too verbose to explain but can't find the problem :-/
Your component expands because it allocates the necessary space for its Icon.
public class JLabelDemo {
private static BufferedImage bi;
public static void main(String[] args) throws IOException{
loadImage();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
createAndShowGUI();
}
});
}
private static void loadImage() throws IOException{
bi = ImageIO.read(JLabelDemo.class.getResource("../resource/forever-alone.jpg"));
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setBackground(Color.YELLOW);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final JLabel emptyLabel = new JLabel();
final JLabel textLabel = new JLabel("This label has text only");
final JLabel textAndImageLabel = new JLabel("This label has text and image");
textAndImageLabel.setIcon(new ImageIcon(bi));
panel.add(emptyLabel);
panel.add(textLabel);
panel.add(textAndImageLabel);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("Empty label dimensions - " + emptyLabel.getSize());
System.out.println("Text only label dimensions - " + textLabel.getSize());
System.out.println("Image width: " + bi.getWidth() + ", Image height: " + bi.getHeight());
System.out.println("Text and image label dimensions - " +textAndImageLabel.getSize());
}
}
The following is outputted to console:
Empty label dimensions - java.awt.Dimension[width=0,height=0]
Text only label dimensions - java.awt.Dimension[width=129,height=16]
Image width: 194, Image height: 180
Text and image label dimensions - java.awt.Dimension[width=363,height=180]
Consider using a JLayeredPane to add components in layers. There are trips and traps though when doing this in matters of opacity, size and position of components added.
For example,
import java.awt.*;
import javax.swing.*;
public class TestLabel {
private static final Dimension SIZE = new Dimension(800, 600);
public static void main(String[] args) {
GridBagConstraints gbc = new GridBagConstraints();
JPanel defaultPane = new JPanel();
defaultPane.setLayout(new GridBagLayout());
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
// Add panels (note gbc weighty and fill carries over all instances)
gbc.weightx = 0.3;
gbc.gridx = 0;
gbc.gridy = 0;
JPanel panel = new JPanel();
panel.setBackground(Color.GREEN);
defaultPane.add(panel, gbc);
gbc.weightx = 0.7;
gbc.gridx = 1;
gbc.gridy = 0;
panel = new JPanel();
panel.setBackground(Color.RED);
defaultPane.add(panel, gbc);
gbc.weightx = 0.3;
gbc.gridx = 0;
gbc.gridy = 1;
panel = new JPanel();
panel.setBackground(Color.BLUE);
defaultPane.add(panel, gbc);
gbc.weightx = 0.7;
gbc.gridx = 1;
gbc.gridy = 1;
panel = new JPanel();
panel.setBackground(Color.YELLOW);
defaultPane.add(panel, gbc);
defaultPane.setSize(SIZE);
JLabel label = new JLabel("THE PANEL IS NOW DISTORTED TO FIT THIS LABEL WHY IS THIS HAPPENING");
label.setOpaque(true);
label.setBackground(Color.WHITE);
JPanel northPalettePanel = new JPanel();
northPalettePanel.setOpaque(false);
northPalettePanel.add(label);
JPanel palettePanel = new JPanel(new BorderLayout());
palettePanel.setOpaque(false);
palettePanel.setSize(SIZE);
palettePanel.setLocation(0, 0);
palettePanel.add(northPalettePanel, BorderLayout.NORTH);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(SIZE);
layeredPane.add(defaultPane, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(palettePanel, JLayeredPane.PALETTE_LAYER);
JFrame frame = new JFrame("Label Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(layeredPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
Java swing is pretty old for me but if I remember well, setting a preferred size (setPreferredSize()) sometime solve these kind of problem ... Also try top lay with setMaximumSize and setMinimumSize.
You can maybe find more information in java documentation:
http://download.oracle.com/javase/tutorial/uiswing/layout/using.html#sizealignment
Regards!