0

I'm making a BMR calculator and one of my panels gives the user an option to change how they wish to enter their height, from cm to ft/inches.

Here's the block of code that deals with said panel.

    // Height JComponents
    heightLabel = new JLabel("Height:");
    heightCMField = new JTextField(4);
    heightFTField = new JTextField(3);
    heightFTLabel = new JLabel("ft");
    heightINCHLabel = new JLabel("inch");
    heightINCHField = new JTextField(3);
    cmButton = new JToggleButton("cm");
    feetButton = new JToggleButton("feet");
    heightPanel.add(heightLabel);


    if (cmButton.isSelected()) {
        heightPanel.add(heightCMField);
    } else if (feetButton.isSelected()) {
        heightPanel.add(heightFTField);
        heightPanel.add(heightFTLabel);
        heightPanel.add(heightINCHField);
        heightPanel.add(heightINCHLabel);
    } 

    heightPanel.add(cmButton);
    heightPanel.add(feetButton);

My problem is, when I press the kg or cm button, the text fields do not appear so I'm thinking I've used isSelected() wrong somehow.

An image of how this appears is below. You can see that no text fields appear even when feet is selected. What can I do to fix this?

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • A [toggle button](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) is either selected or not selected. Your GUI looks like the buttons should be regular JButtons. – Gilbert Le Blanc Jun 16 '15 at 01:52
  • @GilbertLeBlanc I want to group these like radio buttons, so that I have one on as default and I can switch between them. Is that possible with toggle buttons? –  Jun 16 '15 at 02:25
  • *"I want to group these like radio buttons,"* Use a `JRadioButton` (in a `ButtonGroup`)! Further, the field for `Weight:`, `Height:` and `Age:` should each probably be a `JSpinner` with a `SpinnerNumberModel`. – Andrew Thompson Jun 16 '15 at 02:59
  • @AndrewThompson I prefer the toggle button look; is there no way I could make them work like radio buttons? I've managed to group them together so that only one can be selected at the same time, but it doesn't detect when I switch from cm to feet or vice versa. Also, I'll look into JSpinner and SpinnerNumberModel. –  Jun 16 '15 at 03:06
  • *"I prefer the toggle button look"* That might be relevant if you are the only user of the app., but in that case the problem becomes too specialized for me to care about.. – Andrew Thompson Jun 16 '15 at 03:12
  • Even if I do change it to JRadioButton, the issue persists, i.e. the labels/fields don't update. –  Jun 16 '15 at 03:13
  • @Ftahir192: It's possible to group toggle buttons like radio buttons, but the code required is so long and advanced, there;s no way I can describe all of the necessary code in 30,000 characters (the limit of an answer). – Gilbert Le Blanc Jun 16 '15 at 11:12
  • I think I managed to do it (albeit not sure if correctly). I just created a buttonGroup and to that button group I added cmButton and feetButton, and set cmButton to the default true. –  Jun 16 '15 at 15:50
  • kgButton = new JToggleButton("kg", true); lbButton = new JToggleButton("lbs"); ButtonGroup weightGroup = new ButtonGroup(); weightGroup.add(kgButton); weightGroup.add(lbButton); –  Jun 16 '15 at 15:51

1 Answers1

0

You need to add a listener:

cmButton.addActionListener(event->{

    /**
     * Code to show heightCMField.
     */
});

feetButton.addActionListener(event-> {

    /**
     * COde to show heightFTField and heightINCHField 
     */
});  

And if you are using JToggleButton, I suppose you want to use just one ToggleButton that switches your GUI. So if thats the case, delete cmButton and feetButton. And add only one new ToggleButton that does it all.

JToggleButton switchButton = new JToggleButton();
switchButton.setText("cm");  

switchButton.addActionListener(event->{

    if(switchButton.getText().equals("feet")) {

        switchButton.setText("cm");
        /* Code to show heightFTField and heightINCHField */
    } else if(switchButton.getText().equals("cm")) {

        switchButton.setText("feet");
        /* Code to show heightCMField */
    }
});  

You can also go for `ItemListener`.
Aditya Singh
  • 2,343
  • 1
  • 23
  • 42
  • Thanks for this, trying to get my head around it. I think I'll stick with 2 toggle buttons for now - what would I replace event-> with? Cheers –  Jun 16 '15 at 03:40
  • I can't seem to get any of this working and I'm not sure why. http://pastebin.com/ABxdT6fV is my current code, any ideas what I did wrong? –  Jun 16 '15 at 05:13
  • event-> remains as it is. Your code needs to goto comment section. – Aditya Singh Jun 16 '15 at 05:25
  • Whats wrong with pastebin? I tried your code as above to no avail, can't seem to get anything to work in terms of actionListeners. –  Jun 16 '15 at 05:32
  • I should add, the actionListener is registering the toggle selection, but the items just aren't being added to the panel for whatever reason. `code`feetButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println("blah1"); heightPanel.add(heightFTField); heightPanel.add(heightFTLabel); heightPanel.add(heightINCHField); heightPanel.add(heightINCHLabel); } });`\code` –  Jun 16 '15 at 05:35