GridBagLayout 2 JPanels - One less width than other - java

I would like to find out how exactly would I go about to having two JPanels on one JPanel, all using GridBagLayout.
Basically you have your Top Most JPanel and then with 2 JPanels on their as below requirement:
--------------------------------------
| | |
| | |
| | |
| JPanel 1 | JPanel |
| | 2 |
| | |
| | |
--------------------------------------
Basically I would need JPanel 1 to be bigger than JPanel 2. But also JPanel 2 must NOT resize, JPanel 1 should only resize if the main panel gets resized.
Any idea's?

Have a look at this code example and see if this is what you wanted :
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GridBagExample {
private JPanel leftPanel;
private JPanel rightPanel;
private GridBagConstraints gbc;
private Random random;
public GridBagExample() {
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(5, 5, 5, 5);
random = new Random();
}
private void displayGUI() {
JFrame frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = getPanel();
contentPane.setLayout(new GridBagLayout());
leftPanel = getPanel();
rightPanel = getPanel();
addComp(contentPane, leftPanel, 0, 0, 1, 1,
GridBagConstraints.BOTH, 0.7, 1.0);
addComp(contentPane, rightPanel, 1, 0, 1, 1,
GridBagConstraints.BOTH, 0.3, 1.0);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp, int gridx,
int gridy, int gridwidth, int gridheight,
int fill, double weightx, double weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add(comp, gbc);
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
return panel;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new GridBagExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
Though if you want your right JPanel not to change size, you can pass GridBagConstraints.NONE to the function instead of GridBagConstraints.BOTH as done by me. Since without the actual contents known for this JPanel which will become its part, its hard to present its true size.
OUTPUT :
EDIT :
Updating my code, to better explain, as to what I am saying, though I used user2699405's (OP) ideas too from the comments.
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GridBagExample {
private JPanel leftPanel;
private JPanel rightPanel;
private GridBagConstraints gbc;
private Random random;
public GridBagExample() {
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(5, 5, 5, 5);
random = new Random();
}
private void displayGUI() {
JFrame frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = getPanel();
contentPane.setLayout(new GridBagLayout());
leftPanel = getPanel();
rightPanel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return (new Dimension(100, 100));
}
};
addComp(contentPane, leftPanel, 0, 0, 1, 1,
GridBagConstraints.BOTH, 1.0, 1.0);
addComp(contentPane, rightPanel, 1, 0, 1, 1,
GridBagConstraints.NONE, 0.0, 1.0);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp, int gridx,
int gridy, int gridwidth, int gridheight,
int fill, double weightx, double weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add(comp, gbc);
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
return panel;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new GridBagExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}

Related

GridBag Layout acting as a flow layout , within a card layout. What am I doing wrong?

This code uses the card layout, as I want to change the window display rather than have multiple windows (or frames). Therefore I want multiple panels, which seems to work.
However within the panels I want to use the gridbag layout for positioning components. But it's not working! It acts as a flow layout. Can anyone help me get over this hurdle? Please.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class another
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel2 panel2;
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane);
panel2 = new MyPanel2();
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
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 another().displayGUI();
}
});
}
}
class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
private JPanel contentPane;
private JPanel myPanel1;
public MyPanel(JPanel panel)
{
contentPane = panel;
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("Label2");
jcomp3 = new JLabel ("Label3");
jcomp4 = new JButton ("openNewWindow");
myPanel1 = new JPanel();
//adjust size and set layout
setPreferredSize (new Dimension (600, 600));
setLayout (new GridBagLayout());
//set component bounds (only needed by Absolute Positioning)
/*
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setLocation(0, 0);
jcomp4.setSize(315, 25);
*/
insert(jcomp2, 0, 0, 1, 1);
insert(jcomp3, 0, 1, 1, 1);
insert(jcomp4, 1, 0, 1, 1);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
//add components
//add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
public void insert(Component c, int gridX, int gridY, int gridW, int gridH)
{
GridBagConstraints constraint = new GridBagConstraints();
constraint.gridx = gridX;
constraint.gridy = gridY;
constraint.gridwidth = gridW;
constraint.gridheight = gridH;
constraint.anchor = GridBagConstraints.LINE_START;
myPanel1.add(c, constraint);
}
}
class MyPanel2 extends JPanel {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public MyPanel2() {
//construct components
jcomp1 = new JButton ("test1");
jcomp2 = new JButton ("test2");
jcomp3 = new JButton ("test3");
jcomp4 = new JTextField (5);
//adjust size and set layout
setPreferredSize (new Dimension (395, 156));
setLayout (null);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (20, 45, 100, 25);
jcomp2.setBounds (135, 60, 100, 25);
jcomp3.setBounds (260, 35, 100, 25);
jcomp4.setBounds (105, 115, 100, 25);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
}
You look to be unnecessarily over-complicating things. You create a JPanel called contentPane and give it a CardLayout, all well and good, but then for some reason you pass that contentPane JPanel into the MyPanel constructor,
JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane);
panel2 = new MyPanel2();
and then within the constructor you assign components to this same contentPane JPanel -- but why? You assign GridBagLayout to the MyPanel this instance, but then add components to the myPanel1 variable within it in a GridBag way when this JPanel has the JPanel default FlowLayout:
class MyPanel extends JPanel {
// ....
private JPanel contentPane;
private JPanel myPanel1;
public MyPanel(JPanel panel) {
contentPane = panel;
// ....
myPanel1 = new JPanel();
setLayout (new GridBagLayout()); // you set *** this *** to GridBagLayout
insert(jcomp2, 0, 0, 1, 1);
insert(jcomp3, 0, 1, 1, 1);
insert(jcomp4, 1, 0, 1, 1);
add (jcomp2); // and then you add components to this without GridBagConstraints ?
add (jcomp3);
add (jcomp4);
}
public void insert(Component c, int gridX, int gridY, int gridW, int gridH) {
// ....
// but then add components in a GridBagLayout way into the myPanel1 JPanel???
myPanel1.add(c, constraint);
}
}
Better to set myPanel to use GridBagLayout:
// adjust size and set layout
setPreferredSize(new Dimension(600, 600));
// setLayout(new GridBagLayout());
myPanel1.setLayout(new GridBagLayout());
Rather than this
Again it seems that you're over-complicating what should be a simple endeavor.
For example:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class SimpleCardExample extends JPanel {
private static final int EB_GAP = 8;
private CardLayout cardLayout = new CardLayout();
public SimpleCardExample() {
setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
setLayout(cardLayout);
add(new GridBagPanel(this), GridBagPanel.class.getSimpleName());
add(new NextPanel(this), NextPanel.class.getSimpleName());
}
public void nextCard() {
cardLayout.next(this);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("SimpleCardExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SimpleCardExample());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class GridBagPanel extends JPanel {
private static final int GAP = 6;
private static final Insets GBC_INSETS = new Insets(GAP, GAP, GAP, GAP);
public GridBagPanel(SimpleCardExample scExample) {
setLayout(new GridBagLayout());
add(new JLabel("Title Goes Here", SwingConstants.CENTER), createGbc(0, 0, 2, 1));
add(new JLabel("Name:"), createGbc(0, 1, 1, 1));
add(new JTextField(15), createGbc(1, 1, 1, 1));
add(new JLabel("Phone:"), createGbc(0, 2, 1, 1));
add(new JTextField(15), createGbc(1, 2, 1, 1));
add(new JLabel("Address:"), createGbc(0, 3, 1, 1));
add(new JTextField(15), createGbc(1, 3, 1, 1));
add(new JButton(new NextAction("Next", scExample)), createGbc(0, 4, 2, 1));
}
private GridBagConstraints createGbc(int x, int y, int w, int h) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbc.insets = GBC_INSETS;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
return gbc;
}
}
class NextPanel extends JPanel {
public NextPanel(SimpleCardExample scExample) {
add(new JButton(new NextAction("Next", scExample)));
}
}
class NextAction extends AbstractAction {
private SimpleCardExample scExample;
public NextAction(String name, SimpleCardExample scExample) {
super(name);
this.scExample = scExample;
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
scExample.nextCard();
}
}

How to align the elements to the top in a GridBagLayout?

I want to put a text element in a GridBagLayout after clicking on a button.
But the elements are vertically centered. So how can I force the grid bag layout to position the elements to the top?
Here is an example code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class RefreshPanel {
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JTextPane [] textPane;// = new JTextPane[1];
private JScrollPane scrollbar;
private ArrayList arrayList = new ArrayList();
private JButton newItem = new JButton("new");
private GridBagLayout gbl = new GridBagLayout();
private JMenu menuStart= new JMenu("Start");
private JMenu menuEdit= new JMenu("Edit");
private JMenu menuWindow= new JMenu("Window");
private JMenu menuHelp= new JMenu("help");
private JMenuBar menuBar = new JMenuBar();
RefreshPanel() {
menuBar.add(menuStart);
menuBar.add(menuEdit);
menuBar.add(menuWindow);
menuBar.add(menuHelp);
scrollbar = new JScrollPane(panel);
panel.setBackground(Color.WHITE);
panel.setLayout(gbl);
addButtonListener();
createFrame();
} //constructor
public void addButtonListener() {
newItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
arrayList.add("data");
textPane = generateTextPane(arrayList.size(), arrayList);
System.out.println("Länge: "+textPane.length);
for(int i=0;i<textPane.length;i++) {
System.out.println(textPane[i].getText());
addComponent(panel, gbl, textPane[i], 0, i, 1, 1, 1, 0);
panel.revalidate();
}
frame.revalidate();
}
});
}
private JTextPane[] generateTextPane(int arraySize, ArrayList arrayList) {
JTextPane [] textPane = new JTextPane[arraySize];
for(int i=0;i<textPane.length;i++) {
textPane[i]=new JTextPane();
textPane[i].setText((String) arrayList.get(i));
System.out.println("length >> "+textPane.length);
}
return textPane;
}
public void addComponent(Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbc.anchor = GridBagConstraints.SOUTH;
gbl.setConstraints( c, gbc );
cont.add( c );
}
public void createFrame() {
frame.add(menuBar, BorderLayout.NORTH);
frame.add(scrollbar, BorderLayout.CENTER);
frame.add(newItem, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300,300));
frame.setVisible(true);
}
public static void main(String [] args) {
new RefreshPanel();
}
}
I know that the grids are dynamically changing their size when I put a new element.
Any ideas how can I solve my problem?
I have changed your GridBagLayout properties. Check this:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class RefreshPanel {
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JTextPane [] textPane;// = new JTextPane[1];
private JScrollPane scrollbar;
private ArrayList arrayList = new ArrayList();
private JButton newItem = new JButton("new");
private GridBagLayout gbl = new GridBagLayout();
private JMenu menuStart= new JMenu("Start");
private JMenu menuEdit= new JMenu("Edit");
private JMenu menuWindow= new JMenu("Window");
private JMenu menuHelp= new JMenu("help");
private JMenuBar menuBar = new JMenuBar();
RefreshPanel() {
menuBar.add(menuStart);
menuBar.add(menuEdit);
menuBar.add(menuWindow);
menuBar.add(menuHelp);
scrollbar = new JScrollPane(panel);
panel.setBackground(Color.WHITE);
gbl.columnWidths = new int[]{0, 0, 0, 0};
gbl.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl);
addButtonListener();
createFrame();
} //constructor
public void addButtonListener() {
newItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
arrayList.add("data");
textPane = generateTextPane(arrayList.size(), arrayList);
System.out.println("Länge: "+textPane.length);
for(int i=0;i<textPane.length;i++) {
System.out.println(textPane[i].getText());
panel.add(textPane[i],new GridBagConstraints( 0, i, 1, 1,
1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
panel.revalidate();
}
frame.revalidate();
}
});
}
private JTextPane[] generateTextPane(int arraySize, ArrayList arrayList) {
JTextPane [] textPane = new JTextPane[arraySize];
for(int i=0;i<textPane.length;i++) {
textPane[i]=new JTextPane();
textPane[i].setText((String) arrayList.get(i));
System.out.println("length >> "+textPane.length);
}
return textPane;
}
public void addComponent(Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbc.anchor = GridBagConstraints.SOUTH;
gbl.setConstraints( c, gbc );
cont.add( c );
}
public void createFrame() {
frame.add(menuBar, BorderLayout.NORTH);
frame.add(scrollbar, BorderLayout.CENTER);
frame.add(newItem, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300,300));
frame.setVisible(true);
}
public static void main(String [] args) {
new RefreshPanel();
}
}

