Java resizing individual components of JPanel - java

I have a JPanel with Box.createVerticalBox() layout containing five JPanels. (1) Labels, (2) a table (3) a JTextField (4) a JTextArea (5) buttons.On resize:
labels should stick to top left corner and keep the same size,
JTextField should stick to left size between (2) and (4) and expand to full width of the frame
Buttons should stick to bottom right corner and keep the same size,
JTable and JTextArea should expand to full width of the frame and equally divide remaining space
I've tried several layouts, but couldn't make resizing work.
To run this program two classes are required EditPanel.java :
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class EditPanel extends JPanel {
private JPanel p1Labels;
private JPanel p2Table;
private JPanel p3ecnTitle;
private JPanel p5Buttons;
private JTextField fieldK;
private JTextField fieldS;
private JScrollPane myScrollBar;
private Box theBox;
public EditPanel() {
init();
}
public void init() { // Creating a vertical Box layout with five sections, placing jpanels there
//First panel with buttons
p1Labels = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
fieldK = new JTextField("Animal");
fieldS = new JTextField("Fox");
p1Labels.add(new JLabel("Kindom: "));
p1Labels.add(fieldK);
p1Labels.add(new JLabel("Species: "));
p1Labels.add(fieldS);
//Second panel with a table
p2Table = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
String[] columnNames = {"First", "Second", "Third", "Fourth"};
Object[][] data = {{"11", "12", "13", "Forteen"},{"21", "22", "23", "Twenty four"}};
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane1 = new JScrollPane(new JTable(data, columnNames));
table.setFillsViewportHeight(true);
p2Table.add(scrollPane1, BorderLayout.CENTER);
//Third panel with a JTextField
p3ecnTitle = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
p3ecnTitle.add(new JLabel("Title: "));
p3ecnTitle.add(new JTextField("", 14));
//Forth panel with JTextArea
//p4TextArea = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));//tried this too
JTextArea ecnArea = new JTextArea(10, 20);
ecnArea.setText("");
ecnArea.setName("Note");
ecnArea.setLineWrap(true);
ecnArea.setWrapStyleWord(true);
myScrollBar = new JScrollPane(ecnArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Fifth container with buttons
p5Buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 2));
p5Buttons.add(new JButton("SAVE"));
p5Buttons.add(new JButton("DELETE"));
p5Buttons.add(new JButton("CANCEL"));
//Placing everything in a container
theBox = Box.createVerticalBox();
theBox.add(p1Labels);
theBox.add(p2Table);
theBox.add(p3ecnTitle);
//theBox.add(p4TextArea);
theBox.add(myScrollBar);
theBox.add(Box.createVerticalGlue());
theBox.add(p5Buttons);
this.add(theBox);
}
}
And the main.java
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame myFrame;
myPanel EditECNDialog;
myFrame = new JFrame();
EditECNDialog = new myPanel();
myFrame.setTitle("Notes");
myFrame.add(EditECNDialog);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}
Which Layout handles resizing the best? Can boxlayout handle resizing?

GridBagLayout is the best layout manager for your app. See https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

You can use a GridBagLayout as Wabbi has already suggested. However is does have the limitation I noted in the comments.
However, if you truly want the textarea and table to always be the same size then you can use the Relative Layout. This layout will first allocate space to components with a fixed size. Then any space remaining is allocated to the components with a relative constraint.
So the basic code would be:
RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true );
JPanel panel = new JPanel( rl );
panel.add(labelPanel);
panel.add(tableScrollPane, new Float(1));
panel.add(textField);
panel.add(textAreaScrollPane, new Float(1));
panel.add(buttonsPanel);
Now the table and text area will grow/shrink equally as the frame is resized.
Can boxlayout handle resizing?
Yes it can handle this type of resizing. Box layout respects the maximum size of a component. So if you override the getMaximumSize() method of your panels to return getPreferredSize(), then the panels will not grow in height.
So extra space will be given to the scrollpanes of the text area and table. Again, same concern. Each component will originally be allocated space based on its preferred size.

