Issue with changing JButton size - java

I'm trying to make a mine sweeper game with Swing library (just for training).
I spotted a problem when I tried to change size of a button with setSize() method (part of Container class). One of my previous program was able to handle that (with ActionListener)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonChanger extends JFrame {
JButton big, small, dis;
JLabel message, poof;
public ButtonChanger(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
message = new JLabel("Click to make it");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
add(message, c);
big = new JButton("BIG");
big.setSize(30, 30); // no reaction for this call
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
add(big, c);
small = new JButton("small");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
add(small, c);
dis = new JButton("disapear");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
add(dis, c);
poof = new JLabel(" poof!");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
poof.setVisible(false);
add(poof, c);
big.setSize(300,300); // same story
event a = new event();
big.addActionListener(a);
small.addActionListener(a);
dis.addActionListener(a);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent a){
String op = a.getActionCommand();
if(op.equals("BIG")){
big.setSize(50,50); // finaly!
} else if(op.equals("small")){
small.setSize(10,10);
} else if(op.equals("disapear")){
dis.setVisible(false);
poof.setVisible(true);
}
}
}
public static void main(String args[]){
ButtonChanger gui = new ButtonChanger();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(250,250);
gui.setTitle("");
}
}
But when I tried to use same solution here - nothing happened.
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel
implements ActionListener{
private Integer pixelWidth;
private Integer pixelHeight;
public Field[][] board;
private Random generator = new Random();
public Board(int width,int height){ // width - x axis scaled with "button" unit
// height - y axis scaled with "button" unit
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
board = new Field[width][height];
pixelWidth = width * Field.fieldW;
pixelHeight = height * Field.fieldH;
for(int i = 0;i < height;i++)
for(int j = 0;j < width;j++){
board[i][j] = new Field(generator.nextBoolean());
}
for(int i = 0;i < height;i++){
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = i;
for(int j = 0;j < width;j++){
c.gridx = j;
add(board[i][j].getFieldButton(),c);
add(board[i][j].getFieldLabel(),c);
}
}
/* for(int i = 0;i < height;i++)
for(int j = 0;j < width;j++){
board[i][j].setField();
}*/
}
public void actionPerformed(ActionEvent e){
String co = e.getActionCommand();
if(co.equals(" ")){ // button is single space sign
for(int i = 0;i < 10;i++)
for(int j = 0;j < 10;j++){
board[i][j].setField(); //inside this method is call for setSize() for fieldButton
}
}
}
}
Previously I changed Board class to make it inhetitate after JFrame (set up as main GUI frame in whole code) but that gave me same output as original.
Did I mess up something?

big = new JButton("BIG");
big.setSize(30, 30); // no reaction for this call
c.fill = GridBagConstraints.HORIZONTAL;
There is no reaction for big.setSize... because then in GridBagConstraints you set fill to horizontal, so button will be "stretched" to fill horizontally.
Instead use c.fill = GridBagConstraints.NONE
fill
Used when the component's display area is larger than the component's requested size to determine whether and how to resize the component. Valid values (defined as GridBagConstraints constants) include NONE (the default), HORIZONTAL (make the component wide enough to fill its display area horizontally, but do not change its height), VERTICAL (make the component tall enough to fill its display area vertically, but do not change its width), and BOTH (make the component fill its display area entirely).
You can read more here: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Related

JFrame: How to add a button that adds a new text field to the frame?

I'm pretty new to Java and I thought I'd try to get my hands dirty and make a GUI but I can't seem to get it to work the way I want it to.
I wrote some code thinking that if I press the "Add" button on the GUI then a new JTextField will appear underneath where all the other textfields are but that doesn't happen. Only one new JTextField does appear but it appears next to my Add button instead of underneath all the other textfields I have and if I press it again, nothing happens. I tried playing around with other variables but it just doesn't seem to be working properly. I feel like something is wrong with my ActionListener but I don't know what.
public class TheGUI extends JFrame{
List<JTextField> listOfTextFields = new ArrayList<JTextField>();
private JTextField desc1;
private JTextField instruct;
private JTextField desc2;
private JButton submit;
private JButton addNew;
public TheGUI() { //My GUI with the default fields & buttons that should be on there.
super("Chili");
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
instruct = new JTextField("Choose your words");
instruct.setEditable(false);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
add(instruct, c);
addNew = new JButton("Add");
c.weightx = 0.0;
c.gridx = 1;
c.gridy = 0;
add(addNew, c);
submit = new JButton("Submit!");
c.weightx = 0.5;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = GridBagConstraints.PAGE_END;
add(submit, c);
desc1 = new JTextField(10);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 1;
add(desc1, c);
desc2 = new JTextField(10);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 2;
add(desc2, c);
addNew.addActionListener(new Adder());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);
}
private class Adder implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
int i = 0;
listOfTextFields.add(new JTextField());
GridBagConstraints textFieldConstraints = new GridBagConstraints();
//Give it a max of 9 text fields that can be created.
while(i < 10) {
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
textFieldConstraints.weightx = 0.5;
textFieldConstraints.gridx = 0;
textFieldConstraints.gridwidth = 2;
textFieldConstraints.gridy = 3 + i;
i++;
}
add(listOfTextFields.get(i), textFieldConstraints);
revalidate();
repaint();
}
}
}
Your while loop is really strange.
Your ActionListener should look like:
private class Adder implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
if (listOfTextFields.size() == 9) {
// Give it a max of 9 text fields that can be created.
return;
}
JTextField textfield = new JTextField();
listOfTextFields.add(textfield);
GridBagConstraints textFieldConstraints = new GridBagConstraints();
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
textFieldConstraints.weightx = 0.5;
textFieldConstraints.gridx = 0;
textFieldConstraints.gridwidth = 2;
textFieldConstraints.gridy = 3 + listOfTextFields.size();
add(textfield, textFieldConstraints);
revalidate();
repaint();
}
}

