Reputation: 385
i created a component which extends a JDialog. It creates a JPanel (with a big size) inside a JScrollPane. Then i add RadioButtons and JLabels to the JPanel
two problems : 1 - the RadioButtons and JLabels don't show. 2 - the scrollBars of the JScrollPane don't show
here is my code :
public class xuggleJOptionPane1 extends JDialog{
Container pane;
JPanel paneMain;
JLabel ms1;
xuggleJOptionPane1 myFrame;
JPanel panel;
JScrollPane paneScroll;
JPanel paneScrollpanel;
public xuggleJOptionPane1(JFrame parent, String str, int nf)
{
super(parent, str);
myFrame = this;
myFrame.setPreferredSize(new Dimension(400, 160));
myFrame.setSize(new Dimension(400, 160));
panel = new JPanel();
panel.setSize(400, 160);
ms1 = new JLabel();
paneScroll = new JScrollPane();
paneScroll.setPreferredSize(new Dimension(380,100));
paneScrollpanel = new JPanel();
paneScrollpanel.setPreferredSize(new Dimension(1600, 1600));
//if i add this line the whole paneScrollpanel disappears
//paneScrollpanel.setSize(1600, 1600);
String pl ="";
if (nf != 1) pl = "es";
String s1 = "We found " + nf + " flux"+pl+". Which one do you wanna choose ?";
ms1.setText(s1);
ArrayList<JRadioButton> Buttons = new ArrayList<JRadioButton>();
ArrayList<JLabel> Labels = new ArrayList<JLabel>();
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < nf; i++)
{
Buttons.add(new JRadioButton());
Labels.add(new JLabel("test"));
paneScrollpanel.add(Buttons.get(i));
paneScrollpanel.add(Labels.get(i));
group.add(Buttons.get(i));
}
Buttons.get(0).setSelected(true);
paneScroll.add(paneScrollpanel);
panel.add(ms1);
panel.add(paneScroll);
myFrame.add(panel);
myFrame.setVisible(true);
myFrame.setResizable(false);
}
}
Upvotes: 1
Views: 818
Reputation: 347194
Change
paneScroll.add(paneScrollpanel);
to
paneScroll.setViewportView(paneScrollpanel);
While I'm here...
This is kinda pointless (IMHO)
myFrame = this;
myFrame.setPreferredSize(new Dimension(400, 160));
myFrame.setSize(new Dimension(400, 160));
You could use
this.setSize(new Dimension(400, 160));
or
setSize(new Dimension(400, 160));
This is kind of pointless
panel.setSize(400, 160);
As the layout manager will make it's own decisions about what size to make the panel
You'd probably also like to look into pack
Of, on a personal note, I really, really dislike frames that aren't resizable :P
Upvotes: 4