KisnardOnline
KisnardOnline

Reputation: 740

proper way to create multiple similar buttons/panels

I have the below Code which I tried to do, but it only shows(the minus/plus button) on the last GridLayout (Intelligence stat):

JButton plusButton = new JButton("+");
JButton minusButton = new JButton("-");

statStrengthGridPanel = new JPanel(new GridLayout(1,3));
statStrengthGridPanel.add(minusButton);
statStrengthGridPanel.add(new JLabel("10"));
statStrengthGridPanel.add(plusButton);

statConstitutionGridPanel = new JPanel(new GridLayout(1,3));
statConstitutionGridPanel.add(minusButton);
statConstitutionGridPanel.add(new JLabel("10"));
statConstitutionGridPanel.add(plusButton);

statDexterityGridPanel = new JPanel(new GridLayout(1,3));
statDexterityGridPanel.add(minusButton);
statDexterityGridPanel.add(new JLabel("10"));
statDexterityGridPanel.add(plusButton);

statIntelligenceGridPanel = new JPanel(new GridLayout(1,3));
statIntelligenceGridPanel.add(minusButton);
statIntelligenceGridPanel.add(new JLabel("10"));
statIntelligenceGridPanel.add(plusButton);

I know I can do something like I did for the Panel names(have multiple ones), but I do not want to do that for the Panels in the first place. I am trying to use best practice and don't want my code to be repetitive. Any suggestions??

The goal is to have 4 stats, to assign points to, with decrement and increment buttons(I decided against sliders). Eventually I will have their have upper and lower limits, decrement the "unused" label, and all of that good stuff, but I just want not to be repetitive.

Upvotes: 0

Views: 629

Answers (1)

Katu
Katu

Reputation: 1564

The reason why its not working is that you are adding the same buttons to different gridpanels. I think that you need to create new ones for every place you want to see them. Try something like

statStrengthGridPanel = new JPanel(new GridLayout(1,3));
statStrengthGridPanel.add(new JButton("-"));
statStrengthGridPanel.add(new JLabel("10"));
statStrengthGridPanel.add(new JButton("+"));

Upvotes: 1

Related Questions