3

I have a frame with a BorderLayout. In the BorderLayout's center there is a JPanel with a GridLayout. Sometimes I have to change this inner JPanel's GridLayout from 9x9 to 4x4 (or 16x16). But unfortunately the size of the cells stay the same. After manually resizing the frame, the Layout adapts the sizes and it looks right again. Here is what it looks like after changing to 4x4 and after resizing:

after changing from 9x9 to 4x4

after resizing the frame

How can I force the inner Panel to automatically update? I tried repaint() on the inner panel (the one with the GridLayout) and the frame. At the moment i use this code which has some nasty side effects:

Dimension size = frame.getSize();
frame.setSize(new Dimension(size.height, size.width+1));
frame.setSize(size);

Is there any other ways to achieve this? Thanks in advance!

user4758246
  • 575
  • 5
  • 13
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Jul 19 '14 at 10:34
  • 1
    *"Sometimes I have to change this inner JPanel's GridLayout from 9x9 to 4x4 (or 16x16).."* Use a `CardLayout` to swap between them. – Andrew Thompson Jul 19 '14 at 10:36
  • This is an option but doensn't work for me because I have a (Sudoku) model object that is displayed in this view and I think it would be a bad solution to check every object if it was 4x4, 9x9 or 16x16 and then load and display the fitting view. – user4758246 Jul 22 '14 at 17:03
  • Your reply makes me think you had little idea of what I was suggesting! – Andrew Thompson Jul 23 '14 at 02:14
  • I think we maybe are talking past each other. I understood that you suggested to use a CardLayout to swap between three GridLayouts with 4x4, 9x9 and 16x16. I want to display a single Sudoku object in the GUI. If I had three GridLayouts inside a CardLayout I had to check whether this Sudoku is 4x4 or 9x9 or 16x16 and then chose the GridLayout where it fits. But I actually want only one GridLayout to display all kinds of sizes. Was that what you were suggesting? – user4758246 Jul 26 '14 at 08:44

1 Answers1

2

After manually resizing the frame, the Layout adapts the sizes and it looks right again

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Thank you very much, for my purpose it suffices to just call setVisible(true). – user4758246 Jul 19 '14 at 10:37
  • @user3158988: If `setVisible()` alone is enough, then you must be wrong in thinking that way. The advice in the answer is worth to be noted down and remembered by heart :-) – nIcE cOw Jul 19 '14 at 10:44
  • 1
    I tried using pack() because it sounded like a very useful function, but it resized every cell in a width-height-ratio of about 6:1 which looked awful. I did not set any preferred sizes (because the gridLayout and borderLayout center would ignore them anyway). Anyway I memorized that function for the next time I may need it :) – user4758246 Jul 19 '14 at 11:36