JButton size and JScrollPane not working well together - java

I am thoroughly confused. I have a pretty decent understanding of how each layout manger works and what each one is used for, but I'm not understanding what combination of layout managers and JPanels are necessary to make what I need work.
What I am trying to accomplish
I have a top bar, and a bottom bar of a container panel NORTH and SOUTH of a BorderLayout.
Within the Center panel, I want an unknown number of buttons 1 or more. Regardless of how many buttons there are they all need to be the same size, if there are dozens then scrolling should start happening once the buttons pass the window size limit.
What I am getting
Depending on the combination of layout mangers and how many nested JPanels I use and all sorts of trouble shooting, I get one massive button filling the entire CENTER element. I get 2 buttons that are the right size, but spread way apart (gap filling the CENTER space), or I get a dozen buttons that are the right size with no scroll.
I can solve any one of these, but then the other breaks. IE if I get a bunch of correctly sized buttons that properly scroll, then when I replace them with a single button its one massive button. Or if I get a single properly sized button then the larger quantity won't scroll etc.
My Code
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.*;
public class TestCode extends JFrame {
private final JFrame frame;
public TestCode(){
frame = new JFrame();
JLabel title = new JLabel("Test Title");
JPanel windowContainer = new JPanel();
JPanel topPanel = new JPanel();
final JPanel middlePanel = new JPanel();
JPanel bottomPanel = new JPanel();
JButton searchButton = new JButton("Search");
JButton browseButton = new JButton("Browse...");
JButton testButton = new JButton("Button 1");
JButton exitButton = new JButton("Exit");
final JTextField searchBar = new JTextField("Search database...");
topPanel.setLayout(new GridLayout(2,0));
topPanel.add(title);
title.setHorizontalAlignment(JLabel.CENTER);
topPanel.setPreferredSize(new Dimension(getWidth(), 100));
// This is a subset of the top section. Top part is two panels, bottom panel is two cells (grid)
JPanel topPanelSearch = new JPanel();
topPanelSearch.setLayout(new GridLayout(0,2));
topPanelSearch.add(searchBar);
topPanelSearch.add(searchButton);
topPanel.add(topPanelSearch);
// PROBLEM AREA STARTS
// middlePanel.setLayout(new FlowLayout());
middlePanel.setLayout(new GridLayout(0, 1, 10, 10));
// middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.PAGE_AXIS));
JPanel innerContainer = new JPanel();
innerContainer.setLayout(new BoxLayout(innerContainer, BoxLayout.PAGE_AXIS));
// innerContainer.setLayout(new FlowLayout());
// innerContainer.setLayout(new GridLayout(0, 1, 10, 10));
for(int i = 0; i < 2; i++){
JButton button = new JButton("Button ");
button.setPreferredSize(new Dimension(400, 100));
JPanel test = new JPanel();
test.add(button);
innerContainer.add(test);
}
JScrollPane midScroll = new JScrollPane(innerContainer);
middlePanel.add(midScroll);
// PROBLEM AREA ENDS
bottomPanel.setLayout(new GridLayout(0, 3));
bottomPanel.setPreferredSize(new Dimension(getWidth(), 100));
bottomPanel.add(testButton);
bottomPanel.add(browseButton);
bottomPanel.add(exitButton);
windowContainer.setLayout(new BorderLayout());
windowContainer.add(topPanel, BorderLayout.NORTH);
windowContainer.add(middlePanel, BorderLayout.CENTER);
windowContainer.add(bottomPanel, BorderLayout.SOUTH);
frame.add(windowContainer);
frame.setTitle("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(480, 800);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args){
TestCode test = new TestCode();
}
}
Visual of some of the fail results
I want the leftmost picture, but buttons should be stacked neatly (like the middle picture) when there are only a few results, and scrollable when there are lots.
What am I doing wrong?

Try with GridBagLayout and a filler component that takes the remaining vertical space and therefore forces the buttons upwards.
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.Box.Filler;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class GridbagButtons extends JFrame {
private final JScrollPane jscrpButtons;
private final JPanel jpButtons;
public GridbagButtons() {
setLayout(new BorderLayout());
jpButtons = new JPanel(new GridBagLayout());
jscrpButtons = new JScrollPane(jpButtons);
add(jscrpButtons, BorderLayout.CENTER);
// add a custom number of buttons
int numButtons = 10;
for (int i = 0; i < numButtons; i++) {
JButton jbButton = new JButton("Button");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = i;
jpButtons.add(jbButton, gbc);
}
// add a vertical filler as last component to "push" the buttons up
GridBagConstraints gbc = new GridBagConstraints();
Filler verticalFiller = new Filler(
new java.awt.Dimension(0, 0),
new java.awt.Dimension(0, 0),
new java.awt.Dimension(0, Integer.MAX_VALUE));
gbc.gridx = 0;
gbc.gridy = numButtons;
gbc.fill = java.awt.GridBagConstraints.VERTICAL;
gbc.weighty = 1.0;
jpButtons.add(verticalFiller, gbc);
setSize(300, 200);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GridbagButtons().setVisible(true);
}
});
}
}