You should use one variable for gridbagconstraints. This way you can do c.gridy++; c.gridx=0; and c.gridx++;
It will be easier to insert new components later.
Explicit d.gridy=4; makes inserting new components difficult.

Related

How to set width of specific panel with BoxLayout Manager

The code below places 3 JPanels inside a JFrame. I want the blue colored panel to have a width of 300 (assume the enclosing Frame has a width of greater than 300). The width of the other two panels should be the remainder. How do I do that?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PanelTest extends JFrame {
private JPanel leftpanel;
public PanelTest() {
getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
this.leftpanel = new JPanel() {
#Override
public Dimension getMaximumSize() {
return new Dimension(300, 9999);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(300, 0);
}
};
this.leftpanel.setBackground(Color.blue);
getContentPane().add(leftpanel);
JPanel rightpanel = new JPanel();
getContentPane().add(rightpanel);
rightpanel.setLayout(new BoxLayout(rightpanel, BoxLayout.Y_AXIS));
JPanel upperpanel = new JPanel();
upperpanel.setBackground(Color.red);
rightpanel.add(upperpanel);
JPanel lowerpanel = new JPanel();
lowerpanel.setBackground(Color.yellow);
rightpanel.add(lowerpanel);
pack();
setVisible(true);
setExtendedState(Frame.MAXIMIZED_BOTH);
}
public static void main(String[] args) {
new PanelTest();
}
}
I slightly updated the above code using #camickr's suggestion of using BorderLayout. The width is now as desired. Thanks #camickr.
I still think BoxLayout should have respected the minimum and maximum sizes. Taking those into consideration, it should have set the width to 500.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PanelTest extends JFrame {
public PanelTest() {
getContentPane().setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel leftpanel = new JPanel();
int width = 500;
leftpanel.setPreferredSize(new Dimension(width, 1));
leftpanel.setBackground(Color.blue);
getContentPane().add(leftpanel, BorderLayout.LINE_START);
JPanel rightpanel = new JPanel();
getContentPane().add(rightpanel, BorderLayout.CENTER);
rightpanel.setLayout(new BoxLayout(rightpanel, BoxLayout.Y_AXIS));
JPanel upperpanel = new JPanel();
upperpanel.setBackground(Color.red);
rightpanel.add(upperpanel);
JPanel lowerpanel = new JPanel();
lowerpanel.setBackground(Color.yellow);
rightpanel.add(lowerpanel);
pack();
setVisible(true);
setExtendedState(Frame.MAXIMIZED_BOTH);
}
public static void main(String[] args) {
new PanelTest();
}
}
The most important size of a component is the preferred size. Most layout managers will use this size first and then maybe use the minimum/maximum sizes depending on the space available.
If you don't specify a preferred size of the panel it will be (10, 10) since this is the default size of a panel using the FlowLayout when no components are added.
This size is outside the bounds of your minimum/maximum values so it appears the BoxLayout will then allocate space to each component in a ratio based on the maximum size of each panel. The blue panel has a size of 300 and the other panel has a size of Integer.MAX_VALUE so much more space gets allocated to the other panel.
One solution is to add:
leftpanel.setPreferredSize(new Dimension(300, 1));
I want the blue colored panel to have a width of 300
I would not change the content pane to use a BoxLayout and instead just use the default BorderLayout of the frame. Then you:
add the blue panel to the BorderLayout.LINE_START.
Add the panel containing the red/yellow components to the BorderLayout.CENTER.
The BorderLayout will respect the width of the blue panel and give the remaining space to the component in the center.
Using this approach there is no need to override the minimum/maximum values of the blue panel.
Of course you would still need to set the preferred size of the blue panel.

Jscrollpane center aligns when label is too small