Changing where an object is on a GridBagLayout in Java

I am working on a project where I have a requirement that when a button is clicked, an object that is in a GridBagLayer moves to a different spot. Here is my code so far:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class main extends JFrame{
private JButton button,littlebutton;
private JLabel holdred1,holdred2;
private ImageIcon red1,red2;
public main(){
//sets the layout
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//sets the button
button = new JButton("Button");
c.gridx = 0;
c.gridy = 0;
add(button,c);
//sets the image
red1 = new ImageIcon(getClass().getResource("/images/redtile.png"));
red2 = new ImageIcon(getClass().getResource("/images/redtile.png"));
//puts it in the JLabel
holdred1 = new JLabel(red1);
c.gridx = 0;
c.gridy = 0;
add(holdred1, c);
holdred2 = new JLabel(red2);
c.gridx = 1;
c.gridy = 0;
add(holdred2, c);
littlebutton = new JButton("Click Me");
c.gridx = 0;
c.gridy = 0;
add(littlebutton,c);
event e = new event();
littlebutton.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent e){
//code goes here
}
}
public static void main(String args[]){
//displaying the window
main gui = new main();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(575,575);
gui.setVisible(true);
gui.setTitle("Change Spot");
}
}
I don't have much experience with Grid Bag Layouts so I have no clue how to do this, Thanks in advance!
The question is how can I make the button change it's gridx to 1
In GridBagConstraints you specify what position you want the element to take in the grid. You already have done that, in this portion of code
c.gridx = 0;
c.gridy = 0;
add(holdred1, c);
holdred2 = new JLabel(red2);
c.gridx = 1;
c.gridy = 0;
add(holdred2, c);
littlebutton = new JButton("Click Me");
c.gridx = 0;
c.gridy = 0;
But you're telling the layout to place multiple components in the same grid using c.gridx = 0, c.gridy = 0. Every component should have its own cell space so they don't overlap.
I edited your code:
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//sets the button
button = new JButton("Button");
c.gridx = 2;
c.gridy = 2;
add(button, c);
//sets the image
// red1 = new ImageIcon(getClass().getResource("/images/redtile.png"));
// red2 = new ImageIcon(getClass().getResource("/images/redtile.png"));
//puts it in the JLabel
holdred1 = new JLabel("Red1");
c.gridx = 1;
c.gridy = 1;
add(holdred1, c);
holdred2 = new JLabel("Red2");
c.gridx = 1;
c.gridy = 2;
add(holdred2, c);
littlebutton = new JButton("Click Me");
c.gridx = 2;
c.gridy = 1;
add(littlebutton, c);
event e = new event();
littlebutton.addActionListener(e);
I assume that you wanted the labels to be on top of the buttons, so I place they on a side so you could practice it.
Another easy tips, if you want a component to take more than a cell in the table you have to use c.weightx = someValue, c.weighty = someValue.
Take a look: How to use GridBagLayout.

Weird layout using GridBagConstraints