Related

GridBagLayout not moving components no matter what values entered?

I currently have a jpanel with layout set to GridBagLayout, with gbc = new GridBagConstraints();, However for whatever value of x, y or gridwidth,gridheight the items don't move at all.
I would greatly appreciate an expert eye to look over my code to see what I am missing, Thanks in advance.
Edit: Added the imports and the main method
Edit 2: turns out I was thinking the x and y values were pixels and that was the reason it wasn't working
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HomeScreenUI {
public void addobjects(Component componente, Container yourcontainer, GridBagLayout layout, GridBagConstraints gbc, int gridx, int gridy, int gridwidth, int gridheight){
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
layout.setConstraints(componente, gbc);
yourcontainer.add(componente);
}
HomeScreenUI(){
//frame
JFrame frame = new JFrame("Opisa");
//panels, one before button click and one after
JPanel panel = new JPanel();
JPanel panelAfterButtonClick = new JPanel();
GridBagLayout ourlayout;
ourlayout = new GridBagLayout();
panel.setLayout(ourlayout);
panelAfterButtonClick.setLayout(ourlayout);
GridBagConstraints gbc = new GridBagConstraints();
//jlabel that isnt displaying + dimensions
JLabel label = new JLabel("Opisa");
label.setFont(new Font("Helvetica", Font.PLAIN, 70));
//second jlabel that isn't displaying
JLabel label2 = new JLabel("Home");
label2.setFont(new Font("Helvetica", Font.PLAIN, 70));
//adding the labels to the panels
panel.add(label);
panelAfterButtonClick.add(label2);
//button that is displaying both before and after
JButton button = new JButton("Click Me..");
JButton buttonAfterClick = new JButton("Clicked Me..");
//adding the buttons to the jpanel
this.addobjects(label, panel, ourlayout, gbc, 0,0, 3, 1);
this.addobjects(button, panel, ourlayout, gbc, 700, 100, 2, 0);
this.addobjects(label2, panelAfterButtonClick, ourlayout, gbc, 200, 200, 1, 1);
this.addobjects(buttonAfterClick, panelAfterButtonClick, ourlayout, gbc, 700, 10, 2, 0);
//function that changes the panel after the button is clicked
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
frame.setContentPane(panelAfterButtonClick);
frame.invalidate();
frame.validate();
}
});
//adding the panel to the frame and setting the size
frame.add(panel);
frame.setSize(720,1280);
frame.setVisible(true);
}
}
public static void main (String args[]) {
HomeScreenUI hs = new HomeScreenUI();
}
It seems you are using GridBagLayout completely different than it was designed for.
Note you have one instance of GridBagLayout but want to use it for two panels. Each panel needs to have it's own LayoutManager instance.
Then look how many GridBagConstraints you have. Each Component that shall be added needs it's own instance to be properly managed.
Then there are strange values you are passing into GridBagConstraints. I suggest you take the time and go through How to Use GridBagLayout.
I modified your code to create the following GUI.
Here's what it looks like after you left-click the button.
You can swap back and forth between the two panels.
Here are the major changes I made to your code.
Code should be organized like an essay. The most important code should come first, followed by the less important code.
Break your code up into methods and classes. Each method should do one thing and do it well. This is called separation of concerns and it helps you to focus on one part of your code at a time.
To start the Swing application, I made a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
The JFrame code is in the class constructor. The JFrame code has to be called in a certain order. This is the order I use for most of my Swing applications. Don't forget to call the setDefaultCloseOperation method. The JFrame.EXIT_ON_CLOSE parameter exits the application when you close the JFrame.
I used a CardLayout to hold the two subordinate JPanels. I created the CardLayout in its own method.
I created each subordinate JPanel in its own method. I used a GridBagLayout for both subordinate JPanels. As you can see in the code, you have to set quite a few GridBagConstraints parameters.
I created an anonymous ActionListener for each of the JButtons. The ActionListeners swap the two subordinate JPanels in the CardLayout.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class HomeScreenUI {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new HomeScreenUI();
}
});
}
private CardLayout cardLayout;
private JPanel cardPanel;
public HomeScreenUI() {
JFrame frame = new JFrame("Opisa");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.cardPanel = createCardPanel();
frame.add(cardPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createCardPanel() {
cardLayout = new CardLayout();
JPanel panel = new JPanel(cardLayout);
panel.add(createOpisaPanel(), "Opisa");
panel.add(createHomePanel(), "Home");
return panel;
}
private JPanel createOpisaPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
// jlabel that isnt displaying + dimensions
JLabel label = new JLabel("Opisa");
label.setFont(new Font("Helvetica", Font.PLAIN, 72));
panel.add(label, gbc);
gbc.gridy++;
JButton button = new JButton("Click Me..");
// function that changes the panel after the button is clicked
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
cardLayout.show(cardPanel, "Home");
}
});
panel.add(button, gbc);
return panel;
}
private JPanel createHomePanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
// second jlabel that isn't displaying
JLabel label2 = new JLabel("Home");
label2.setFont(new Font("Helvetica", Font.PLAIN, 72));
panel.add(label2, gbc);
gbc.gridy++;
JButton buttonAfterClick = new JButton("Clicked Me..");
buttonAfterClick.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
cardLayout.show(cardPanel, "Opisa");
}
});
panel.add(buttonAfterClick, gbc);
return panel;
}
}

