• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

create dynamic checkboxes

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to create dynamic checkboxes as below. It works. But how do I check the status of each checkboxes. My exact doubt is how do I refer to dynamically created checkboxes. Please see my code below: I want to modify so that I can refer to each dynamically created checkboxes.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
Hi,
.. But how do I check the status of each checkboxes.



You got two options.
One is you maintain a List of the check boxes like the list of files.
The second way, which I personally prefer is asking the panel itself for the components.
Check out the getComponentCount and getComponentAt(int index) methods. You can use these two to iterate through all components in a container, check if it is of the type you want (JCheckBox) in this case, if yes, figure out if it is selected.

Hope this helps.
[ March 25, 2008: Message edited by: Maneesh Godbole ]
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Thanks and will try the second method. Is there any example?
My requirement is as below. I have generated one label and set of check boxes. So my dynamic panel would be having something as below:

_____________________________________________________________________

Label1 checkBox0 checkBox1 checkBox2 checkBox3
Label2 checkBox0 checkBox1 checkBox2 checkBox3 checkBox4
.
.
.
______________________________________________________________________

I just started checking how int component = dynamicPanel.getComponentCount(); works and it works perfectly.

I need two suggestions.

1. A best Layout as I am new to Swing
2. Any particular way of accessing each component.
Basically I am trying to collect the names of the labels in one array and selected checkBoxes in another array so that I can do file manupulation from there. ANy suggestions would be of immense help for me. Thanks.
[ March 25, 2008: Message edited by: Gopu Akraju ]
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
[QB]
I need two suggestions.

1. A best Layout as I am new to Swing

Based on the figure you have give, I would say the easiest one would be the GridLayout. It is really very easy to use. Just define the number of rows and columns. e.g panel.setLayout(new GridLayout(2,3)) splits the panel into a grid of two rows and three columns. After that you just keep on adding components and they are placed in subsequent "cells"

2. Any particular way of accessing each component.

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried as below.

1. It prints the correct number of component.
2. getClass method tells me about the type of coponent such as javax.swing.JLabel or javax.swing.JCheckBox
3. But I am not able to get their names? What would be the problem. How do I get their names? Thanks.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:

3. But I am not able to get their names? What would be the problem. How do I get their names? Thanks.



Both the JLabel and JCheckBox have a method called getText().

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, As I said earlier, I am creating my dynamic components separately and adding them into dynamic panel. Hence the following code is able to give the class of the component where as not the name of the label or checkBox.

When I am trying to access the panel and their components as I said above,
String name = dynamicPanel.getComponent(i).getName(); doesn't work and String name = dynamicPanel.getComponent(i).getText(); doesn't exist. Am bit confused too.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
Y Am bit confused too.



Its really quiet simple. Take a look at the code snippet I had posted before.
1) You check what type/class of component is it using the instanceof operator.
2) Then you explicitly typecast it to the known type by
3) Then you invoke the getText() on the checkbox
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Thanks for all the help. It works finally.

[ April 14, 2008: Message edited by: Gopu Akraju ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to make such programm like the one above - with dynamically added checkboxes, but my problem is that I don't know how to implement it in jFrame, automaticlally generted as NetBeans project with some other components, visually added from the IDE. My idea was to bound it with something like jPanel but no success (checkboxes never shows). Please, help!
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Are you using Netbeans for your GUI design. In that case, it uses cardlayout which you have to be comfortable to add the dynamically generated checkboxes. I did try with netbeans but couldn't succeed. Hence I designed GUI in an usual method, and start adding dynamically generated checkboxes into one panel and added into scrollable pane and into jFrame as below:
I add checkboxes into dPanel and then finally call to see the changes.
 
Petar Kanchev
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again, I tried that but I must missing something because it did't work again. Here is my fragment of code:

...
public Interface() throws IOException {
initComponents();
getContentPane().add(new JScrollPane(dPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
for (int i=0; i<5; i++){
JCheckBox box = new JCheckBox("region"+i);
dPanel.add(box);
dPanel.revalidate();
}
}
...

Can you give me a simple but whole example code so I can see all the picture? Thanks again!
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am wondering why it is not working for you. It is almost teh same code I have except that I have a string array numbers which consists of 1,2,3 etccc


I am able to see the panel with dynamically added check boxes.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there.
I want to create some dynamic checkboxes based on the content of two tables from my DB that looks like a pivot.

I was thinking before I red this topic about using a Vector<JPanel> for each line and one more for the labels above.
Each JPanel offcourse will have a Vector of components like described in the recent posts.
I am new to java swing and i don't know very good the layouts and how can I use them just in a single Panel in witout using the Vector of JPanels

Or I was thinking maybe to use a JTable but i'm not so good with them so i'll better stick with the panels
I will post some code to do this tonight.
What do you guys think?
Is there a better way to do this without using the Vector of JPanels?
If you can do this into a single panel and if that isn't to complicated to work with then please help

Cheers!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a single panel set as a GridLayout(6,4) would do it
 
Tanasoiu Alexandru
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mmmm... okay but what about the part where i have to know witch ones were checked...
And if I use just one panel... how can I do this then... ??
I have to go now, I'll read about GridLayouts when I get home and post some code and see what I am able to do.
thanks
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I am developing a desktop application in which I want Admin have option to delete users, for which I planned that whenever Admin clicks on 'delete users' button a new tab will open in which check boxes with the name of all existing users in my database should appear(so that he can delete multiple users simultaneously); so basically I need to generate dynamic check boxes as per my database.

I am using netbeans, jdk 1.6, sqlite3.

I have tried to follow the code above given by @Gopu Akraju but like @Peter Kanchar its not working for me too. What I does is just created new JFrame in netbeans and called a method inside constructor, method's code is as below:

public void generatingCheckboxesDynamically(){
JPanel dPanel = new JPanel();
add(dPanel);
getContentPane().add(new JScrollPane(dPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
for (int i=0; i<5; i++){
JCheckBox box = new JCheckBox("region"+i);
dPanel.add(box);
dPanel.revalidate();
}
}

Please help me to know where I am wrong!!
And yes this code is not connected to database yet, once it will work then I can modify it to work with database.

Also is there any other more better way to accomplish my task?
 
reply
    Bookmark Topic Watch Topic
  • New Topic