The Artima Developer Community
Sponsored Link

Java Answers Forum
Access to elements of a panel

1 reply on 1 page. Most recent reply: Mar 7, 2006 10:38 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

Access to elements of a panel Posted: Mar 7, 2006 8:28 AM
Reply to this message Reply
Advertisement
Hi everyone,
i have created a panel within an scroll pane to contain some checkboxes depending on a String [] passed as parameter. The creation is easy and i show how i did it as follows, but i am not able to access to one of this checkboxes in other function which has to set selected them. I have tryed with getContentPane and similar but no achievement. I have to get access to this checkboxes to select them and get their names/ text.
Any suggestion?? Thanks

param is an String[], e.g., String [] param = new String {"one","zwei","tres"}
<code>
Box box = Box.createVerticalBox();
JScrollPane pScroll = new JScrollPane(box, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_N EEDED);
//Titel of the panel of the isotopes molecules
JLabel label = new JLabel("Test events");
//It should be done only if there is any isotope of teh molecule
if (parameter.length != 0){
// Create group of radio buttons
ButtonGroup group = new ButtonGroup();
//create the checkboxes for the isotopes and add them to the box
for (int i=0;i< parameter.length;i++)
{
if(parameter!= null){
JRadioButton rb = new JRadioButton(parameter);
box.add(rb);
group.add(rb);
}
//System.out.println(par ameter);
}
}
//Add elements to the panel
cPanel.add(label, BorderLayout.NORTH);
//Add the scrollbar in case there were too many isotopes
JScrollPane scrollPaneForListBox = new JScrollPane(box);
collisionPanel.add(scrollPaneForListBox, BorderLayout.CENTER);
//set minimum and maxim size
cPanel.setMinimumSize(new Dimension(300,250));
cPanel.setPreferredSize(new Dimension(300,250));
cPanel.setMaximumSize(new Dimension(300,250));
// Add some breathing room...
cPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
return cPanel;

//create the checkboxes for the isotopes and add them to the box
<endcode>

<code>
// Handle action events from all the buttons.
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

//Handle the New window button.
if (ACCEPT_CONFIGURATION.equals(command)) {
//activate the mexec command
for (int i=0;i< isotopoPanel.getComponentCount();i++)
{
System.out.println(isotopoPanel.getComp onents().getClass());
}
System.out.println(isotopoPanel.getComponent(1));
JCompo nent aux = (JComponent) isotopoPanel.getComponent(1);
//aux.getComponents();
for (int i=0;i< aux.getComponentCount();i++)
{
System.out.println(aux.getComponents().getClass( ));
}

System.out.println(isotopoPanel.getComponent(1));
aux = (JComponent) aux.getComponent(1);
for (int i=0;i< aux.getComponentCount();i++)
{
System.out.println(aux.getComponents().getClass( ));
}

}
<endcode>


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Access to elements of a panel Posted: Mar 7, 2006 10:38 PM
Reply to this message Reply
1. reformat your code using the java tags as shown on the right of the input field when oposting here.

2. getComponents() returns all components contained in a JPanel. instanceof should help you to find out if the component is one of your checkboxes.

3. Better way to do this: When creating the boxes, don't just put them on the panel, but add the malso to a a list.
Access them later using this list.

Flat View: This topic has 1 reply on 1 page
Topic: JVMs current Heap Size and Thread Pool size Previous Topic   Next Topic Topic: can someone do this for me?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use