Why setPreferredSize does not change the size of the button? - java

Here is the code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TestGrid {
public static void main(String[] args) {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 9));
panel.setMaximumSize(new Dimension(9*30-20,4*30));
JButton btn;
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(30, 30));
panel.add(btn);
}
btn = new JButton();
btn.setPreferredSize(new Dimension(30, 10));
panel.add(btn);
for (int j=1; j<=4; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(30, 30));
panel.add(btn);
}
}
mainPanel.add(panel);
frame.add(mainPanel);
frame.setSize(450,950);
frame.setVisible(true);
}
}
I suppose to have a table of buttons with 4 rows and 9 columns. And the middle column should be narrower that other columns. I tried Dimension(30, 10) and Dimension(30, 10) both have no effect on the width of the middle column. Why?

Layout managers are free to ignore the preferred size. Specifically, GridLayout will always make each cell in the grid exactly the same size (it's a pretty useless layout manager for that reason).
You'll have to use a different layout manager, such as nested BoxLayout or a GroupLayout.

GridLayout is quite inflexible in that each and every cell is the same size, typically honoring the largest height and width settings of any object added to the grid.
If the rows and/or columns need to have varying sizes you should use GridBagLayout.

setPreferredSize will not change the size of the button until dimension is set by using Dimension.
Example:-
Dimension dim = new Dimension(20,20), then use setPerferredSize(dim).

Related

Updating component's height also changes it's width

I have a GUI with one main JPanel and inside of it multiple rows, each row being another JPanel. Every row (of type JPanel) consists of 4 smaller JPanels and every panel out of those 4 has a component inside of it. The end result is a grid like interface.
Main panel has a BoxLayout and panels that are parts of a row have FlowLayout.
When I update height of some component (from row) using some listener, entire row becomes taller, which works as expected. But what happens is that not only height is changed, but also width of components (inside a row) is changed. I understand that BoxLayout is trying to layout the components using maxSize and minSize that I can set to be the same value and that worked, but then when I resize the window, other rows expand and the row with same minSize and maxSize doesn't and the grid structure becomes messed up.
What I want to achieve, is that I update only the height of the row. And when I resize the window, entire row expands, and the structure of grid is still the grid. Here is the Short, Self Contained, Correct (Compilable), Example:
Main class:
public class Main {
public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
new MainFrame(450,150);
});
}
}
MainFrame class:
public class MainFrame extends JFrame{
public MainFrame(int width, int height) {
super("Title");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
setVisible(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(mainPanel);
add(scrollPane);
for(int i=0; i<50; i++) {
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
panel1.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.black));
panel1.setPreferredSize(new Dimension(70,35));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
panel2.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.black));
panel2.setPreferredSize(new Dimension(70,35));
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
panel3.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.black));
panel3.setPreferredSize(new Dimension(70,35));
JTextArea area1 = new JTextArea("hello " + i);
area1.setPreferredSize(new Dimension(70,25));
panel1.add(area1);
JTextArea area2 = new JTextArea("hello " + i);
area2.setPreferredSize(new Dimension(70,25));
panel2.add(area2);
JTextArea area3 = new JTextArea("hello " + i);
area3.setPreferredSize(new Dimension(70,25));
panel3.add(area3);
JPanel row = new JPanel();
row.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.black));
row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
row.add(panel1);
row.add(panel2);
row.add(panel3);
JButton button = new JButton("Click me");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.black));
buttonPanel.setPreferredSize(new Dimension(70,35));
buttonPanel.add(button);
button.addActionListener(event -> {
panel1.setPreferredSize(new Dimension(panel1.getWidth(), panel1.getHeight() + 30));
area1.setPreferredSize(new Dimension(area1.getWidth(), area1.getHeight() + 30));
area1.updateUI();
});
row.add(buttonPanel);
mainPanel.add(row);
}
}
}
If you run this code and press button it will update not only row's height, but also row's width and grid is not aligned well anymore.
You are setting the "preferred size" based on the "size" of the component. The two can be different.
Your code should be something like:
//panel1.setPreferredSize(new Dimension(panel1.getWidth(), panel1.getHeight() + 30));
Dimension d = panel1.getPreferredSize();
panel1.setPreferredSize(new Dimension(d.width, d.height + 30));
Also, you should not be using updateUI(). That is a method used internally by Swing on a LAF change.
Instead, when you want to invoke the layout manager you invoke revalidate() on the top level component that was changed:
//area1.updateUI();
panel1.revalidate();