setUndecorated(true) content of the JFrame not displaying

I'm creating a gui with java swing who is supposed to be implemented on an LCD touch screen. The gui will take all the screen and is not supposed to be closable. So "setUndecorated(true)" is essential.
Problem, when I'm using it, all the content of the frame is not displaying, only the buttons appears when I'm moving my mouse over them. Then all the content appears when I maximise and minimise the frame.
Here is the code I'm using :
public static void main(String[] args) {
//Création de notre JFrame
fenetre = new JFrame();
fenetre.setTitle("IHM");
fenetre.setSize(800, 600);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setLocationRelativeTo(null);
support = new JPanel();
///////Adding content to the JPanel()/////////////
fenetre.setContentPane(support);
fenetre.setUndecorated(true);
fenetre.setVisible(true);
}
The only solution I've find so far is to change the size of the frame after set the frame visible like so :
fenetre.setSize(801, 601);
fenetre.setSize(800, 600);
But I'm not really correcting the problem, just getting around it.
(EDIT)
My appologies for that uncomplete code.
Here is a complete exemple that you can test yourself.
First test it with undecorated to false, then try with undecorated to true and visualise the content of the window by maximising and minimising the gui.
Finally, try it with the Quick fix in the bottom of the code.
the code :
package test;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test {
//Declarations
private static JFrame fenetre;
private static JPanel support;
private static JLabel l1, l2;
private static JComboBox<String> c1, c2;
private static JButton b1, b2;
//List to initiate the comboBoxes
private static String[] listC1 = {"Peter", "Alex", "Jhon", "Bran"};
private static String[] listC2 = {"10", "20", "32", "45"};
public static void main(String[] args) {
//Initialisation of the JFrame
fenetre = new JFrame();
fenetre.setTitle("IHM");
fenetre.setSize(800, 600);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setLocationRelativeTo(null);
//Initialisation of the JPanel
support = new JPanel();
support.setBackground(Color.red);
support.setPreferredSize(fenetre.getSize());
//Initialisation of the components
l1 = new JLabel("What's my name");
l2 = new JLabel("What's my age");
c1 = new JComboBox<String>(listC1);
c2 = new JComboBox<String>(listC2);
b1 = new JButton("validate name");
b2 = new JButton("validate age");
//Creating layout
support.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//Adding components
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(20, 20, 0, 0);
gbc.gridx = 0;
gbc.gridy = 0;
support.add(l1, gbc);
gbc.gridy = 1;
support.add(c1, gbc);
gbc.insets = new Insets(20, 20, 20, 0);
gbc.gridy = 2;
support.add(b1, gbc);
gbc.insets = new Insets(20, 20, 0, 20);
gbc.gridx = 1;
gbc.gridy = 0;
support.add(l2, gbc);
gbc.gridy = 1;
support.add(c2, gbc);
gbc.insets = new Insets(20, 20, 20, 20);
gbc.gridy = 2;
support.add(b2, gbc);
//Adding Listeners
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("My name is " + c1.getSelectedItem());
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("My age is " + c2.getSelectedItem());
}
});
//Adding the JPanel to the content pane of the JFrame
fenetre.setContentPane(support);
//Setting undecorated true (switch between true and false for testing)
fenetre.setUndecorated(false);
fenetre.setVisible(true);
//Quick fix, uncomment to test
//fenetre.setSize(801, 601);
//fenetre.setSize(800, 600);
}
}
You don't add any buttons to your JPanel so how do you expect it to display them?
Either way, here's a simple example of displaying a button:
JFrame fenetre = new JFrame();
fenetre.setTitle("IHM");
fenetre.setExtendedState(JFrame.MAXIMIZED_BOTH);
fenetre.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
fenetre.setLocationRelativeTo(null);
JPanel support = new JPanel();
JButton button = new JButton("button");
support.add(button);
fenetre.setContentPane(support);
fenetre.setVisible(true);
Note that you should use a proper layout.
I also added setExtendedState(JFrame.MAXIMIZED_BOTH) to fill the screen, and setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) as you didn't want the screen to be closable.
Problem, when I'm using it, all the content of the frame is not displaying, only the buttons appears when I'm moving my mouse over them.
Components should be added to the panel/frame BEFORE the frame is made visible.
If you add components to a visible frame, then the basic logic is:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // make sure panel is repainted

