user2795095
user2795095

Reputation: 511

Two JPanels in one JFrame - the second overrides the first

I am trying to create a GUI where I add two JPanels to one JFrame, but the second JPanel I add override the first. In my first JPanel I have a sudoku box, and in the second I want a button. But, since the first one I add always overrides the second, this doesn't work.

My sudoku JPanel uses GridLayout, and this alone works perfectly. The problem is when I try to add the second JPanel (which has a JButton). Since the button needs to be another size than the squares in the sudoku box, I can't add this button to the first JPanel.

Is it possible to solve this using two JPanels, or do I need a different layout? I have read some about GridBagLayout and think this might be a solution, but it's a bit boring to change the whole code for my JPanel which has the sudoku in it.

Upvotes: 1

Views: 423

Answers (1)

Dmitri
Dmitri

Reputation: 1378

You could just make another JPanel with an appropriate layout manager, and add the two smaller panels inside it. Having panels inside of panels is a good way to break up your layout into less complex pieces while maintaining better control of resize behavior, etc.

Upvotes: 1

Related Questions