Button creating another column in Panel

The button on the Panel was supposed to be below the last row of ovals but what it does was add a column:
Here is my Code
Panel p1 = new Panel();
JButton shiftLeft = new JButton("Shift Left");
JButton shiftRight = new JButton("Shift Right");
p1.setLayout(new GridLayout(Rows, Columns));
for (int i=0; i<Rows; i++) {
for (int j = 0; j < Columns; j++) {
arcs[i][j] = new ArcsPanel(i, j);
p1.add(arcs[i][j]);
arcs[i][j].addMouseListener(me);
}
}
p1.add(shiftRight);
add(p1, BorderLayout.CENTER);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
The button on the Panel was supposed to be below the last row of ovals
A GridLayout always adds components in rows/columns.
If you want the button separate from the GridLayout then you need to add the button to the frame directly:
//p1.add(shiftRight);
add(p1, BorderLayout.CENTER);
add(shiftRight, BorderLayout.PAGE_END);
Or if you don't want the button resized, then you need to wrap it in another panel first:
JPanel buttonPanel = new JPanel();
buttonPanel.add( shiftRight );
add(buttonPanel, BorderLayout.PAGE_END);
The point is to achieve desired layout you can use multiple panels each with different layouts. You are not force to use a single panel or layout manager.

JScrollPane is not Working in JPanel

I have to use JScrollPane in my Project but it is not working.
I have pasted my code where I use a JSCrollPane in my main JPanel.
frame = new JFrame();
frame.setBounds(100, 100, 1179, 733);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane_1.setBounds(0, 0, 1163, 694);
frame.getContentPane().add(scrollPane_1);
JPanel panel = new JPanel();
scrollPane_1.setViewportView(panel);
panel.setLayout(null);
Setting the layout to Null means you need to handle the placement manually --> Specify the pixel location and handle the size of the container.
A layout manager handles this placement for you. The manager calculates its preferred size based on its content. The ScrollPane uses this calculated size from the layout manager.
This means you should use a layout manager, place your components within it. The rest should work automatically.
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500, 500);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(30, 15));
for (int row = 0; row < 30; row++) {
for (int col = 0; col < 15; col++) {
panel.add(new Button("Button" + row + "/" + col));
}
}
frame.getContentPane().add(new JScrollPane(panel));
frame.setVisible(true);
}
I am not sure which layout you are using, but you need to set your panel layout something like this
panel.setLayout(new FormLayout(
"default:grow",
"fill:default:grow"));

GridBagLayout erratic resizing and displaying