Adding multiple JButtons moves other JButtons (Java)

The problem I've been having for a while is that whenever I add a JButton to a JPanel, any other JButton that I had on was shifted in that direction.
Here is the first code:
//imports
import java.awt.*;
import javax.swing.*;
public class Example {
public static void main(String[] args) {
// Create the frame and panel and the Grid Bag Constraints
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Create ONE JButton
JButton button1 = new JButton("Button1");
// Set the frame's properties
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.setVisible(true);
// Set Basic Grid Bag Constraints settings.
c.gridx = 0;
c.gridy = 0;
// Set the Insets
c.insets = new Insets(0, 0, 0, 0);
// Add the Grid Bag Constraints and button1 the panel
panel.add(button1, c);
}
}
Everything seems to working right?
Well if we add a second button:
//imports
import java.awt.*;
import javax.swing.*;
public class Example {
public static void main(String[] args) {
// Create the frame and panel and the Grid Bag Constraints
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Create TWO JButtons
JButton button1 = new JButton("Button1");
JButton button2 = new JButton("Button2");
// Set the frame's properties
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.setVisible(true);
// Set Basic Grid Bag Constraints settings.
c.gridx = 0;
c.gridy = 0;
// Set the Insets
c.insets = new Insets(0, 0, 0, 0);
// Add the Grid Bag Constraints and button1 the panel
panel.add(button1, c);
// Set the Insets
c.insets = new Insets(500, 0, 0, 0);
// Add the Grid Bag Constraints and button2 the panel
panel.add(button2, c);
}
}
Then button1 moves down towards button2. Does anyone know why and/or a fix for it?
EDIT: What I am asking, is how do you add another button without moving the other buttons.
I don't know what your intent is. You only state it doesn't do what you expect. So I can't give you an exact solution.
In any case start by reading the section from the Swing tutorial on How to Use GridBagLayout for working examples using a GridBagLayout.
I see two issues:
You never change the gridx/y values. Both components are added to grid (0, 0) which is not how the GridBagLayout should work. Every component needs to be added to a different cell.
The Insets value of (500, ....) seems very big.

jtextarea wont fill panel with GridBagConstraints