Increasing distance from top of JPanel

I'm trying to increase the distance of my JButtons from the top of my Panel to make it more visually appealing, i've tried using an invisible button but have had no luck.
public class SimpleBorder {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border etched = (Border) BorderFactory.createEtchedBorder();
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
JTextArea text = new JTextArea(10, 40);
JScrollPane scrol = new JScrollPane(text);
JScrollPane scrol2 = new JScrollPane(list);
JPanel panel= new JPanel();
panel.add(scrol2,BorderLayout.WEST);
panel.add(scrol, BorderLayout.EAST);
panel.setBorder(etched);
frame.add(panel);
frame.setVisible(true);
}
}
Any ideas ?
The key basically lies, in the fact, that the component doesn't knows it's actual size, till frame.pack() won't be called. Hence after this I am performing this calculation, to determine how much empty space to put for Border and again calling frame.pack() to repack() everything after putting the Border.
Please do have a look at this example, and see if this suite your needs :
import java.awt.*;
import javax.swing.*;
public class MainMenu {
private JButton playButton;
private JButton instructionButton;
private JButton scoreboardButton;
private JButton exitButton;
private JPanel menuPanel;
private GridBagConstraints gbc;
public MainMenu() {
gbc = new GridBagConstraints();
gbc.insets = new Insets(15, 15, 15, 15);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
}
private void displayGUI() {
JFrame frame = new JFrame("Main Menu");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout());
menuPanel = new JPanel(new GridBagLayout());
menuPanel.setOpaque(true);
menuPanel.setBackground(Color.BLACK);
playButton = new JButton("Play");
instructionButton = new JButton("Instructions");
scoreboardButton = new JButton("Scoreboard");
exitButton = new JButton("Exit");
addComp(menuPanel, playButton, 0, 0, 1, 1, 1.0, 0.20,
GridBagConstraints.HORIZONTAL);
addComp(menuPanel, instructionButton, 0, 1, 1, 1, 1.0, 0.20,
GridBagConstraints.HORIZONTAL);
addComp(menuPanel, scoreboardButton, 0, 2, 1, 1, 1.0, 0.20,
GridBagConstraints.HORIZONTAL);
addComp(menuPanel, exitButton, 0, 3, 1, 1, 1.0, 0.20,
GridBagConstraints.HORIZONTAL);
contentPane.add(menuPanel);
frame.setContentPane(contentPane);
frame.pack();
contentPane.setBorder(
BorderFactory.createEmptyBorder(
contentPane.getHeight() - (contentPane.getHeight() / 4),
20, 5, 20));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp,
int gridx, int gridy,
int gridwidth, int gridheight,
double weightx, double weighty,
int fill) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
panel.add(comp, gbc);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new MainMenu().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
Here is the output :
EDIT 1 :
Moreover, if you will use GridLayout instead of using GridBagLayout for the MainMenu, then I guess the results will be more promising. Please have a look at this example for that change :
import java.awt.*;
import javax.swing.*;
public class MainMenu {
private JButton playButton;
private JButton instructionButton;
private JButton scoreboardButton;
private JButton exitButton;
private JPanel menuPanel;
private void displayGUI() {
JFrame frame = new JFrame("Main Menu");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new GridBagLayout());
menuPanel = new JPanel(new GridLayout(0, 1, 5, 5));
menuPanel.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
menuPanel.setOpaque(true);
menuPanel.setBackground(Color.BLACK);
playButton = new JButton("Play");
instructionButton = new JButton("Instructions");
scoreboardButton = new JButton("Scoreboard");
exitButton = new JButton("Exit");
menuPanel.add(playButton);
menuPanel.add(instructionButton);
menuPanel.add(scoreboardButton);
menuPanel.add(exitButton);
contentPane.add(menuPanel);
frame.setContentPane(contentPane);
frame.pack();
contentPane.setBorder(
BorderFactory.createEmptyBorder(
contentPane.getHeight() -
(contentPane.getHeight() -
(3 * menuPanel.getHeight())),
20, 0, 20));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new MainMenu().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
EDIT 2 :
Another variant is looking much better, though, this time, the base JPanel is using GridLayout and the MenuPanel is using GridBagLayout. Please have a look at this example :
import java.awt.*;
import javax.swing.*;
public class MainMenu {
private JButton playButton;
private JButton instructionButton;
private JButton scoreboardButton;
private JButton exitButton;
private JPanel menuPanel;
private GridBagConstraints gbc;
public MainMenu() {
gbc = new GridBagConstraints();
gbc.insets = new Insets(15, 15, 15, 15);
gbc.anchor = GridBagConstraints.CENTER;
}
private void displayGUI() {
JFrame frame = new JFrame("Main Menu");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new GridLayout(1, 1, 5, 2));
menuPanel = new JPanel(new GridBagLayout());
menuPanel.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
menuPanel.setOpaque(true);
menuPanel.setBackground(Color.BLACK);
playButton = new JButton("Play");
instructionButton = new JButton("Instructions");
scoreboardButton = new JButton("Scoreboard");
exitButton = new JButton("Exit");
addComp(menuPanel, playButton, 0, 0, 1, 1, 1.0, 0.10,
GridBagConstraints.CENTER);
addComp(menuPanel, instructionButton, 0, 1, 1, 1, 1.0, 0.10,
GridBagConstraints.CENTER);
addComp(menuPanel, scoreboardButton, 0, 2, 1, 1, 1.0, 0.10,
GridBagConstraints.CENTER);
addComp(menuPanel, exitButton, 0, 3, 1, 1, 1.0, 0.10,
GridBagConstraints.CENTER);
contentPane.add(menuPanel);
frame.setContentPane(contentPane);
frame.pack();
contentPane.setBorder(
BorderFactory.createEmptyBorder(
contentPane.getHeight() -
(contentPane.getHeight() -
(2 * menuPanel.getHeight()) + 100),
20, 2, 20));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp,
int gridx, int gridy,
int gridwidth, int gridheight,
double weightx, double weighty,
int fill) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
panel.add(comp, gbc);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new MainMenu().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}

