-1

Whenever I run this code the radio buttons display correctly until I tab in or hover into the window, then it only shows one of the radios and the only way I can see the others is by hovering over them.

Why do the radio buttons not display until I hover over the window or tab into it?

    JRadioButton rDollar1 = new JRadioButton("Dollars");
    JRadioButton rCDollar1 = new JRadioButton("Canadian Dollars");
    JRadioButton rMPesos1 = new JRadioButton("Mexican Pesos");
    JRadioButton rCPesos1 = new JRadioButton("Colombian Pesos");


    ButtonGroup bg1 = new ButtonGroup();
    bg1.add(rDollar1);
    bg1.add(rCDollar1);
    bg1.add(rMPesos1);
    bg1.add(rCPesos1);

    frame1.setSize(500, 500);
    frame1.setVisible(true);
    frame1.add(rDollar1);
    frame1.add(rCDollar1);
    frame1.add(rMPesos1);
    frame1.add(rCPesos1);
    rDollar1.setBounds(10, 50, 50, 20);
    rDollar1.setVisible(true);
    rCDollar1.setBounds(10, 100, 50, 20);
    rCDollar1.setVisible(true);
    rMPesos1.setBounds(10, 150, 50, 20);
    rMPesos1.setVisible(true);
    rCPesos1.setBounds(10, 200, 50, 20);
    rCPesos1.setVisible(true);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) Add components first, call `frame1.setVisible(true)` last, immediately after calling `pack()`. – Andrew Thompson Jan 09 '20 at 02:13

1 Answers1

1

Whenever I run this code the radio buttons display correctly until I tab in or hover into the window

This is because when you "tab in" or you hover over the radio buttons, you're triggering a call to repaint(), which is responsible for painting your GUI.

In this case the weird behavior is due to you having this line:

frame1.setVisible(true);

before you've added every other component to the JFrame. But that's only the beginning of your problems, as you'll eventually find other errors such as your GUI not being painted correctly on some other computers that are not the one you used to make the program, and that's because of the calls to setBounds(...) which suggests you're using null-layout which is evil and frowned uppon. Instead learn how to use and mix the different layout managers, or else you'll be in a situation similar to this one

Frakcool
  • 10,915
  • 9
  • 50
  • 89