This question is very similiar to this: JScrollPane doesn't top align when there is more than enough space to show the content I tried this solution, but it does not work.
When I add a jlabel to jscrollpane, when the jlabel is small, the label becomes centered. It works normally when the scrollbar shows. Setting boxlayout does not change anything. I feel like this isn't working properly because I'm setting a perferred size to the panel? But if I remove the line panel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 50), screenSize.height-150)); The label becomes small when there is no text, and grows to accomdate text, which I don't want. If I add the panel instead of the label, it makes the screen scrollable even though there isn't text?
This is my code:
public class Test {
// JFrame
static JFrame frame = new JFrame("Test");
//panel 1
static JPanel panel = new JPanel(new BorderLayout());
// label to display text
static JLabel label = new JLabel();
//scroll panel in main method
public static void main(String[] args) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
label.setBorder(new EmptyBorder(5, 5, 5, 20));
label.setText("any text makes it centered beyond 40 lines");
//create panel
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(label, BorderLayout.NORTH);
panel.add(Box.createVerticalGlue());
panel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 50), screenSize.height-150));
panel.setBorder(new EmptyBorder(5, 5, 5, 10));
JScrollPane jspanel = new JScrollPane(label, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//jspanel.add(label, BorderLayout.CENTER);
jspanel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 70), screenSize.height-180));
jspanel.getVerticalScrollBar().setUnitIncrement(20);
jspanel.setBorder(BorderFactory.createEmptyBorder());
jspanel.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
jspanel.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
//panel.setPreferredSize(new Dimension(640, 480));
//frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(jspanel);
frame.setSize((int)(screenSize.width*0.7), screenSize.height - 50);
frame.revalidate();
frame.pack();
frame.setVisible(true);
}
}
jspanel.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
jspanel.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
That will align the scrollpane in its parent container, depending on the layout manager being used. It does not affect the alignment of any component added to the scrollpane. It is not needed.
the label becomes centered
The label is sized to fill the entire space available, so you need to customize how the text of the label is painted.
If you don't want it centered then you can place it at the top using:
label.setVerticalAlignment( SwingConstants.TOP );
After reworking your code, I came up with the following GUI.
I added a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
I eliminated all static references, except for the main method.
I reworked your code into methods so I could focus on one part of the GUI at a time.
Here's the complete runnable example. This is a minimal reproducible example.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class JScrollPaneTestGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JScrollPaneTestGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("JScrollPane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane jspanel = createJScrollPane();
frame.add(jspanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private JScrollPane createJScrollPane() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(Color.YELLOW);
panel.setPreferredSize(new Dimension(400, 300));
JLabel label = new JLabel();
label.setBorder(new EmptyBorder(5, 5, 5, 20));
label.setText("any text makes it centered beyond 40 lines");
//create panel
panel.add(label, BorderLayout.NORTH);
JScrollPane jspanel = new JScrollPane(panel);
return jspanel;
}
}

Make Java JScrollpane scroll vertically instead of horizontally

