Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2005

Tip: Looking for answers? Try searching our database.

A JList with JRadioButtonMenuItems

Thread view: 
KrishRads Venkat - 17 May 2005 19:19 GMT
Hi,
I am trying to create a menu which has RadiobuttonMenuItems displayed
dynamically.I am doing this with a JList,JScrollPane and a Vector.I am able
to have a menu of hardcoded values but not one with RadiobuttonMenuItems
created Dynamically.Any help will be more than appreciated.Foll is the code:

public class temp1 extends JFrame{

public temp1(){
super("Scroll Test");
Vector myVector = new Vector();
JPanel contentPane = new JPanel(new BorderLayout());
DefaultListModel model;
model = new DefaultListModel();

for(int j = 0; j < 40; j++)
myVector.add(new String(j + ""));
//myVector.add(new JMenuItem("Hello"));
JList theList = new JList(myVector);
JScrollPane sp = new JScrollPane(theList);
JPanel p = new JPanel(new BorderLayout());
p.setPreferredSize(new Dimension(100, 100));
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Scroll");
p.add(sp, BorderLayout.CENTER);
menu.add(p);
menu.add(new JMenuItem("Hello"));
menuBar.add(menu);
this.setJMenuBar(menuBar);

this.setContentPane(contentPane);
this.setSize(300, 300);
}

public static void main(String [] args){
JFrame j = new temp1();
j.show();
}
}
Signature

Thanks
Krishnan

Arnaud Berger - 18 May 2005 08:27 GMT
Hi,

You may do it this way .

Please note that this example ran fine with JRE 1.4 , but quite strangely
with 1.5 (i.e, couldn't select a radiobutton with the mouse, only with the
space key / scrolling was very very hard).

Well if anyone knows the reason for such a difference, it would be great.

public class temp1 extends JFrame {

public temp1() {

super("Scroll Test");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout());
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
ButtonGroup group=new ButtonGroup();

for (int j = 0; j < 40; j++){
JRadioButton button=new JRadioButton(new String(j + ""));
p.add(button);
group.add(button);
button.setSelected(true);
}

JScrollPane sp = new JScrollPane(p);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Scroll");
sp.setPreferredSize(new Dimension(100, 100));
menu.add(sp);
menu.add(new JMenuItem("Hello"));
menuBar.add(menu);
this.setJMenuBar(menuBar);
this.setSize(300, 300);
}

public static void main(String[] args) {
JFrame j = new temp1();
j.show();
}
}

Regards,

Arnaud

> Hi,
> I am trying to create a menu which has RadiobuttonMenuItems displayed
[quoted text clipped - 38 lines]
> Thanks
> Krishnan
KrishRads Venkat - 18 May 2005 10:42 GMT
Thanks Arnaud for your speedy response.Yeah the scrolable radio buttons
worked fine.But what I was trying to have are JRadiobuttonMenuItems not
JRadioButtons.I tried replacing the latter with the former.The butons were
being displayed alright but the window used to shut as soon as I tried to
scroll.I wonder if we could JRadioButtonMenuItems directly to the panel
rather than to a JMenu.

Please advise.

Thanks once again.
Arnaud Berger - 18 May 2005 10:53 GMT
Menuitems are probably meant to be added to menus only.
I couldn't say how predictable their behavior may be if added to anything
else.

Why would you absolutely need JRadioButtonMenuItems  rather than
JRadioButtons ?

Regards,

Arnaud

> Thanks Arnaud for your speedy response.Yeah the scrolable radio buttons
> worked fine.But what I was trying to have are JRadiobuttonMenuItems not
[quoted text clipped - 10 lines]
> Message posted via JavaKB.com
> http://www.javakb.com/Uwe/Forums.aspx/java-gui/200505/1


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.