0

I need to make sure that when I select a specific radio button, a JPanel that contains three JSpinner and three JLabel appears.

I wrote this code:

JRadioButton custom = new JRadioButton("Custom");
SpinnerModel spinnerModel = new SpinnerNumberModel(2, 0, 80, 1);
JSpinner height = new JSpinner(spinnerModel);
SpinnerModel spinnerModel2 = new SpinnerNumberModel(2, 0, 80, 1);
JSpinner width = new JSpinner(spinnerModel2);
SpinnerModel spinnerModelMine = new SpinnerNumberModel(1, 0, 99, 1);
JSpinner mines = new JSpinner(spinnerModelMine);

When I choose the custom JRadioButton, this JPanel should appear:

GroupLayout panelEstGroupLayout = new GroupLayout(panelEst);

        panelEstGroupLayout.setHorizontalGroup(
                panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup
                (
                        panelEstGroupLayout.createSequentialGroup()
                        .addGap(5)
                        .addGroup
                        (
                            panelEstGroupLayout.createParallelGroup(Alignment.TRAILING)
                            .addComponent(startGame)
                            .addComponent(boxTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        )
                )
                .addGroup
                (
                        panelEstGroupLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup
                        (
                                panelEstGroupLayout.createParallelGroup(Alignment.TRAILING)
                                .addGroup
                                (
                                        Alignment.LEADING, panelEstGroupLayout.createSequentialGroup()
                                        .addGap(6)
                                        .addComponent(heightLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(ComponentPlacement.UNRELATED)
                                        .addComponent(widthLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
                                        .addGap(18)
                                        .addComponent(minesLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
                                )
                                .addGroup
                                (
                                        Alignment.LEADING, panelEstGroupLayout.createSequentialGroup()
                                        .addComponent(height, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(ComponentPlacement.RELATED)
                                        .addComponent(width, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(ComponentPlacement.RELATED)
                                        .addComponent(mines, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
                                )
                        )
                )
        );
        panelEstGroupLayout.setVerticalGroup(
                panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(panelEstGroupLayout.createSequentialGroup()
                        .addGap(5)
                        .addComponent(boxTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addGroup(panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(panelEstGroupLayout.createSequentialGroup()
                                .addGap(74)
                                .addComponent(startGame))
                            .addGroup(Alignment.TRAILING, panelEstGroupLayout.createSequentialGroup()
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(panelEstGroupLayout.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(heightLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(widthLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(minesLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(panelEstGroupLayout.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(height)
                                    .addComponent(width, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(mines, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(43)))
                        .addGap(0))
            );

I insert this code into an if with the condition: if(custom.isSelected()) but it doesn't work. Why?

  • 3
    Please provide a [mcve] . – Arnaud Jun 20 '17 at 13:42
  • 1
    you need an ActionListener in which you check if the button is selected. otherwise, the program will check if the button is selected at the point of your if. also is a `GroupLayout` not a complete panel or frame or whatever – XtremeBaumer Jun 20 '17 at 13:45
  • [How to Write an Action Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – River Jun 20 '17 at 13:51
  • Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jun 20 '17 at 13:59

1 Answers1

3

You should register an action listener for the radio button, and when it is changed you can then hide/unhide or disable/enable the panel as appropriate based on the radio button state.

As for when the panel is created, you could choose to create it right away but keep it hidden/disabled until needed. Alternately you could only create it after the action comes in. Personally I prefer to create all elements at the start rather than distribute GUI element creation throughout the code.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
  • 1
    I like the idea of enabling / disabling the components, and agree that hide / unhide is less than optimal and all components should be added at start-up. But the same effect as hide / unhide can be achieved by using a card layout, and it allows adding all components at start-up (so the GUI can be correctly sized using `pack()`). – Andrew Thompson Jun 20 '17 at 14:02