Expanding a node of a JTree minimizes GridBagLayout - java

I have created a simple Swing application which currently consists of a JToolBar, a JTree and a RSyntaxTextArea, all inside a GridBagLayout.
The JTree has currently only one top node and that has only one child node.
Complete UI with JToolBar, JTree and RSyntaxTextArea
When expanding the top node of the JTree, the whole GridBagLayout kind of "minimizes":
I've googled this phenominum, but since there's no error message or something else in the console, I'm kind of helpless right now.
I'm using the following code to create the UI:
RSyntaxTextArea textArea = new RSyntaxTextArea(50, 150);
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
textArea.setCodeFoldingEnabled(true);
RTextScrollPane sp = new RTextScrollPane(textArea);
cp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
cp.add(createToolbar(), c);
c.gridx = 0;
c.gridy = 1;
c.ipadx = 90;
c.fill = GridBagConstraints.BOTH;
cp.add(createTree(), c);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
cp.add(sp, c);
...
private JToolBar createToolbar() {
JToolBar tb = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton ob = new JButton(new ImageIcon("..."));
tb.add(ob);
tb.setFloatable(false);
tb.setRollover(true);
return tb;
}
...
private JTree createTree() {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Projects");
JTree tree = new JTree(top);
DefaultMutableTreeNode test = new DefaultMutableTreeNode("I'm a test!");
top.add(test);
return tree;
}
Update: A minimal code example to compile on your system for testing purposes:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Tester extends JFrame {
public Tester () {
initializeComponent();
}
private void initializeComponent() {
JPanel cp = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea(50, 150);
JScrollPane sp = new JScrollPane(textArea);
cp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
cp.add(createToolbar(), c);
c.gridx = 0;
c.gridy = 1;
c.ipadx = 90;
c.fill = GridBagConstraints.BOTH;
cp.add(createTree(), c);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
cp.add(sp, c);
setContentPane(cp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
private JTree createTree() {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Projects");
JTree tree = new JTree(top);
DefaultMutableTreeNode test = new DefaultMutableTreeNode("I'm a test!");
top.add(test);
return tree;
}
private JToolBar createToolbar() {
JToolBar tb = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton ob = new JButton("Button");
tb.add(ob);
tb.setFloatable(false);
tb.setRollover(true);
return tb;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Tester().setVisible(true);
}
});
}
}

When expanding the top node of the JTree, the whole GridBagLayout kind of "minimizes":
The GridBagLayout will shrink to the minimum size of a component when there is not enough space to display the entire component.
Swing application which currently consists of a JToolBar, a JTree and a RSyntaxTextArea, all inside a GridBagLayout.
I would just use the default BorderLayout of the frame:
add(toolbar, BorderLayout.PAGE_START);
add(treeScrollPane, BorderLayout.CENTER);
add(textAreaScrollPane, BorderLayout.PAGE_END);
Note how I added the JTree to a JScrollPane. Now scrollbars will appear for the tree when needed.
If you really want to use the GridBagLayout then read the section from the Swing tutorial on How to Use GridBagLayout for an explanation of how to use the various constraints. You may want to start with the "weightx/y" constraints which control which components get space as the frame size is changed. Also, look at the "fill" constraint.

Related

Java JScrollpane not visible

