2

I'm having an issue where my JRadioButtons will not appear until moused over. I've looked up this issue before and it seems most people have solved it using layouts. However, I am not supposed to use them for my assignment, being completely new to graphics with Java I'm really struggling. Any ideas? Anything is appreciated.

import java.awt.GridLayout;

import javax.swing.*;
public class InsuarancePolicyApp {

public static void main(String[] args) {

//JFrame
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(800, 600);
f.setLayout(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Insurance Policy Application");

//JButtons
JButton addClient = new JButton("Add Client");
JButton openPolicy = new JButton("Open Policy");
f.getContentPane().add(addClient);
f.getContentPane().add(openPolicy);
openPolicy.setSize(300, 60);
addClient.setSize(380,60);
addClient.setLocation(30, 180);
openPolicy.setLocation(450, 180);

//JTextField
JTextField name = new JTextField("Name: ");
f.getContentPane().add(name);
name.setLocation(30,30);
name.setSize(380, 30);

//JList
String[] clientarray = {"Mark Mywords", "Jim Class", "Stan Dupp", "Mel Onhead", "Bob's Yoyo Shop", "Toys Aren't Us", "The Fish Rack"};
JList clients = new JList(clientarray);
f.getContentPane().add(clients);
clients.setLocation(30, 280);
clients.setSize(380, 250);

String[] policyarray = {"Policy 002354","Policy 005345", "Depreciable Policy 0789423", "Expirable Policy 009724"};
JList policies = new JList(policyarray);
f.getContentPane().add(policies);
policies.setLocation(450, 280);
policies.setSize(300, 250);


//JScrollPane
JScrollPane clientScroll = new JScrollPane(clients, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(clientScroll);
clientScroll.setLocation(30,280); 
clientScroll.setSize(380,250);


JScrollPane policiesScroll = new JScrollPane(policies, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(policiesScroll);
policiesScroll.setLocation(450,280); 
policiesScroll.setSize(300,250);

//JradioButons 
JRadioButton b1 = new JRadioButton("Company",false);
JRadioButton b2 = new JRadioButton("Individual",false);
JRadioButton b3 = new JRadioButton("Standard",false);
JRadioButton b4 = new JRadioButton("Depreciable",false);
JRadioButton b5 = new JRadioButton("Expirable",false);

//ButtonGroups
ButtonGroup clientsGroup = new ButtonGroup();
b1.setLocation(120, 70);
b1.setSize(100,30);
b2.setLocation(260, 70);
b2.setSize(100,30);
f.getContentPane().add(b2);
f.getContentPane().add(b1);
clientsGroup.add(b1);
clientsGroup.add(b2);


ButtonGroup policiesGroup = new ButtonGroup();
b3.setLocation(600, 10);
b4.setLocation(600, 60);
b5.setLocation(600, 110);
b3.setSize(100,60);
b4.setSize(100,60);
b5.setSize(100,60);
f.getContentPane().add(b3);
f.getContentPane().add(b4);
f.getContentPane().add(b5);
policiesGroup.add(b3);
policiesGroup.add(b4);
policiesGroup.add(b5);

//Jlabel
JLabel label = new JLabel("Type:");
f.getContentPane().add(label);
label.setLocation(30, 55);
label.setSize(60,60);

JLabel label2 = new JLabel("Type:");
f.getContentPane().add(label2);
label2.setLocation(545, 10);
label2.setSize(60,60);
}
}
David M
  • 4,325
  • 2
  • 28
  • 40
  • 1
    By your use of the word 'assignment' I assume you are in a class and someone is teaching you, however, the design is horrendous. You don't run all your code in the main method, because you can't access fields. Putting code in the main method requires you to put ALL of your code in the main method which is obviously bad design, especially in an object-oriented language. – Jimmt Feb 13 '13 at 20:30

1 Answers1

4

The problem is that the JRadioButtons are not being painted as the JFrame has already been displayed when they are added.

You need to call JFrame#setVisible after you add all the components to the container:

f.setVisible(true);

Aside: Don't use absolute positioning (null layout), use a layout manager instead. Using a layout manager removes the need for the setting component sizes & positions.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I'm pretty sure there's no difference before/after. It's either you add it, and then you see it, or you see it, and then you add it, but either way it happens too fast to be noticed. – Jimmt Feb 13 '13 at 20:32
  • @Jimmt be sure that answer to this question – mKorbel Feb 13 '13 at 20:35
  • his problem is that the JRadioButtons don't show up, isn't it? – Jimmt Feb 13 '13 at 20:36
  • Thanks putting f.setVisible(true); at the the end worked perfectly. and I know I should be using layouts it would make my life a lot easier but, my prof doesn't want us using them to my understanding. – user2069838 Feb 13 '13 at 20:36
  • 3
    @Jimmt This **is** the problem here. Why the downvote? Have you tested this? – Reimeus Feb 13 '13 at 20:37
  • I tested it, yeah, setVisible before and after. – Jimmt Feb 13 '13 at 20:37
  • 3
    +1 reimeus... @Jimmt it makes a big difference adding components to a visible container requires `revalidate()` and `repaint()` to be called on the container after components are added to reflect the changes. – David Kroukamp Feb 13 '13 at 20:45
  • 3
    @user2069838 *"my prof doesn't want us using them"* - get a new professor. IMHO, layout managers are one of the most powerful and useful elements of the Swing library and in this case, there's more reason to use them not - IMHO – MadProgrammer Feb 13 '13 at 20:57
  • still don't really understand as it makes no difference when I test it, but alright... – Jimmt Feb 14 '13 at 04:49