I have several methods which create their own component, JButton or JLabel in a JPanel. Then I have a separate method which adds all these JPanels the the JFrame. I also use gridx and gridy on the JPanels to position them how I want. This is: lookreply on the left, then top right the title and below in a 2X2 table quit, restart, pickup and hello. However my current code when run displays a weird, random layout.
The lookreply is on the left, but then to the right is quit, a space, restart then hello all vertical. pickup and title aren't seen. I dont know why this is happening.
Please see my code below.:
public class GUI extends JPanel
{
/**
* Creation of variables used throughout the GUI Class.
*/
//JPanel panel = new JPanel(new GridBagLayout());
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
GUI g = new GUI();
g.create();
}
private void create()
{
JFrame screen = new JFrame("Dungeon of Doom");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//set size to full screen.
screen.setExtendedState(Frame.MAXIMIZED_BOTH);
//Add all JPanes to screen
screen.add(lookReply(), c);
c.gridy = 0;
c.gridx = 0;
screen.add(title(), c);
c.gridy = 0;
c.gridx = 1;
c.gridwidth = 2;
screen.add(quit(), c);
c.gridy = 1;
c.gridx = 1;
screen.add(restart(), c);
c.gridy = 1;
c.gridx = 2;
screen.add(pickup(), c);
c.gridy = 2;
c.gridx = 1;
screen.add(hello(), c);
c.gridy = 2;
c.gridx = 2;
screen.setVisible(true);
}
One of the methods (quit)
private JPanel quit()
{
JPanel quitPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton quit = new JButton("QUIT");
quitPanel.add(quit, c);
return quitPanel;
}
All the other methods are pretty much the same except the title is a a JLabel and the table iterates to create a 5x5 table of JLabel within its own JPanel. Any help is appreciated!
I have found what was doing this.
As seen in the code I was adding the component before setting the layout.
screen.add(lookReply(), c);
c.gridy = 0;
c.gridx = 0;
whereas it should be
c.gridy = 0;
c.gridx = 0;
screen.add(lookReply(), c);

Inconsistent placement of components (button and checkbox) using GridBagLayout

The code below was copied from Oracle tutorials and modified to place components OTHER than buttons--a label and two text fields and an additional button. It produces the form that it should (shown below), so you shouldn't have to scrutinize the first 30 or so lines of code:
If I UNcomment out the two lines that refer to "box" and comment out the adjoining lines that refer to button, look at where the checkbox is placed.
I can't figure out how what's going on. I want the checkbox on the last line, as the code SEEMS to say it MUST be positioned, yet it is NOT.
All I am doing is changing TWO LINES that refer to button to refer instead to checkbox. Are the rules for GridBagLayout component-dependent?? What should I do (and why isn't the code working as is) to get checkbox on bottom line?
public class Testy {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0; c.gridy = 0;
JLabel label = new JLabel("Button 1 ");
pane.add(label, c);
JTextField text = new JTextField("Button 2 ");
c.gridx = 1; c.gridy = 0;
pane.add(text, c);
JButton button = new JButton("Button 3 ");
c.gridx = 2; c.gridy = 0;
pane.add(button, c);
text = new JTextField("LONG Text 4 ");
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0; c.gridy = 1;
pane.add(text, c);
button = new JButton("5 ");
c.ipady = 0;
c.weighty = 1.0;
c.gridx = 1; c.gridy = 2;
c.gridwidth = 2;
pane.add(button, c);
// Here's the part of the code in question
button = new JButton("666"); // This placement is FINE, at bottom of form.
// JCheckBox box = new JCheckBox("Me?"); // "box" does NOT get placed on last line.
c.weighty = 0;
c.gridx = 0; c.gridy = 3;
c.gridwidth = 1;
// pane.add(box);
pane.add(button, c);
}
Here are the required imports and two methods that I removed so the problem code area above could be seen without scrolling:
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
private static void createAndShowGUI() /* calls addComponentsToPane() */{
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) /* calls createAndShowGUI() */ {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
} }); }
}
// pane.add(box);
pane.add(button, c);
You just have to use the right method:
pane.add(box, c);
See Container#add(Component comp) and Container#add(Component comp, Object constraints).

GradBagLayout not distributing columns based on weight

This is what my GUI looks like now:
I want it to have the three columns to be equally distributed. To do this, I set each of the weights equal to 1/3. Obviously, it's not working.
Here is my code for creating the Frame:
public static JPanel createLayout(int rows) {
JPanel product = new JPanel(new GridBagLayout());
String[] lables = {"School ", "Advanced #", "Novice # "};
double weight = .3333333333333;
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(3, 3, 3, 3);
c.weightx = weight;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.gridy = 1;
for (int j = 0; j < lables.length; j++) {
c.gridx = j;
JLabel l = new JLabel(lables[j]);
product.add(l, c);
}
for (int i = 0; i < rows; i++) {
c.gridy++;
for (int j = 0; j < lables.length; j++) {
c.gridx = j;
JTextField f = new JTextField();
product.add(f, c);
}
}
c.gridy++;
c.gridx = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.NONE;
JPanel b = new JPanel();
JButton add = new JButton("+");
b.add(add);
JButton delete = new JButton("-");
b.add(delete);
product.add(b, c);
return product;
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Debate Calculator");
JPanel debates = new JPanel();
frame.add(createLayout(5), BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
The problem is that you components are not equally sized to begin with. I can't explain exactly why it does what it does, but the size of your labels is each different because they have a different number of characters. I know you tried to make then the same size, but a " " is not the same as "W".
I changed your code to use the following and it seems to work:
JTextField f = new JTextField(10);
Now the width of each text field is greater than the label, so that is the width that is used to give each column a proportional size.
You might consider using a GridLayout. The default behaviour is to make each cell the same size.
The problem is your button bar at the bottom. Set the gridwidth to REMAINDER on that element.

Categories