We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,337 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reset/resize grid

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
20 Hours
Discussion Span
1 Year Ago
Last Updated
15
Views
Question
Answered
idlackage
Light Poster
38 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Remove all and recreate it

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

idlackage
Light Poster
38 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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 );

   }

}
idlackage
Light Poster
38 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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();                      //<<<<<<<<<<<<<<<<
NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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?

idlackage
Light Poster
38 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

idlackage
Light Poster
38 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Can you show the code?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

idlackage
Light Poster
38 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

what the problem is

You are not removing the components.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

idlackage
Light Poster
38 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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!

idlackage
Light Poster
38 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by NormR1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0975 seconds using 2.73MB