Relative sizing not working - java

I've been trying to make a JLabel that is sized relatively to the size of the window yet for some reason this JLabel is not appearing on the screen.
This is the code used in my MainGUI class which holds the basic interface:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainGUI extends JFrame{
JPanel core;
GridBagConstraints c;
JLabel[] sts;
public MainGUI(){
core = new JPanel(new GridBagLayout());
getContentPane().add(core, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
sts = new JLabel[10];
int width = (int)(66/100) * getWidth(), height = (int)(75/100) * getHeight(); //problem: due to these sizes the JLabel is not appearing
for(int i = 0; i < sts.length; i++){
sts[i] = new JLabel("test");
sts[i].setOpaque(true);
sts[i].setBackground(Color.BLACK);
sts[i].setForeground(Color.BLACK);
sts[i].setPreferredSize(new Dimension(width,height)); //size being set
}
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
core.add(sts[1], c);
}
}
Any help in finding a solution for this would be greatly appreciated, thanks in advance.

Few issues-
Foreground and background colors are same for label
You need to set some text on the label
Check if preferred size is calculated correctly, as the label is displayed if that line is commented out. The height and width calculated is zero.
This will always return zero as the result of division is int-
int width = (int)(66/100) * getWidth();
Make it-
int width = (int)(((float)66/100) * getWidth());

Try RelativeLayout library. A tutorial is here at Wiki.

Related

Improper sizing using GridBagLayout

I am trying to use a GridBagLayout to have a JFrame that contains a JPanel that has a grid layout and a JPanel with just a large button. I want the rows to all be the same size, and the JPanel with the JButton to be the same size as one row. However, the button panel, which is currently empty, is about 1/3 of the JFrame. I'm not quite sure what's happening, but it is pretty important to me that I maintain this structure because the rest of my code uses this. Any help is appreciated, and thank you in advance.
This is my code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Minesweeper extends JPanel {
private final int SIZE = 7;
public void startGame(){
JFrame holder = new JFrame();
JPanel window = new JPanel();
JPanel pan = new JPanel();
holder.setLayout(new GridBagLayout());
GridBagConstraints con = new GridBagConstraints();
con.weightx = 1;
con.weighty = 1;
con.gridx = 0;
con.gridy = 0;
con.fill = GridBagConstraints.BOTH;
con.gridheight = SIZE;
con.gridwidth = SIZE;
holder.getContentPane().setBackground(Color.darkGray);
holder.setSize(450, 450);
holder.setResizable(false);
holder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBackground(Color.darkGray);
window.setLayout(new GridLayout(SIZE, SIZE));
for (int c=0; c<(SIZE*SIZE); c++){
int row = (c/SIZE);
int col = (c%SIZE);
JPanel p = new JPanel();
p.setBackground(Color.gray);
Border b = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
p.setBorder(b);
window.add(p);
}
holder.add(window, con);
con.gridx = 0;
con.gridy = SIZE+1;
con.gridheight = 0;
con.gridwidth = SIZE;
holder.add(pan, con);
holder.setVisible(true);
}
public static void main(String[] args){
Minesweeper start = new Minesweeper();
start.startGame();
}
}
This is what is being shown:
con.gridy = SIZE+1;
You can't specify a gridy value of 8. There are only two components added to the grid. The grid doesn't know that one of your panels happens to contain 7 rows of components. So the value should be 1.
This won't solve the problem but should clear up a misunderstanding of how GridBagLayout works.
holder.setSize(450, 450);
You are manually setting a size to the frame. Each component is originally sized at its preferred size. When there is extra space in the frame the space is distributed equally between the two components.
You should NOT be setting the size. Each component should determine its own size and then you should use pack(). So you need to use custom components that override the getPreferredSize() method to return the appropriate size for each component so pack() can do its job.
Also, the pack() is done just before the setVisible().

GridBagLayout creating very small panels

I am trying to set the layout of a JFrame to be a grid bag layout. I want to to essentially look like 4 grids of equal size, but with the bottom 2 merged into one panel. I am adding JPanels to each. However, I get 3 small grids at the middle of the JFrame, not properly sized. It looks like this instead.
My code is as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Hangman extends JPanel{
private String word;
private JPanel hA, gL, letters;
public void setupLayout(JFrame window){
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
hA = new JPanel();
hA.setBackground(Color.blue);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
window.add(hA, c);
gL = new JPanel();
gL.setBackground(Color.green);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
window.add(gL, c);
letters = new JPanel();
letters.setBackground(Color.black);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
window.add(letters, c);
}
public void startWindow(){
JFrame window = new JFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = (int) screenSize.getHeight();
int windowHeight = (int) ((screenHeight / 4) * 3);
window.setSize(windowHeight, windowHeight);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setupLayout(window);
window.setVisible(true);
}
public void startGame(){
Prep prepare = new Prep();
word = prepare.findWord(true);
startWindow();
}
public static void main(String[] args){
Hangman start = new Hangman();
start.startGame();
}
}
It's not very important what Prep does. It just takes a random word from a text file. I couldn't see how it would affect the layout. To recap, I need to make it so each of the 4 grid spaces span one fourth of the window, and then make the bottom 2 cells merged with a JPanel added to each of them. Any help is appreciated. Thank you in advance.
EDIT:
I needed to set the weight of all of them to be 1. It is solved.
GridBagLayout displays each component at its preferred size. Since you didn't add any components to the panels you just see a small panel.
If you want the panels to fill the space available then you need to play with the constraints.
Read the section from the Swing tutorial on How to Use GridBagLayout for more information and working examples. You will want to look at the "fill" and "weightx/weighty" constraints.
and then make the bottom 2 cells merged
you will also need to look at the "gridwidth/gridheight" constraints.

Setting GridBagLayout grid's size

I'm trying to create a console using Java Swing (GridBagLayout).
I don't know why, but as you can see at the left margin, grids don't have the correct size.
It's supposed to be shown this way:
Where light blue is the list, green the image, orange the text panel and yellow the text field.
I don't know how to make the list bigger and the image smaller. Too, the text field's grid is binded to the list one, even tough the list is on y 1 and the text field on y 2.
Here's some code.
// Command List
DefaultListModel<String> listInput = new DefaultListModel<String>();
JList<String> list = new JList<String>(listInput);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(list);
list.setBackground(new Color(160, 160, 160));
list.setSelectionBackground(new Color(150, 150, 150));
scrollPane.setPreferredSize(new Dimension(20, 20));
manager.setCommandList(listInput);
c.insets = new Insets(2, 2, 2, 2);
c.ipady = 0;
c.ipadx = 100;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 2;
c.weightx = 0.1;
c.weighty = 0.6;
c.fill = GridBagConstraints.BOTH;
console.add(scrollPane, c);
// Image Displayer
JLabel image = new JLabel(new ImageIcon());
manager.setImageField(image);
c.insets = new Insets(0, 0, 0, 0);
c.ipady = 0;
c.ipadx = 0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.1;
c.weighty = 0.3;
c.fill = GridBagConstraints.BOTH;
console.add(image, c);
where 'c' is a grid bag constraint and console the main JPanel.
As you can see, the list has a grid height of 2 and weight of 0.6, and the image a grid height of 1 and weight of 0.9, so not sure why the list is 4 times smaller than the image.
Another issue, I've added a listener to the JLabel holding the image (on resize), anyways, it isn't called. Should I add the listener to the main panel? as the image is only being resized by the layout manager.
Thanks ^^
EDIT:
SSCCE:
package co.relieved.jelly.application.display.swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
#SuppressWarnings("serial")
public class Test extends JPanel {
static JLabel image;
public static void main(String[] args) {
display();
try {
BufferedImage buffer = ImageIO
.read(new File("/home/juanco/Pictures/Screenshot from 2016-02-08 22-43-22.png"));
image.setIcon(new ImageIcon(
buffer.getScaledInstance(image.getWidth(), image.getHeight(), BufferedImage.SCALE_SMOOTH)));
} catch (IOException ex) {
ex.printStackTrace();
}
}
public Test() {
super(new GridLayout(1, 1));
JTabbedPane tabs = new JTabbedPane();
/*** >>> Console Pane <<< ***/
JPanel console = new JPanel();
console.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.ipadx = 0;
c.ipady = 0;
c.gridwidth = 1;
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
// Console Screen
JTextPane screen = new JTextPane();
screen.setEditable(false);
c.gridx = 1;
c.gridy = 0;
c.gridheight = 2;
c.weightx = 0.8;
c.weighty = 1;
console.add(screen, c);
// Console Input
JTextField input = new JTextField();
c.insets = new Insets(2, 0, 2, 0);
c.ipady = 3;
c.gridy = 2;
c.gridheight = 1;
c.weighty = 0;
c.fill = GridBagConstraints.HORIZONTAL;
console.add(input, c);
// Command List
DefaultListModel<String> listInput = new DefaultListModel<String>();
listInput.setSize(1);
JList<String> list = new JList<String>(listInput);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(list);
c.insets = new Insets(2, 2, 2, 2);
c.ipady = 0;
c.ipadx = 100;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 2;
c.weightx = 0.1;
c.weighty = 0.6;
c.fill = GridBagConstraints.BOTH;
console.add(scrollPane, c);
// Image Displayer
image = new JLabel(new ImageIcon());
c.insets = new Insets(0, 0, 0, 0);
c.ipadx = 0;
c.gridy = 0;
c.gridheight = 1;
c.weighty = 0.3;
console.add(image, c);
// General
tabs.addTab("Console", console);
/*** >>> Logs Pane <<< ***/
JPanel logs = new JPanel();
tabs.addTab("Logs", logs);
// Setup
tabs.setSelectedIndex(0);
add(tabs);
}
static void display() {
JFrame frame = new JFrame("Relieved Console");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 500));
frame.add(new Test(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Here's some code.
Which doesn't help. The grid can only be completed with the entire code. That is we need to know the gridwidth and gridheight for all components in order to determine the allocation of space to each component in the grid.
the text field's grid is binded to the list one, even tough the list is on y 1 and the text field on y 2.
You can't just randomly assign a component to a grid. The component will only go to grid 2 if the component above it has a grid height of 2. So basically each of your columns needs to have components with a total grid height of 3.
I don't know how to make the list bigger
Setting a preferred size of (20, 20) doesn't help. Anyway you should not be using the setPreferredSize() method.
Instead you should be using:
list.setVisibleRowCount(...);
to specify the visible rows. Then the JList can determine its own preferred size.
Another layout option is to use nested panels which can simplify the layout.
So you could start with a "west" panel that uses a BorderLayout. Then you add the label to "PAGE_START" and the list to "CENTER".
Then you create a "center" panel. Add the main component to the "CENTER" and the text field to the "PAGE_START".
Then you add the two panels to the frame:
frame.add(westPanel, BorderLayout.LINE_START);
frame.add(centerPanel, BorderLayout.CENTER);
Edit:
Sorry, I take back my comment about making each column have a grid height of 3. You can't just specify a total grid height of 3 because you only have 2 components in each column, so each component can only have a height of 1.
Check out my answer in this posting: Why does this GridBagLayout not appear as planned? for a hack that allows you to manipulate gridHeight/Weight by using invisible components in a row/column.
However, I don't recommend that approach. it will be far easier to use my suggestion of nested panels using a BorderLayout (or some other layout manager on the nested panels).
It seems to me that you would need to also specify the gridwidth and/or gridheight for the text field as well.
GridBagLayout is like a glorified GridLayout. It will attempt to align all of the components with the nearest grid spaces around it. Oracle's Tutorials also describe this behavior.
Much of the time, issues with laying out components using GridBagLayout come from the other components being added to the layout instead of the apparent problem child.
I fixed it splitting the "console" JPanel in two, a border layout panel to the left and a grid bag layout panel to the right. As #camickr suggested

JPanel not updating when using invalidate, validate and repaint

I have a problem that one of my JPanels doesn't update itself according to the window when its supposed too. I shall try and briefly explain whats happening. A list of characters is fed into an arraylist (named lookReply) from another class (this works as I've tested this) it then uses 2 iterations to assign each one to a coordinate in a square table made from JLabels. These JLabels are in the lookReplyGUIPanel JPanel. After a button is pressed new characters get loaded into the arraylist and it repeats itself. However the window does not show this update. I know they are getting fed into the JLabels through some tests but its just the updating of the window doesn't seem to be working. I am using invalidate, validate and repaint but it still doesn't work. Please see my code below for the parts needed.
The first method called - deals with the arraylist then calls the other method.
private void look()
{
//clear arrayList then add new lookReply to it
lookReply.clear();
while (HumanUser.lookReply==null)
{
}
for(int n = 0; n<HumanUser.lookReply.length(); n++)
{
lookReply.add(HumanUser.lookReply.charAt(n));
}
lookReply();
//UP TO HERE WORKS FINE
screen.invalidate();
screen.validate();
screen.repaint();
}
The method which deals with the JPanel in question.
/**
* Made up of several smaller JPanels each relate to 1 map position from the lookReply command.
*/
private JPanel lookReply()
{
JPanel lookReplyGUIPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
Border blackline = BorderFactory.createLineBorder(Color.black);
//Create a square table dependent on the radius of view.
//Then fill each cell with the block retrieved from the lookReply arrayList.
for(int y = lookReplyY; y>0; y--)
{
for(int x = lookReplyX; x>0; x--)
{wall...
//Then assign it to a variable which is used in the JLabel.
String item = null;
if (lookReply.size()!=0)
{
item = lookReply.get(x*y-1) + "";
}
//ADD TO CHECK WHAT EACH ITEM IS AND USE THE RELEVENT PICTURE
JLabel lookx = new JLabel(item);
int width = (2*screenHeight/(5*lookReplyX)-10);
lookx.setPreferredSize(new Dimension(width, width));
lookx.setBorder(blackline);
c.gridx = x;
c.gridy = y;
lookReplyGUIPanel.add(lookx, c);
}
}
return lookReplyGUIPanel;
}
Some more details of the overview of everything if this helps. The first method called adds all the JPanels to the JFrame in the correct position using gridBagConstraints. The JFrame is created outside a method so all other methods can see it. All help much appreciated and happy to provide more details if needed! thanks
Here is a simple compilable piece of code which demonstrates the same problem. Ignoring the fact the window has to be resized - every button click on button increments m by 1 and should update to display the lookReply method of a 5X5 table of m. However it does not.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class temp
{
JFrame screen = new JFrame("temp");
int m = 1;
public static void main(String[] args)
{
temp g = new temp();
g.create();
}
private void create()
{
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridy = 0;
screen.add(lookReply(), c);
c.gridy = 1;
screen.add(button(), c);
screen.setVisible(true);
}
private JPanel lookReply()
{
JPanel lookReplyGUIPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
for(int y = 5; y>0; y--)
{
for(int x = 5; x>0; x--)
{
JLabel lookx = new JLabel((m + ""));
c.gridx = x;
c.gridy = y;
lookReplyGUIPanel.add(lookx, c);
}
}
screen.invalidate();
screen.validate();
screen.repaint();
return lookReplyGUIPanel;
}
private JPanel button()
{
JPanel button = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton b = new JButton("button");
b.setBorder(null);
c.gridx = 2;
c.gridy = 2;
button.add(b, c);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
m++;
lookReply();
}
});
return button;
}
}
So, based on your example, lookReply creates a NEW JPanel, but you don't do anything with it. So the component is never added to the screen, so all attempts to make it refresh will have no effect...
Update your GUI elements using a javax.swing.Timer, as shown here and here.