I'm trying to display a series of buttons in a JScrollpane. Reading around, I managed to exit with this code, but nothing is displayed. I do not understand a possible mistake. Thank you for help
As suggested I made some changes, I edited but not works
EDITED
or I'm stupid, or here is some other problem. Here is my complete code with the output image
public class Main extends javax.swing.JFrame {
private final JPanel gridPanel;
public Main() {
initComponents();
// EXISTING PANEL
gridPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
this.Avvio();
}
private void Avvio() {
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));
pane.setLayout(new GridBagLayout());
for (int i = 0; i < 10; i++) {
JButton button;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 2");
c.gridx = 1;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 3");
c.gridx = 2;
c.gridy = i;
pane.add(button, c);
}
gridPanel.add(pane);
gridPanel.revalidate();
gridPanel.repaint();
}
}
Alright, from your comments in another answer:
No problem for compile , simply the Jpanel is empty. The buttons does not appear.
After calling this.Avvio(); you must call:
this.add(scrollPane);
this.pack();
This will produce the following outputs (before and after resizing it):
But there's still no JScrollPanel
This at least solves the first problem, however you have more errors in your code, some of which have already been commented in other answers:
You're extending JFrame, this isn't needed as you can create a JFrame instance / object and use it later. You're never changing the JFrame's behavior and that's why it's not needed to extend it. See Extends JFrame vs. creating it inside the program for more information about this.
You're not calling pack() nor setSize(...) this creates a tiny window, which you need to manually resize. Call pack() recommended before making your JFrame visible. (As suggested at the beginning of this answer).
You're calling .invokeLater() method twice. You need to call it just once, I prefer this way:
SwingUtilities.invokeLater(() -> new Main()); //Note there is no call to .setVisible(true); as per point #1. It should go later in the program like: frame.setVisible(true);
You're calling gridPanel.revalidate(); and gridPanel.repaint() while it doesn't affect your program, it's not needed as your GUI is still not visible, and thus those calls have no effect on your program, you can safely remove them.
You're creating a new GridBagConstraints object on each iteration of the for loop, you can just change its properties inside it and declaring it outside the for loop, which will make your program better.
After following the above recommendations, you can end up with a code like this one:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Main {
private final JPanel gridPanel;
private JFrame frame;
public Main() {
// EXISTING PANEL
gridPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
this.Avvio();
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
private void Avvio() {
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));
pane.setLayout(new GridBagLayout());
for (int i = 0; i < 10; i++) {
JButton button;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 2");
c.gridx = 1;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 3");
c.gridx = 2;
c.gridy = i;
pane.add(button, c);
}
gridPanel.add(pane);
}
public static void main(String args[]) {
/* Create and display the form */
SwingUtilities.invokeLater(() -> {
new Main();
});
}
}
Which still produces this output:
BUT... We still can improve it a little more!
We may have two nested for loops, for the GridBagConstraints properties as well as the generation of the buttons:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class ScrollablePaneWithButtons {
private static final int ROWS = 10;
private static final int COLS = 3;
private JFrame frame;
private JPanel pane;
private JButton[][] buttons;
private GridBagConstraints gbc;
private JScrollPane scroll;
private JButton[] menuButtons;
private JPanel menuPane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ScrollablePaneWithButtons()::createAndShowGui);
}
private void createAndShowGui() {
frame = new JFrame(this.getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new GridBagLayout());
menuPane = new JPanel();
menuPane.setLayout(new GridLayout(1, 3));
buttons = new JButton[ROWS][COLS];
menuButtons = new JButton[] {new JButton("Edit"), new JButton("Delete"), new JButton("Sort Fields")};
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.weightx = 0.5;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
buttons[i][j] = new JButton("Button " + (j + 1));
gbc.gridx = j;
gbc.gridy = i;
pane.add(buttons[i][j], gbc);
}
}
scroll = new JScrollPane(pane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
for (JButton b : menuButtons) {
menuPane.add(b);
}
frame.add(scroll);
frame.add(menuPane, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And this example is (in my opinion) easier to read and follow up. And this is the output the above code is generating:
You can still choose which code to use, either doing the modifications at the first part of this answer, the second one following the recommendations above or the last one which is shorter.
Problems noted:
Avvio - the pane layout was reset during each loop. Set it once before the loop.
Avvio - the pane was added to the grid pane in each loop. Add it once after the loop.
Avvio - the constraints place the buttons in the same grid locations. With the previous two issues fixed, only the last three buttons placed appear.
I'm assuming you want three buttons in a row, so I changed the loop to use the counter as a row counter. The code below will create ten rows of three buttons.
What appears:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
public class Main extends javax.swing.JFrame {
private JPanel gridPanel;
public Main() {
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(600,400);
//EXISTING PANEL
gridPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
this.Avvio();
this.add(borderLayoutPanel, BorderLayout.CENTER);
this.setVisible(true);
}
private void Avvio() {
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));
pane.setLayout(new GridBagLayout());
for (int i = 0; i < 10; i++) {
JButton button;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 2");
c.gridx = 1;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 3");
c.gridx = 2;
c.gridy = i;
pane.add(button, c);
}
gridPanel.add(pane);
gridPanel.revalidate();
gridPanel.repaint();
}
public static void main(String args[]) {
new Main();
}
}
There are several things to do to make it work:
Add a main method
This main method is the entry point. This makes sure the swing-code runs in the AWT-thread. This is what the SwingUtilities.invokeLater is for
Instantiate, pack and display the frame. The size setting is only for experimenting with the scrollpane
Declare the gridPanel as an instance variable
wrap the gridPanel with the scrollPane
Optionally, wrap the scrollPane with the borderLayoutPanel
Invoke the Avvio method because this is the one that adds the buttons
Add the outmost element to the frame
Here is the fixed code:
public class MyFrame extends javax.swing.JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MyFrame frame = new MyFrame();
frame.pack();
frame.setSize(600, 300);
frame.setVisible(true);
});
}
private JPanel gridPanel;
public MyFrame() {
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
gridPanel = new JPanel(new GridLayout(0, 1));
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
this.Avvio();
this.add(borderLayoutPanel, BorderLayout.CENTER);
}
private void Avvio() {...}
}
I have simplified the program and removed all the mistakes
and bad practices. (Missing package, unnecessary panels, calling invokeLater() twice and others.)
Here is a working example:
package com.zetcode;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JavaScrollPaneEx extends JFrame {
public JavaScrollPaneEx() {
initUI();
}
private void initUI() {
JPanel panel = new JPanel(new BorderLayout());
JPanel buttonPanel = createButtonPanel();
JScrollPane scrollPane = new JScrollPane(buttonPanel);
panel.add(scrollPane, BorderLayout.CENTER);
add(panel);
setTitle("Buttons in JScrollBar");
setSize(350, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
c.insets = new Insets(5, 5, 5, 5);
for (int i = 0, j = 0; i < 5; i++) {
JButton btn = new JButton("Button " + (j + 1));
c.weightx = 0.5;
c.gridx = i;
c.gridy = 0;
panel.add(btn, c);
btn = new JButton("Button " + (j + 2));
c.gridx = i;
c.gridy = 1;
panel.add(btn, c);
btn = new JButton("Button " + (j + 3));
c.gridx = i;
c.gridy = 2;
panel.add(btn, c);
j += 3;
}
return panel;
}
public static void main(String args[]) {
EventQueue.invokeLater(() -> {
JavaScrollPaneEx ex = new JavaScrollPaneEx();
ex.setVisible(true);
});
}
}
And this is the screenshot.
And since I consider GridBagLayout to be a very bad
layout manager, I have created a similar example with MigLayout
manager.
We need the following Maven dependency for this example:
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-swing</artifactId>
<version>5.0</version>
</dependency>
The source:
package com.zetcode;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;
public class JavaScrollPaneEx2 extends JFrame {
public JavaScrollPaneEx2() {
initUI();
}
private void initUI() {
JPanel panel = new JPanel(new BorderLayout());
JPanel buttonPanel = createButtonPanel();
JScrollPane scrollPane = new JScrollPane(buttonPanel);
panel.add(scrollPane, BorderLayout.CENTER);
add(panel);
setTitle("Buttons in JScrollBar");
setSize(350, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new MigLayout());
for (int i = 0, j = 0; i < 5; i++) {
JButton btn1 = new JButton("Button " + (j + 1));
JButton btn2 = new JButton("Button " + (j + 2));
JButton btn3 = new JButton("Button " + (j + 3));
JButton btn4 = new JButton("Button " + (j + 4));
JButton btn5 = new JButton("Button " + (j + 5));
panel.add(btn1, "sgx");
panel.add(btn2, "sgx");
panel.add(btn3, "sgx");
panel.add(btn4, "sgx");
panel.add(btn5, "sgx, wrap");
j += 5;
}
return panel;
}
public static void main(String args[]) {
EventQueue.invokeLater(() -> {
JavaScrollPaneEx2 ex = new JavaScrollPaneEx2();
ex.setVisible(true);
});
}
}

GridBagLayout not working inside JPanel *Updated*

First post, and tried to research this as much as possible but finally gave up and decided to ask.
I am building a recipe book program, and on one page it has the list of recipes. When you select a recipe it will appear on the left side as labels. The issue is on my page I have two JPanels. One hosts the list of recipes, the other is labels for displaying the information. On the side that has the labels I cant get them to move, or alter or anything inside the JPanel.
I feel this has a simple solution but I am too inexperienced to see. Anyway thanks for the help in advance.
////Form Panel Class
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class FormPanel extends JPanel {
private JScrollPane pane;
private JPanel pane2;
// viewing of information
private JLabel nameLabel1;
private JLabel nameLabel2;
private JLabel nameLabel3;
public FormPanel() {
nameLabel1 = new JLabel("Label1");
nameLabel2 = new JLabel("Recipe Name: ");
nameLabel3 = new JLabel("Tag Lines: ");
pane = new JScrollPane();
pane2 = new JPanel();
layoutComponents();
pane.setPreferredSize(new Dimension(200, 450));
pane2.setBorder(BorderFactory.createTitledBorder("Recipes"));
pane2.setPreferredSize(new Dimension(375, 450));
}
public void layoutComponents() {
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
///////////////////////////////////////
// First Row
gc.gridy = 0;
gc.weightx = 1;
gc.weighty = 1;
gc.gridx = 1;
gc.anchor = GridBagConstraints.FIRST_LINE_START;
add(pane2, gc);
////////////////////////////////////////////
///////////////////////////////////////
// First Row
gc.gridy = 0;
gc.weightx = 2;
gc.weighty = 1;
gc.gridx = 1;
gc.anchor = GridBagConstraints.FIRST_LINE_END;
gc.insets = new Insets(5, 5, 5, 5);
add(pane, gc);
////////////////////////////////////////////
GridBagConstraints gc2 = new GridBagConstraints();
//
gc2.gridy = 0;
gc2.weightx = 1;
gc2.weighty = 1;
gc2.gridx = 0;
gc2.fill = GridBagConstraints.PAGE_START;
pane2.add(nameLabel2, gc2);
////////////////////////////////////////////
gc2.gridy = 1;
gc2.weightx = 1;
gc2.weighty = 1;
gc2.gridx = 0;
gc2.fill = GridBagConstraints.CENTER;
pane2.add(nameLabel3, gc2);
////////////////////////////////////////////
gc2.gridy = 2;
gc2.weightx = 1;
gc2.weighty = 1;
gc2.gridx = 0;
gc2.fill = GridBagConstraints.SOUTH;
pane2.add(nameLabel1, gc2);
}
}
//Mainframe Class
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Mainframe extends JFrame {
private FormPanel formPanel;
private JTabbedPane tabPane;
public Mainframe() {
super("My Digital Cookbook");
setLayout(new BorderLayout());
formPanel = new FormPanel();
tabPane = new JTabbedPane();
add(tabPane, BorderLayout.PAGE_START);
tabPane.addTab("New Recipe", formPanel);
setMinimumSize(new Dimension(500, 600));
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
//Application Class
import javax.swing.SwingUtilities;
public class Application {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run(){
System.out.println(SwingUtilities.isEventDispatchThread());
new Mainframe();
}
});
}
}
The lower three labels might have wonkey weights and what not just due to trying to get them to move but they all just stay in a straight line near the top of the Jpanel.
Additionally I don't think I needed to re-declare my gridbagconstraints but I decided to try for debugging attempts.
**Update -I now have posted a much simpler version of my program with the error still existing. I apologize that I don't have it smaller due to lack of flexible skills.