I am new to Java and currently trying to learn it. I am working on a simple program to display a number of buttons on a frame. I also want to make the panel scroll vertically but instead it scrolls horizontally.
Here is my code so far:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class GridView {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("Display Buttons");
frame.setBounds(30, 30, 300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(3, 4, 30, 20);
Container content = frame.getContentPane();
content.setLayout(grid);
JPanel panel = new JPanel();
JButton button = null;
for (int i = 1; i <= 100; i++) {
panel.add(button = new JButton(" Press " + i));
}
content.add(panel);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(panel), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
Can anyone tell me why my scroll is shown horizontally and how to fix it? Any kind of response will be much appreciated.
EDIT:
I am sorry. My question was incomplete. I want to make it something like this, but with vertical scroll.
//GridLayout grid = new GridLayout(3, 4, 30, 20);
GridLayout grid = new GridLayout(0, 4, 30, 20);
If you want columns of component then don't specify the rows value in the layout manager. Just specify the columns and the components will wrap when required
//content.setLayout(grid);
//JPanel panel = new JPanel();
JPanel panel = new JPanel(grid);
You add the buttons to the panel so you need to set the layout manager of the panel. Otherwise the JPanel will use the default FlowLayout, which display all the buttons on a single row.
It shows horizontally because the buttons are added horizontally so the width of the panel exceeds the view port.
If you added the buttons vertically, by using a different layout manager, the scroll pane would show vertically. For example:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); //or setLayout(grid); if you meant to use tge gridlayout for the buttons
/*JButton button = null;*/ //never used
for (int i = 1; i <= 100; i++) {
panel.add(new JButton(" Press " + i));
}
Edit to answer the edited question. See comments:
public static void main(String[] args) {
JFrame frame = new JFrame("Display Buttons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(0, 4, 30, 20);
JPanel panel = new JPanel(grid);
for (int i = 1; i <= 100; i++) {
panel.add(new JButton(" Press " + i));
}
JScrollPane sp = new JScrollPane(panel);
//by default scrollpane will appear as needed, vertically AND horizontally
//to prevent it from showing horizontally :
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(sp, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}

Trying to add a scrollbar to a JPanel with GridLayout, but the JCheckBoxes just get made smaller instead

So I'm trying to set up a Gui in Java which holds a list of checkboxes. What determines the length of the list is the highlighted checkboxes. However, when I add more things to the list the checkboxes just get smaller to fit the panel. I've added a vertical scrollbar, but this just doesn't do anything. Is there something I have to do to stop the GridLayout from resizing what it holds or is it the wrong layout?
package darrt;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestForScrollBat {
public static void main(String[] args){
new TestForScrollBat();
}
public TestForScrollBat(){
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(0, 1));
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(50, 30, 300, 50);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JLabel label = new JLabel(" Soc Categories");
JCheckBox soc1 = new JCheckBox("Blood and Lymphatic System Disorder");
JCheckBox soc2 = new JCheckBox("Cardiac Disorders");
JCheckBox soc3 = new JCheckBox("Congenital, familial and Genetic Disorders");
JButton jbtn = new JButton("Go!");
panel.add(label);
panel.add(soc1);
panel.add(soc2);
panel.add(soc3);
panel.add(jbtn);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
I had it before so that it would add a scroll to this panel, but now it doesn't even do that.. It just creates a new JPanel on the JFrame
Your problem is about the following lines in your code:
scrollPane.setBounds(50, 30, 300, 50);
You should not set static sizes and locations when using layouts. You are telling a specific size and location to the scrollPane while you had add it to the center of the contentPane before. These two are in conflict.
And next problem is about this line:
frame.add(panel);
This line will detach the panel from you JScrollPane and add it directly to the contentPane of the JFrame.
By deleting/commenting these lines, your problem will be solved.

How to add many components to a JFrame in a format

I have a JPanel, 4 ComboBoxes, and a button. I want to have a 700 x 500 JFrame, with the panel taking up the left 500 x 500. The right side I want, vertically, 2 combo boxes, another 2 combo boxes, and then the button. Hopefully this makes sense: I just want to have them all visible and I want the boxes paired in groups of 2. Example code of what I've tried is here:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
public class Test extends JFrame{
public Test () {
super();
//setLayout(new FlowLayout());
JPanel canvas = new JPanel();
canvas.setBackground(Color.red);
canvas.setSize(500, 500);
JComboBox field1 = new JComboBox();
JComboBox field2 = new JComboBox();
JComboBox field3 = new JComboBox();
JComboBox field4 = new JComboBox();
JButton button = new JButton();
JPanel info = new JPanel();
info.setBackground(Color.blue);
info.add(field1, BorderLayout.NORTH);
info.add(field2, BorderLayout.EAST);
info.add(field3, BorderLayout.CENTER);
info.add(field4, BorderLayout.WEST);
info.add(button, BorderLayout.SOUTH);
add(info, BorderLayout.EAST);
add(canvas, BorderLayout.WEST);
setTitle("TEST");
setSize(700, 500);
}
public static void main (String[] args) {
JFrame testFrame = new Test();
testFrame.setVisible(true);
}
}
Any help or suggestions about how to go about laying this out would be great.
If you want your combo buttons and the button vertically stacked on top of each other, I would use a grid layout instead of a border layout. Just make the border layout have 1 column and 5 rows.

Categories

Resources