GridbagLayout - get ButtonSize when c.fill = BOTH

I'm not really used to Java and Swing, but I need an answer for a school project :)
I have a JButton that is stretched to it's parents width/height via GirdbagLayout:
frame = new JFrame();
frame.setResizable(false);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
contentPane.setVisible(true);
JButton test = new JButton("TEST");
c.gridx = 0; c.gridy = 0; c.ipadx = 30; c.ipady = 30; c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH;
test.setVisible(true);
contentPane.add(test, c);
frame.setVisible(true);
Now, I need to get the button's width. The reason: The Button's font-size is calculated relative to the button's size (for this calculation its width is needed.).
System.out.println("BUTTON WIDTH "+test.getWidth());
test.getWidth() is zero :(
(this is called after pane, frame and Button were set visible).
What can I do :)
Thx in advance
UPDATE:
As suggested by Yohan Danvin, I used frame.pack().
But the behavior becomes a bit strange: As if the size-change would be animated (cfr. css-transitions - that's where I sometimes get similiar problems), it changes within about 30ms:
frame.pack();
frame.setVisible(true);
System.out.println(test.getWidth());
for(int i=0; i<10; i++){
try{
Thread.sleep(10);
System.out.println(test.getWidth());
} catch(Exception err){}
}
The first and second output is "93", the 9 other ones "1600" (what would be correct).
What happens in this time? Why changes the width?
Looking forward to anyone to enlighten me :)
UPDATE:
This way, it works, the correct width is calculated:
import javax.swing.*;
import java.awt.*;
public class VIEW{
private JFrame frame;
public VIEW(){
frame = new JFrame();
frame.setResizable(false);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridBagLayout());
contentPane.setVisible(true);
GridBagConstraints c = new GridBagConstraints();
JButton test = new JButton("TEST");
c.gridx = 0; c.gridy = 0; c.ipadx = 30; c.ipady = 30; c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH;
test.setVisible(true);
contentPane.add(test, c);
frame.pack();
frame.setVisible(true);
System.out.println(this.getWidth(test));
}
private int getWidth(JButton button){
try{
int i = 0, width = 0;
while(i++ < 10 && (width = button.getWidth()) < 100)
Thread.sleep(10);
return width;
} catch(Exception err){
return 0;
}
}
}
But of course it's a bit hacky to wait using Thread.sleep :) (and exspecially to wait till the value is bigger than 100... - this might only fit for this example and maybe even only for my screen resolution.)
Feel free to copy this class into your IDE and try it out :)
FINAL UPDATE:
SwingUtilities.invokeLater(new Runnable(){
public void run(){
System.out.println(test.getWidth());
}
});
==> waits for the window to maximize. perfect.
Problem solved :)
The problem is the size for the button has not been computed yet.
Try calling:
frame.pack();
before making the frame visible, then get the width.
UPDATE:
I think you're getting this issue because you use frame.setExtendedState(JFrame.MAXIMIZED_BOTH);, which unfortunately is not taken into consideration at the beginning (= by .pack()).
I think you have no choice than to wait for the window to be fully maximized before you can get the right value.
Use SwingUtilities.invokeLater(/*get the width here*/); instead of your custom thread sleeps. This is a more standard way to have code run after all OS events (including the maximization of the window I'm thinking) have been taken care of.

Categories