Java GridBagLayout - How to position my components gap-less and one by one?

I'm using GridBagLayout to place my GUI components by the following code, wanting the components lay one by one in a column, without any gaps :
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestGUI extends JFrame{
public TestGUI(){
JPanel bigPanel = new JPanel(new GridBagLayout());
JPanel panel_a = new JPanel();
JButton btnA = new JButton("button a");
panel_a.add(btnA);
JPanel panel_b = new JPanel();
JButton btnB = new JButton("button b");
panel_b.add(btnB);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weighty = 1D;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTH;
bigPanel.add(panel_a, c);
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
bigPanel.add(panel_b, c);
this.add(bigPanel);
}
public static void main(String[] args) {
TestGUI gui = new TestGUI();
gui.setVisible(true);
gui.pack();
}
}
I wish the panels will be shown one by one in the column. but now i got this :
As i am going to add some more components in the bigPanel, and required some more customization to the layout, so i need to use GridBagLayout instead of other Layout.
You need to add an extra component so that it will fill the rest of the available space and push the two button-panels to the top. When you will add more components, you can of course remove that component.
Another option (without requiring an extra component) would have been to set weighty=1.0 for the panel_b and anchor=NORTH, but then you would have to change that when you add more components.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestGUI extends JFrame {
public TestGUI() {
JPanel bigPanel = new JPanel(new GridBagLayout());
JPanel panel_a = new JPanel();
JButton btnA = new JButton("button a");
panel_a.add(btnA);
JPanel panel_b = new JPanel();
JButton btnB = new JButton("button b");
panel_b.add(btnB);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
bigPanel.add(panel_a, c);
bigPanel.add(panel_b, c);
c.weighty = 1.0;
// Temporary panel to fill the rest of the bigPanel
bigPanel.add(new JPanel(), c);
this.add(bigPanel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TestGUI gui = new TestGUI();
gui.pack();
gui.setVisible(true);
}
});
}
}