How would I be able to achieve this expandable layout in Java? Flexible BoxLayout etc

I would like to be able to have three JPanels p1 p2 and p3, and have them lay out like so:
I have been playing around with FlowLayout, BoxLayout etc but I'm not really sure if I am heading in the right direction. I am quite new with Java so I don't know what does what if I'm quite honest.
I like how BoxLayout works, resizing the panels, but I would like to be able to give it some sort of width attribute.
I am not using a visual designer for this, this is my window code at the moment:
private void initialize() {
Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame();
frame.setLayout(new GridLayout(1, 3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
dimensions.height / 2 - WINDOW_HEIGHT / 2,
WINDOW_WIDTH, WINDOW_HEIGHT);
JPanel p1 = new JPanel(new BorderLayout());
p1.setBackground(Color.red);
JPanel p2 = new JPanel(new BorderLayout());
p2.setBackground(Color.black);
JPanel p3 = new JPanel(new BorderLayout());
p3.setBackground(Color.blue);
frame.add(p2);
frame.add(p1);
frame.add(p3);
}
Any pointers appreciated!
EDIT: I have managed to get it to work how I wanted, thanks to mKorbel. The right column isn't laid out exactly as I was going to do it but I actually changed my mind and decided to keep the other layout.
The code:
import java.awt.*;
import javax.swing.*;
public class PanelWindow extends JFrame {
private static final long serialVersionUID = 1L;
private static final int WINDOW_HEIGHT = 600;
private static final int WINDOW_WIDTH = 800;
private JPanel p1, p2, p3;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
PanelWindow panelWindow = new PanelWindow();
}
});
}
public PanelWindow() {
initialize();
}
private void initialize() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
p1 = new JPanel();
p1.setBackground(Color.red);
p2 = new JPanel();
p2.setBackground(Color.green);
p3 = new JPanel();
p3.setBackground(Color.blue);
gbc.gridx = gbc.gridy = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 97;
gbc.insets = new Insets(2, 2, 2, 2);
add(p1, gbc);
gbc.gridy = 1;
gbc.weightx = gbc.weighty = 3;
gbc.insets = new Insets(2, 2, 2, 2);
add(p2, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.weightx = 20;
gbc.insets = new Insets(2, 2, 2, 2);
add(p3, gbc);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
dimensions.height / 2 - WINDOW_HEIGHT / 2,
WINDOW_WIDTH, WINDOW_HEIGHT);
setTitle("Panel Window");
}
}
not my favorite LayoutManager, example by using GridBagLayout, easiest could be to use MigLayout, maybe ...
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class BorderPanels extends JFrame {
private static final long serialVersionUID = 1L;
public BorderPanels() {
setLayout(new GridBagLayout());// set LayoutManager
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel1 = new JPanel();
Border eBorder = BorderFactory.createEtchedBorder();
panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
gbc.gridx = gbc.gridy = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 70;
add(panel1, gbc); // add compoenet to the COntentPane
JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
gbc.gridy = 1;
gbc.weightx = gbc.weighty = 30;
gbc.insets = new Insets(2, 2, 2, 2);
add(panel2, gbc); // add component to the COntentPane
JPanel panel3 = new JPanel();
panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
gbc.gridx =1;
gbc.gridy = 0;
gbc.gridwidth = /*gbc.gridheight = */1;
gbc.gridheight = 2;
gbc.weightx = /*gbc.weighty = */20;
gbc.insets = new Insets(2, 2, 2, 2);
add(panel3, gbc);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
pack();
setVisible(true); // important
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() { // important
public void run() {
BorderPanels borderPanels = new BorderPanels();
}
});
}
}
One word: MigLayout. As you have seen, it is possible with the standard layout managers. But I can guarantee you, the layout code would be reduced to 4 lines with MigLayout... (Although it would take you some time to get into MigLayout as it works a bit different than the usual layout managers).