From my code I expect my JTextArea to fill the top left border seen below:
But as you can see its taking up a tiny section in the middle.
I am using GridBagConstraints on the panel which contains the components.
There is a main class which calles up a class called frame. This class creates the JFrame and sets the size as well as other things. This is then called from the MainFrame.java which has extended the jframe which creates 3 panels and sets their layout. this is seen below
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame
{
private Panel1 storyPanel;
private Panel2 statsPanel;
private Panel3 commandsPanel;
public MainFrame(String title)
{
super(title);
// Setting Layout
GridBagConstraints gbc = new GridBagConstraints();
storyPanel = new Panel1();
storyPanel.setLayout(new GridBagLayout());
statsPanel = new Panel2();
commandsPanel = new Panel3();
Container p = getContentPane();
p.add(storyPanel, BorderLayout.WEST);
p.add(statsPanel, BorderLayout.EAST);
p.add(commandsPanel, BorderLayout.SOUTH);
}
}
The panel in questions is Panel1 or storyPanel. I have set the layout and the code calls the Panel1.java as seen below:
import javax.swing.*;
import java.awt.*;
public class Panel1 extends JPanel
{
public Panel1()
{
GridBagConstraints gbc = new GridBagConstraints();
//Set size of Panel1
int xsizeP1 = (Frame.xsize() / 2);
int ysizeP1 = (Frame.ysize() / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(" test ");
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//GridBagConstraints setup for components
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
add(scroll, gbc);
}
}
I don't understand why my gbc.fill isnt making the JTextArea fill the top left border of the screen shot.
Thanks in advance for any reply's
-Tom T
changing the layout to border layout
import javax.swing.*;
import java.awt.*;
public class Panel1 extends JPanel
{
public Panel1()
{
//GridBagConstraints gbc = new GridBagConstraints();
//gbc.weightx = 0.1;
//gbc.weighty = 0.1;
BorderLayout b = new BorderLayout();
//Set size of Panel1
int xsizeP1 = (Frame.xsize() / 2);
int ysizeP1 = (Frame.ysize() / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(" test ");
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//gbc.gridx = 0;
//gbc.gridy = 0;
//gbc.weightx = 1;
//gbc.weighty = 1;
//gbc.fill = GridBagConstraints.BOTH;
add(scroll, b.CENTER);
}
}
Don't set the layout of storyPanel, as it's contents have already been added and laid out, so changing the layout manager here will discard any properties you applied. Instead, set the layout in Panel1's constructor before you add any components.
Use GridBagConstraints.BOTH for the fill property. The fill property can only have one specified value
Use weightx and weighty to specify how much of the available space the component should use
For example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MainFrame extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new MainFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private Panel1 storyPanel;
// private Panel2 statsPanel;
// private Panel3 commandsPanel;
public MainFrame(String title) {
super(title);
storyPanel = new Panel1();
Container p = getContentPane();
p.add(storyPanel, BorderLayout.WEST);
p.add(new JLabel("East"), BorderLayout.EAST);
p.add(new JLabel("South"), BorderLayout.SOUTH);
}
public class Panel1 extends JPanel {
public Panel1() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//Set size of Panel1
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(20, 20);
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//GridBagConstraints setup for components
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
add(scroll, gbc);
}
}
}
Having said all that, a BorderLayout would be simpler
public class Panel1 extends JPanel {
public Panel1() {
setLayout(new BorderLayout());
//Set size of Panel1
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(20, 20);
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scroll);
}
}
i tried a border layout and set it to center expecting it to resize but that failed as well
Would suggest that you're making a fundamental mistake some where, as it works fine for me. Remember, set the layout BEFORE you add any components to the container

How to resize a JPanel to fit a JFrame in "docknorth" with no interference to remaining JPanels