Why is my JPanel inside a JScrollPane not scrolling?

I have a JPanel (yellow) placed in JScrollPane.
When I enter some text in JTextPane, it resizes, but the vertical scrollbar is still not active. yelowPanel.getSize() returns the same value it was before.`(You can see it on redPanel).
So how can I refresh yellowPanel? I want to scroll panel vertically.
I tried to:
panelCreating.revalidate();
panelCreating.invalidate();
panelCreating.repaint();
Works only panelCreating.setPreferredSize(new Dimension(333, 777)); but I don't know what size to set. It depends on content.
There is a small example:
package swingtest;
import java.awt.Color;
import java.awt.Dimension;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class SwingTest extends JFrame {
public SwingTest() {
initComponents();
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new SwingTest().setVisible(true);
}
});
}
private JPanel panelCenter, panelCreating;
private JScrollPane scrollPaneCreating, scrollPaneCenter;
private JTextPane textPane1, textPane2;
private JButton button1;
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(300, 300));
panelCreating = new JPanel();
panelCreating.setMinimumSize(new Dimension(160, 200));
panelCreating.setPreferredSize(new Dimension(160, 200));
scrollPaneCreating = new JScrollPane(panelCreating,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
textPane1 = new JTextPane();
textPane1.setText("a\na");
textPane2 = new JTextPane();
textPane2.setText("b\nb");
button1 = new JButton("+++");
panelCenter = new JPanel();
panelCenter.setBackground(Color.blue);
scrollPaneCenter = new JScrollPane(panelCenter);
// ----------------- Left Panel Init -----------------------
panelCreating.setLayout(new GridBagLayout());
panelCreating.setBackground(Color.ORANGE);
panelCreating.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(0, 0, 4, 4);
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.weightx = c.weighty = 0;
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIVE;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
panelCreating.add(textPane1, c);
button1.addActionListener(new ActionListener() {
int height = 50;
#Override
public void actionPerformed(ActionEvent e) {
textPane1.setText(textPane1.getText() + "\na");
textPane1.setPreferredSize(new Dimension(150, height));
textPane2.setText(textPane2.getText() + "\nb");
textPane2.setPreferredSize(new Dimension(150, height));
height += 30;
panelCreating.revalidate();
panelCreating.repaint();
scrollPaneCreating.revalidate();
}
});
panelCreating.add(button1, c);
panelCreating.add(textPane2, c);
// -------------------------------------------------------
getContentPane().setLayout(new GridBagLayout());
c = new GridBagConstraints();
c.ipadx = c.ipady = 0;
c.insets = new Insets(0, 0, 0, 0);
c.weighty = 0;
c.gridheight = 1;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.weightx = 0;
c.fill = GridBagConstraints.BOTH;
getContentPane().add(scrollPaneCreating, c);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
getContentPane().add(scrollPaneCenter, c);
}
}
Yellow panel also uses GridBagLayout.
Sorry for my English
Instead of setting a preferred size on panelCreating, set it on scrollPaneCreating. And don't set a preferred size on the text components, they will grow as you add new lines of text to them. The idea is to have the panel inside the scroll pane grow as large as it needs to, and just restrict the size of the scroll pane itself.
// [...]
panelCreating = new JPanel();
//panelCreating.setMinimumSize(new Dimension(160, 200));
//panelCreating.setPreferredSize(new Dimension(160, 200));
scrollPaneCreating = new JScrollPane(panelCreating,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPaneCreating.setMinimumSize(new Dimension(160, 200));
scrollPaneCreating.setPreferredSize(new Dimension(160, 200));
// [...]
#Override
public void actionPerformed(ActionEvent e) {
textPane1.setText(textPane1.getText() + "\na");
//textPane1.setPreferredSize(new Dimension(150, height));
textPane2.setText(textPane2.getText() + "\nb");
//textPane2.setPreferredSize(new Dimension(150, height));
//height += 30;
// This isn't necessary either
//panelCreating.revalidate();
//panelCreating.repaint();
//scrollPaneCreating.revalidate();
}
Edited to add: another alternative is to set sizes on the JViewport that is attached to the scroll pane. The viewport is where the content is displayed. You can sort of think of the scroll pane as being composed of the viewport plus scrollbars. If the scroll pane is set to a fixed size, then the viewport size is determined by subtracting the scroll bar size. But if the viewport is set to a fixed size, then the scroll pane size is determined by adding the scroll bar size to the viewport size. Setting a fixed size on the viewport is preferable if you want to precisely control how much content should be displayed on screen, because scroll bar sizes can vary by operating system.
scrollPaneCreating.getViewport().setMinimumSize(new Dimension(160, 200));
scrollPaneCreating.getViewport().setPreferredSize(new Dimension(160, 200));
The default layout of JPanel is FlowLayout. Try GridLayout instead. There's a related example here. For example,
panelCreating = new JPanel(new GridLayout());
scrollPaneCreating = new JScrollPane(panelCreating);
Addendum: Also consider nested layouts. The example below uses BoxLayout for the left panel.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
/** #see https://stackoverflow.com/questions/9184476 */
public class SwingTest extends JFrame {
private static final int N = 8;
public SwingTest() {
initComponents();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new SwingTest().setVisible(true);
}
});
}
private JPanel panelCenter, panelCreating;
private JScrollPane scrollPaneCreating, scrollPaneCenter;
private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelCreating = new JPanel();
scrollPaneCreating = new JScrollPane(panelCreating,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panelCenter = new JPanel();
panelCenter.setBackground(Color.blue);
scrollPaneCenter = new JScrollPane(panelCenter);
// ----------------- Left Panel Init -----------------------
panelCreating.setLayout(new BoxLayout(panelCreating, BoxLayout.Y_AXIS));
panelCreating.setBackground(Color.orange);
panelCreating.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
panelCreating.add(createTextPane());
panelCreating.add(Box.createVerticalStrut(N));
panelCreating.add(createTextPane());
panelCreating.add(Box.createVerticalStrut(N));
panelCreating.add(createTextPane());
// -------------------------------------------------------
setLayout(new GridLayout(1, 0));
add(scrollPaneCreating);
add(scrollPaneCenter);
pack();
}
private JTextPane createTextPane() {
JTextPane pane = new JTextPane();
pane.setText(""
+ "Twas brillig and the slithy toves\n"
+ "Did gyre and gimble in the wabe;\n"
+ "All mimsy were the borogoves,\n"
+ "And the mome raths outgrabe.");
pane.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
return pane;
}
}
I fixed your problem adding 1 line.
panelCreating.setPreferredSize(new Dimension((int) panelCreating.getPreferredSize().getWidth(),
(int)(panelCreating.getPreferredSize().getHeight()+30)));
The line must be inserted after the following lines;
#Override
public void actionPerformed(ActionEvent e) {
textPane1.setText(textPane1.getText() + "\na");
textPane1.setPreferredSize(new Dimension(150, height));
textPane2.setText(textPane2.getText() + "\nb");
textPane2.setPreferredSize(new Dimension(150, height));
height += 30;
Explanation:
The grid bag layout do not set it's pane preferred size when you set the preferred size of the textPanes inside it, but the scrollPane only scrolls based on the preferred size of the pane in it. So you must set the new preferred size of the pane every time you change the size of the components in it, then the scrollPane will know exactly what he must do. That's what i did, a add a line that increased the preferred size of creatingPanel, wich is the one inside the scrollPanel

Customizing FocusTraversalPolicy for JFrame with GridBagLayout

I have a JFrame with four components: three JPanels and a JTabbedPane. Two panels have selectable components and are located approximately in BorderLayout.NORTH and EAST, while the JTabbedPane sits approximately in CENTER. The tabs of the TabbedPane are aligned on the bottom of the pane. I say approximately because the actual layout is done with a GridBagLayout (sorry!).
Currently, the focus traversal cycle goes like this:
Current Cycle:
1) North JPanel
2) East JPanel
3) JTabbedPane selected tab
4) JTabbedPane selected tab content
However, I'd like it to be:
Desired Cycle:
1) North JPanel
2) JTabbedPane selected tab content
3) JTabbedPane selected tab
4) East JPanel
I have tried the Vector-based FocusTraversalPolicy given by the Java Focus Guide with no luck: I couldn't get the cursor to go down into the tab panel contents.
I then looked into extending/changing the other FocusTraversalPolicy classes out there, but got nowhere with that either.
I'm fairly sure the issue is the current FocusTraversalPolicy uses component location on screen to determine the layout cycle. Technically the side panel is higher on the screen than the center tabbed pane. I'd really like to change this though. Anybody else run into this/have any insight? Thanks!!
EDIT:
Here's an SSCCE with proper layout code demonstrating the problem. Just a note: the issue is NOT the layout, as that has to stay the same.
package SO;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class ssccee7729709{
JPanel north;
JPanel north2;
JPanel east;
JTabbedPane center;
JFrame content;
void initialize() {
north = new JPanel(new FlowLayout()) {
{
this.setBackground(Color.CYAN);
}
};
north.add(new JLabel("Title panel"));
north2 = new JPanel(new FlowLayout()) {
{
this.setBackground(Color.RED);
}
};
north2.add(new JLabel("north2 Panel"));
north2.add(new JTextField(4));
north2.add(new JTextField(4));
east = new JPanel(new GridLayout(3,1)) {
{
this.setBackground(Color.BLUE);
}
};
east.add(new JButton("b1"));
east.add(new JButton("b2"));
center = new JTabbedPane(JTabbedPane.BOTTOM, JTabbedPane.WRAP_TAB_LAYOUT);
for (int i = 0; i < 2; i++) {
JPanel panel = new JPanel(new GridLayout(4,1));
panel.add(new JLabel("Panel " + i));
panel.add(new JTextField(6));
panel.add(new JTextField(6));
panel.add(new JTextField(6));
center.addTab("Tab " + i, panel);
}
content = new JFrame();
content.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
content.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
{
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 4;
gbc.weightx = 1;
content.add(north, gbc);
}
{
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.gridwidth = 1;
gbc.weightx = 1;
// gbc.weighty = .1;
gbc.gridy = 1;
gbc.gridx = 1;
content.add(north2, gbc);
}
{
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbc.gridheight = GridBagConstraints.RELATIVE;
gbc.weighty = 1;
gbc.gridy = 2;
content.add(center, gbc);
}
{
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.VERTICAL;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridx = 3;
gbc.gridheight = 4;
content.add(east, gbc);
}
content.setVisible(true);
content.pack();
}
public static void main(String args[]) {
ssccee7729709 so = new ssccee7729709();
so.initialize();
}
}
If you look at the source for LayoutFocusTraversalPolicy, which is the default policy, it pretty much just extends SortingFocusTraversalPolicy, which takes a Comparator<? extends Component>.
You should be able to provide your own Comparator which is an inner class and so has access to parent components, and provide appropriate comparison values by looking at themselves and their parents.
It probably wouldn't hurt to check out the source to LayoutComparator which is what LayoutFocusTraveralPolicy uses.
After cloning LayoutComparator, I had to add the following lines to the compare method:
...previous code...
int zOrder = a.getParent().getComponentZOrder(a) - b.getParent().getComponentZOrder(b);
/// added these:
if (a instanceof EastPanel) {
if (b instanceof JTabbedPane || b instanceof NorthPanel) {
return -1;
}
}
if (b instanceof EastPanel) {
if (a instanceof JTabbedPane || a instanceof NorthPanel) {
return 1;
}
}
if (horizontal) {
if (leftToRight) {
...more code...
Honestly I don't know how this works...the mechanism of using compare to order components is strange to me...but this works, so... hooray magic!

Categories