I'm making a program that uses a GridBagLayout in a container. My primary use for this is to have two JPanels, which occupy 75% and 25% of the horizontal space of the window. For some reason though, the two panels look more like 90/10, and when resizing, the smaller one rapidly changes in size, between it's apparent minimum size, and what I believe is the desired 25%.
Here is the relevant code.
frmReedreadV = new JFrame();
frmReedreadV.setBounds(x, y, width, height);
frmReedreadV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmReedreadV.getContentPane().setLayout(new BoxLayout(frmReedreadV.getContentPane(), BoxLayout.Y_AXIS));
JPanel stretchyPanel = new JPanel();
frmReedreadV.getContentPane().add(stretchyPanel);
stretchyPanel.setLayout(new CardLayout(0, 0));
JPanel textAndUsers = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
textArea = new JTextArea();
textArea.setMargin(new Insets(2, 5, 5, 2));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
gbc.weightx = 0.8;
textAndUsers.add(scrollPane, gbc);
list = new FriendsList(listUpdate);
gbc.weightx = 0.2;
textAndUsers.add(list.frmUserList, gbc);
stretchyPanel.add(textAndUsers);
FriendsList is a JList contained in a JPanel.
There are other buttons and text fields in the main CardLayout content pane, but those shouldn't affect what is inside of this GridBagLayout, correct?
I made another copy of this JPanel as a standalone application, and it displays and resizes perfectly. See here:
JFrame frame = new JFrame();
frame.getContentPane().setLayout((new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 550, 600);
JPanel stretchyPane = new JPanel();
frame.getContentPane().add(stretchyPane);
stretchyPane.setLayout(new CardLayout(0, 0));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextArea text = new JTextArea();
text.setMargin(new Insets(2, 5, 5, 2));
JScrollPane panel1 = new JScrollPane(text);
FriendsList panel2 = new FriendsList(new Object());
c.fill = GridBagConstraints.BOTH;
c.weightx = .8;
c.weighty = 1;
panel.add(panel1, c);
c.weightx = .2;
//c.fill = GridBagConstraints.HORIZONTAL;
panel.add(panel2.frmUserList, c);
stretchyPane.add(panel);
frame.setVisible(true);
What could be causing the difference between the two, since I've replicated my original line by line into the copy?
The weightx and weighty properties might appear to act as proportional sizes, but that is not what they do. In fact they determine the distribution of extra space in the layout.
If you set everything to its preferred size by calling pack() on your JFrame, there will be no extra space. Which means the weightx and weighty properties have no effect while it's in that state.
Once the user starts resizing the window to be larger, there will be extra space, and only then will GridBagLayout consult the weightx and weighty properties to determine how to apportion that extra space to each column and row. Until then, it's entirely possible for a component with a small weightx to be wider than a component with a larger weightx, if their preferred sizes dictate it.
Hopefully this simple program will demonstrate this concept. Try using the mouse (or keyboard) to resize the window to be wider, and observe how each of the textfields grows:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridBagProportions {
static void buildAndShowWindow() {
JTextField small = new JTextField("small (0.8)", 5);
JTextField large = new JTextField("LARGE (0.2)", 30);
small.setMinimumSize(small.getPreferredSize());
large.setMinimumSize(large.getPreferredSize());
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets.left = 6;
gbc.insets.top = 6;
gbc.insets.bottom = 6;
gbc.weightx = 0.8;
panel.add(small, gbc);
gbc.weightx = 0.2;
gbc.insets.right = 6;
panel.add(large, gbc);
JFrame frame = new JFrame("GridBagLayout Proportions");
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
buildAndShowWindow();
}
});
}
}
So what can be done about it? Well, this is one layout scenario that GridBagLayout cannot do. I would try using a SpringLayout:
SpringLayout layout = new SpringLayout();
JPanel textAndUsers = new JPanel(layout);
SpringLayout.Constraints scrollPaneConstraints =
new SpringLayout.Constraints(scrollPane);
Spring scrollPaneWidth = scrollPaneConstraints.getWidth();
SpringLayout.Constraints listConstraints =
new SpringLayout.Constraints(scrollPaneWidth,
scrollPaneConstraints.getY(),
Spring.scale(scrollPaneWidth, 0.25f),
scrollPaneConstraints.getHeight());
layout.putConstraint(SpringLayout.EAST, textAndUsers, 0,
SpringLayout.EAST, frmUserList);
layout.putConstraint(SpringLayout.SOUTH, textAndUsers, 0,
SpringLayout.SOUTH, scrollPane);
textAndUsers.add(scrollPane, scrollPaneConstraints);
textAndUsers.add(frmUserList, listConstraints);
Notice that the creation of listConstraints specifies a width argument which is Spring.scale(scrollPaneWidth, 0.25f). This ensures the list is always one-fourth as wide as the scrollPane containing the JTextArea.
SpringLayout is tricky to use, in that you have to make sure to link the far edges of the layout container to child components explicitly, because SpringLayout won't grow to accommodate all the child components automatically. That's what the putConstraint calls are doing.