I am doing a little test of a demo Swing GUI. In this demo, the JFrame is composed of 3 "master" JPanels. If you will, the first (jp1) is composed of JLabels, and the other two are composed of several other JPanels. I am using MigLayout.
Here is my sample code:
// All the jPanels
JFrame frame = new JFrame();
frame.setLayout(new MigLayout());
JPanel jp1 = new JPanel();
jp1.setLayout(new MigLayout());
jp1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp2 = new JPanel();
jp2.setLayout(new MigLayout());
jp2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp3 = new JPanel();
jp3.setLayout(new MigLayout());
jp3.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp4 = new JPanel();
jp4.setLayout(new MigLayout());
jp4.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp5 = new JPanel();
jp5.setLayout(new MigLayout());
jp5.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp6 = new JPanel();
jp6.setLayout(new MigLayout());
jp6.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp7 = new JPanel();
jp7.setLayout(new MigLayout());
jp7.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel bigPanel1 = new JPanel();
bigPanel1.setLayout(new MigLayout());
bigPanel1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel bigPanel2 = new JPanel();
bigPanel2.setLayout(new MigLayout());
bigPanel2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
//All the labels to be added to JPanel jp1
JLabel label1 = new JLabel();
label1.setText("LABEL1");
JLabel label2 = new JLabel();
label2.setText("LABEL2");
JLabel label3 = new JLabel();
label3.setText("LABEL3");
JLabel label4 = new JLabel();
label4.setText("LABEL4");
jp1.add(label1);
jp1.add(label2);
jp1.add(label3);
jp1.add(label4,"wrap");
bigPanel1.add(jp2);
bigPanel1.add(jp6);
bigPanel1.add(jp3,"grow,wrap");
bigPanel2.add(jp4);
bigPanel2.add(jp7);
bigPanel2.add(jp5,"grow,wrap");
frame.getContentPane().add(jp1,"dock north, wrap");
frame.getContentPane().add(bigPanel1,"span,grow,wrap");
frame.getContentPane().add(bigPanel2,"span,grow,wrap");
frame.pack();
frame.setVisible(true);
Which results in this output:GUI OUTPUT
What I want to achieve is being able to add labels into the 1st JPanel (jp1) without messing with the remainder JPanels width.
Additionally, I want to make the several JPanels inside a bigPanel to occupy its full width, as well as in jp2,jp6 and jp3 to fill bigPanel1.
How should I do this? Thanks in advance.
I have never used MigLayout, and personally dont see the reason if it can be done using default java LayoutManager.
Okay so I used a combination FlowLayout and GridBagLayout to achieve this, along with gc.fill=GridBagConstraints.NONE and gc.anchor=GridBagConstraints.WEST for those panels which we dont want to fill the contentpane width, also updated as per your comment to stop the JPanel/JFrame from growing larger than the given max width when more JLabels are added this was done using a JScrollPane:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class Test {
public Test() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new GridBagLayout());
final JPanel labelPanel = new JPanel();
labelPanel.setBorder(new LineBorder(Color.black));
for (int i = 0; i < 5; i++) {
labelPanel.add(new JLabel("Label" + (i + 1)));
}
final int maxWidth = 200;
final JScrollPane jsp = new JScrollPane(labelPanel) {
#Override
public Dimension getPreferredSize() {
//we set the height by checking if we exceeed the wanted ith thus a scrollbar will appear an we must incoprate that or labels wont be shpwn nicely
return new Dimension(maxWidth, labelPanel.getPreferredSize().width < maxWidth ? (labelPanel.getPreferredSize().height + 5) : ((labelPanel.getPreferredSize().height + getHorizontalScrollBar().getPreferredSize().height) + 5));
}
};
JPanel otherPanel = new JPanel();
otherPanel.add(new JLabel("label"));
otherPanel.setBorder(new LineBorder(Color.black));
JPanel otherPanel2 = new JPanel();
otherPanel2.add(new JLabel("label 1"));
otherPanel2.add(new JLabel("label 2"));
otherPanel2.setBorder(new LineBorder(Color.black));
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 1.0;
gc.weighty = 1.0;
gc.gridx = 0;
gc.gridy = 0;
frame.add(jsp, gc);
gc.fill = GridBagConstraints.NONE;
gc.anchor = GridBagConstraints.WEST;
gc.gridy = 1;
frame.add(otherPanel, gc);
gc.anchor = GridBagConstraints.WEST;
gc.gridy = 2;
frame.add(otherPanel2, gc);
frame.pack();
frame.setVisible(true);
frame.revalidate();
frame.repaint();
}
public static void main(String[] args) {
//Create Swing components on EDT
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test();
}
});
}
}
I have use BorderLayout and FlowLayout to manage the layouts. The frame has two JPanel's and one JPanel in it has two more JPanel's. All the internal panels use FlowLayout to align the JLabels. To arrange these panels on the JFrame I have used BorderLayout.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class LayoutTest {
public LayoutTest() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new GridBagLayout());
JPanel motherPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
motherPanel.add(topPanel, BorderLayout.NORTH);
motherPanel.add(bottomPanel, BorderLayout.CENTER);
JPanel topUpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel topBottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
topUpperPanel.setBorder(new LineBorder(Color.BLACK));
topBottomPanel.setBorder(new LineBorder(Color.BLACK));
bottomPanel.setBorder(new LineBorder(Color.BLACK));
topPanel.add(topUpperPanel, BorderLayout.PAGE_START);
topPanel.add(topBottomPanel, BorderLayout.CENTER);
for(int i = 0; i < 3; i++) {
JLabel label = new JLabel("Label-" + String.valueOf(i));
label.setBorder(new LineBorder(Color.BLACK));
topUpperPanel.add(label);
}
for(int i = 0; i < 2; i++) {
JLabel label = new JLabel("Label-" + String.valueOf(i));
label.setBorder(new LineBorder(Color.BLACK));
topBottomPanel.add(label);
}
for(int i = 0; i < 5; i++) {
JLabel label = new JLabel("Label-" + String.valueOf(i));
label.setBorder(new LineBorder(Color.BLACK));
bottomPanel.add(label);
}
frame.add(motherPanel);
frame.setTitle("Layout Manager");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutTest();
}
});
}
}
P.S: I would suggest you to separate the panels such that there will be "whithout no interference with remaining JPanels."

Categories