• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set (minimum) panel size for panel with a Graphics2D object only?

 
Evert DeBoer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program to solve Sudoku puzzles. I create the user interface using AWT/Swing.

One panel of the main window (JFrame object) contains a Graphics2D component only (a grid for the sudoku puzzle). The problem is that this panel is too small for its contents when it is created. I have tried many things to make it bigger (like the setSize method of the panel), but to no avail. Who can tell me how I can make the panel with a single Graphics2D object larger?

Below is the code for the main window, which is a JFrame object. I set its size to 400 x 400 to have ample room for all panels. I then create two panels: a sudoku panel that contains the sudoku grid and a button panel that contains two buttons. The button panel has a flow layout.

I then create the main panel. This has a vertical BoxLayout. Finally I add the sudoku panel and the button panel to the main panel and the main panel to the frame.



Below is the code for the SudokuPanel class.



When I run the program, the following Window shows up:



As you can see, the button panel is exactly in the middle (and it remains in the middle, no matter how I resize the window). And there is not enough room for the sudoku panel.

How can I make enough room for the sudoku panel when the window is created?
 
Piet Souris
Rancher
Posts: 1631
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Evert,

in your Sudokupanel, override the 'getPreferredSize()' method.
You can calculate the exact width and height of your panel.

In this case, do not set the size of your frame (line 07).
Use 'pack();' to set the size of your frame.

Greetz,
Piet
 
Evert DeBoer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! t works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!