How to prevent Boxlayout/Box from stretching children components?

As you can see in the runnable code below, i try to have a Box with expandable child-boxes. The children Boxes can change they size and this all works good. The main problem is that the size is always relative to the parent. But I want them to have a specific size and in case there is no place anymore use the JScrollPane. At the moment they shrink the other children-boxes only.
I tried Glue and Filler, but it didn't work. The glue just had no effect and the filler had the side effect to keep always some place at the (even when the ScrollPane is in action). That is pretty ugly to have there so much free space.
So, do you know a good way to prevent the Boxes from stretching the children?
Thank you in advance!
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
public class ExpandableMenueDemo {
Box allBoxes;
ExpandableMenueDemo(){
allBoxes = Box.createVerticalBox();
TitledBorder title;
title = BorderFactory.createTitledBorder("Filter");
allBoxes.setBorder(title);
for (int i = 0 ;i<3;i++){
//generate collapsable components
SubBox b = new SubBox("SubBox"+i);
allBoxes.add(b.getSwingBox());
}
allBoxes.add(Box.createVerticalGlue());
}
public Container getMenue(){
return allBoxes;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
ExpandableMenueDemo m = new ExpandableMenueDemo();
Box mainBox = Box.createHorizontalBox();
mainBox.add(new JScrollPane(m.getMenue()));
mainBox.add(new JTable(20,5));
frame.setContentPane(mainBox);
frame.pack();
frame.setVisible(true);
}
class SubBox{
Box box;
Box header;
String name;
JButton cBtn;
boolean isCollapsed = true;
JLabel headerLine;
SubBox(String name) {
this.name= name;
box = Box.createVerticalBox();
headerLine = new JLabel(name+" () :");
header= Box.createHorizontalBox();
cBtn = new JButton("v");
cBtn.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
if (isCollapsed)show();
else collapse();
}
});
collapse();
header.add(cBtn);
header.add(Box.createHorizontalStrut(10));
header.add(headerLine);
header.add(Box.createHorizontalGlue());
}
Box getSwingBox() {
Box b = Box.createVerticalBox();
b.add(header);
b.add(box);
return b;
}
public void collapse(){
System.out.println("collapse");
box.removeAll();
this.isCollapsed=true;
cBtn.setText("v");
}
public void show(){
System.out.println("show");
box.removeAll();
this.isCollapsed=false;
cBtn.setText("^");
for (int i = 0; i<3;i++) {
Box b = Box.createHorizontalBox();
b.add(Box.createHorizontalStrut(20));
b.add(new JCheckBox("checkBox "+i));
b.add(Box.createHorizontalGlue());
box.add(b);
}
}
}
}
BoxLayout can accepting setXxxSize,
maybe better example for expanding/collapsing JPanels nests another JComponents
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ExpandingPanels extends MouseAdapter {
private ActionPanel[] aps;
private JPanel[] panels;
public ExpandingPanels() {
assembleActionPanels();
assemblePanels();
}
#Override
public void mousePressed(MouseEvent e) {
ActionPanel ap = (ActionPanel) e.getSource();
if (ap.target.contains(e.getPoint())) {
ap.toggleSelection();
togglePanelVisibility(ap);
}
}
private void togglePanelVisibility(ActionPanel ap) {
int index = getPanelIndex(ap);
if (panels[index].isShowing()) {
panels[index].setVisible(false);
} else {
panels[index].setVisible(true);
}
ap.getParent().validate();
}
private int getPanelIndex(ActionPanel ap) {
for (int j = 0; j < aps.length; j++) {
if (ap == aps[j]) {
return j;
}
}
return -1;
}
private void assembleActionPanels() {
String[] ids = {"level 1", "level 2", "level 3", "level 4"};
aps = new ActionPanel[ids.length];
for (int j = 0; j < aps.length; j++) {
aps[j] = new ActionPanel(ids[j], this);
}
}
private void assemblePanels() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 1, 2, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
JPanel p1 = new JPanel(new GridBagLayout());
gbc.gridwidth = GridBagConstraints.RELATIVE;
p1.add(new JButton("button 1"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
p1.add(new JButton("button 2"), gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
p1.add(new JButton("button 3"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
p1.add(new JButton("button 4"), gbc);
JPanel p2 = new JPanel(new GridBagLayout());
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
p2.add(new JLabel("enter"), gbc);
gbc.anchor = GridBagConstraints.WEST;
p2.add(new JTextField(8), gbc);
gbc.anchor = GridBagConstraints.CENTER;
p2.add(new JButton("button 1"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
p2.add(new JButton("button 2"), gbc);
JPanel p3 = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea(8, 12);
textArea.setLineWrap(true);
p3.add(new JScrollPane(textArea));
JPanel p4 = new JPanel(new GridBagLayout());
addComponents(new JLabel("label 1"), new JTextField(12), p4, gbc);
addComponents(new JLabel("label 2"), new JTextField(16), p4, gbc);
gbc.gridwidth = 2;
gbc.gridy = 2;
p4.add(new JSlider(), gbc);
gbc.gridy++;
JPanel p5 = new JPanel(new GridBagLayout());
p5.add(new JButton("button 1"), gbc);
p5.add(new JButton("button 2"), gbc);
p5.add(new JButton("button 3"), gbc);
p5.add(new JButton("button 4"), gbc);
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
p4.add(p5, gbc);
panels = new JPanel[]{p1, p2, p3, p4};
}
private void addComponents(Component c1, Component c2, Container c, GridBagConstraints gbc) {
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = GridBagConstraints.RELATIVE;
c.add(c1, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
c.add(c2, gbc);
gbc.anchor = GridBagConstraints.CENTER;
}
private JPanel getComponent() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(1, 3, 0, 3);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
for (int j = 0; j < aps.length; j++) {
panel.add(aps[j], gbc);
panel.add(panels[j], gbc);
panels[j].setVisible(false);
}
JLabel padding = new JLabel();
gbc.weighty = 1.0;
panel.add(padding, gbc);
return panel;
}
public static void main(String[] args) {
Runnable doRun = new Runnable() {
#Override
public void run() {
ExpandingPanels test = new ExpandingPanels();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(test.getComponent()));
f.setSize(360, 500);
f.setLocation(200, 100);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(doRun);
}
}
class ActionPanel extends JPanel {
private static final long serialVersionUID = 1L;
private String text;
private Font font;
private boolean selected;
private BufferedImage open, closed;
public Rectangle target;
final int OFFSET = 30,
PAD = 5;
ActionPanel(String text, MouseListener ml) {
this.text = text;
addMouseListener(ml);
font = new Font("sans-serif", Font.PLAIN, 12);
selected = false;
setBackground(new Color(200, 200, 220));
setPreferredSize(new Dimension(200, 20));
setBorder(BorderFactory.createRaisedBevelBorder());
setPreferredSize(new Dimension(200, 20));
createImages();
setRequestFocusEnabled(true);
}
public void toggleSelection() {
selected = !selected;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
if (selected) {
g2.drawImage(open, PAD, 0, this);
} else {
g2.drawImage(closed, PAD, 0, this);
}
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics(text, frc);
float height = lm.getAscent() + lm.getDescent();
float x = OFFSET;
float y = (h + height) / 2 - lm.getDescent();
g2.drawString(text, x, y);
}
private void createImages() {
int w = 20;
int h = getPreferredSize().height;
target = new Rectangle(2, 0, 20, 18);
open = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = open.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(getBackground());
g2.fillRect(0, 0, w, h);
int[] x = {2, w / 2, 18};
int[] y = {4, 15, 4};
Polygon p = new Polygon(x, y, 3);
g2.setPaint(Color.green.brighter());
g2.fill(p);
g2.setPaint(Color.blue.brighter());
g2.draw(p);
g2.dispose();
closed = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
g2 = closed.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(getBackground());
g2.fillRect(0, 0, w, h);
x = new int[]{3, 13, 3};
y = new int[]{4, h / 2, 16};
p = new Polygon(x, y, 3);
g2.setPaint(Color.red);
g2.fill(p);
g2.setPaint(Color.blue.brighter());
g2.draw(p);
g2.dispose();
}
}
Instead of re-inventing the wheel, you might consider to use JXTaskPane/-Container contained in SwingX. Its demo shows it in action (as taskPanes) at the left - that's the part for choosing the demos.

Categories