0

I put a bunch of JButtons in a 2D array into a GridLayout. I want the user to be able to change the size of this grid at run time, but I tried resetting the row/cols, re-adding the buttons, remaking the panel that this grid is in - all to no avail. How do I accomplish this?

Thanks for any help!

2
Contributors
14
Replies
15
Views
4 Years
Discussion Span
Last Post by idlackage
0

I tried removing it with removeLayoutComponent but it didn't work for some reason. Is there something else I should/can use?

0

Can you make a small,simple program that compiles, executes and shows what you are trying to do?

0

Something along the lines of this:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Test extends JFrame  {

   JButton[][] grid;
   int size;
   int newSize;
   Scanner in;
   GridLayout layout;

   Test() {

      super( "Test" );
      size = 5;
      layout = new GridLayout( size, size );
      setLayout( layout );
      setSize( 500, 500 );

      grid = new JButton[size][size];

      for( int i = 0; i < size; i++ ) {
         for( int j = 0; j < size; j++ ) {
            grid[i][j] = new JButton();
            add( grid[i][j] );
         }
      }

      grid[0][0].setText( "PROMPT" );
      grid[0][0].addActionListener( new ACT() );

      in = new Scanner( System.in );

   }

   private class ACT implements ActionListener {
      public void actionPerformed( ActionEvent e ) {
         prompt();
      }
   }

   public void prompt() {
      System.out.println( "Enter some size" );
      newSize = in.nextInt();
      remakeGrid();
   }

   void remakeGrid() {
      for( int i = 0; i < size; i++ ) {
         for( int j = 0; j < size; j++ ) {
            layout.removeLayoutComponent( grid[i][j] );
         }
      }
      size = newSize;
      layout.setRows( size );
      layout.setColumns( size );
      for( int i = 0; i < size; i++ ) {
         for( int j = 0; j < size; j++ ) {
            grid[i][j] = new JButton();
            add( grid[i][j] );
         }
      }
      pack();
   }

   public static void main( String[] args ) {

      Test app = new Test();
      app.setVisible( true );
      app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   }

}
0

What happens when you execute this code? I get the following:
lang.ArrayIndexOutOfBoundsException: 5

Recommendation for testing code: Don't require user input. Hardcode a test value:

  newSize = 7; //in.nextInt();                      //<<<<<<<<<<<<<<<<
0

When I execute it and put in a number smaller than the original size (5), it'll add a bunch of new buttons to the existing ones. I don't know why it doesn't work for larger numbers either since I (at least seem to have) changed the rows and cols of the layout before looping through and adding new things--error occurs right before the adding-for loop. I guess I'm going about setRows/setColumns wrong since they're apparently not doing anything?

0

put in a number smaller than the original size

That is the reason I suggested you use a hardcoded value so we would all test with the same data.

Try removing everything and rebuiding the GUI's contents.

0

Yes, I have changed it to a hardcoded value. Removing everything is to be a big part in the problem though, since it's apparently not even happening despite the method call.

0

Haven't changed the code at all - just replaced newSize = in.nextInt(); with newSize = 7; as you suggested. I still have no idea what the problem is with regards to removing components (in remakeGrid()) so I don't know what else to change further.

0

And the problem is that I don't know how to. The only solution I've searched up repeatedly is removeLayoutComponents, but that doesn't do anything here.

0

Where do you remove the components from the container? Your code removes them from the layout manager.
Won't you need a new layout manager when the size of the grid changes?

0

Where do you remove the components from the container?

Alright, thanks - that's what I was having trouble with. The first time I tried calling remove(), I got an error, so I thought that wasn't the way to go but from your reminder I tried it again and it's fine now.

Apparently I don't need a new layout manager, as far as I've tested it...

Thanks for all the help!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.