How to divide an applet/frame into two parts using panel and perform events between them [duplicate]

This question already has answers here:
Resizing issue with canvas within jscrollpane within jsplitpane
(3 answers)
Closed 8 years ago.
The program i want to make is
Divide an applet/frame(awt) into two parts using panel. The first panel contains four buttons naming ellipse, rectangle, circle and triangle. Taking one button and asking coordinates and make a figure on the other panel.
Please someone explain me the concept or working because im not good at layouts and know methods to use to convey from one panel to other..
Thank you very much
I would recommend starting out with a layout manager.. if you want 2 JPanels next to each other, you can use the GridLayout layout manager. It takes 2 arguments, and one of it's overloaded constructors takes 4 arguments.
setLayout(new GridLayout(rows, columns)); //one commonly used constructor
setLayout(new GridLayout(rows, columns, horizontalSpacePixels, verticleSpace));
GridLayout, when used will reshape to fit the largest component, and make each part of the grid an equal size-- however this isn't the case when you use a GridLayout inside of a GridLayout (the inner GridLayout might be too big to fit within the confines that the outer GridLayout puts on it.). If I simply do
JFrame jf = new JFrame("Laying the grid out");
jf.setLayout(new GridLayout(5, 5));
JPanel[] jp = new JPanel[25];
JLabel[] jl = new JLabel[25];
for(int i = 0; i < 25; i++) {
jp[i] = new JPanel();
jp[i].setBackground(Color.YELLOW);
jl[i] = new JLabel("This is label no. " + (i+1));
jp[i].add(jl[i]);
}
//now to add all 25 components in the 5x5 grid; you simply add them, and it
//automatically positions the jpanels in the order that you place them.. left to right.
for(int i = 0; i < 25; i++)
jf.add(jp[i]);
Here is an example program that involves a simple GridLayout, and an actionListener that responds to button events by changing one of the JPanel's color.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui {
private JPanel p2;
private JLabel side2;
private JFrame jf;
public static void main(String[] args){
new Gui();
}
public Gui(){
jf = new JFrame("Holds 2 panels side by side.");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new GridLayout(1, 2));
JPanel p1 = new JPanel();
p2 = new JPanel();
p1.setBackground(Color.BLACK);
p2.setBackground(Color.BLACK);
p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
JLabel[] space = new JLabel[20];
for(int i = 0; i < 20; i++)
space[i] = new JLabel(" ");
JButton jb1 = new JButton("Button 1");
JButton jb2 = new JButton("Button 2");
jb1.addActionListener(new BListen());
jb2.addActionListener(new BListen());
p1.add(space[0]);
p1.add(jb1);
p1.add(space[1]);
p1.add(jb2);
p1.add(space[2]);
jf.add(p1);
side2 = new JLabel("Change the color here with the buttons there.");
side2.setForeground(Color.GREEN);
p2.add(side2);
jf.add(p2);
jf.setSize(600, 200);
jf.setVisible(true);
}
private class BListen implements ActionListener {
public void actionPerformed(ActionEvent e) {
String buttonClicked = e.getActionCommand();
if(buttonClicked.equals("Button 1")) {
JOptionPane.showMessageDialog(null, "You pressed Button 1.");
p2.setBackground(Color.BLUE);
side2.setForeground(Color.MAGENTA);
jf.setVisible(true);
}
else if(buttonClicked.equals("Button 2")) {
JOptionPane.showMessageDialog(null, "You pressed Button 2.");
p2.setBackground(Color.ORANGE);
side2.setForeground(Color.DARK_GRAY);
jf.setVisible(true);
}
}
}
}

Categories