Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Creating Dynamically Radio Buttons, Labels

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating Dynamically Radio Buttons, Labels

    Hi everybody,

    I am developing a desktop application which evaluates an excel file. In the excel file there are Course Titles, Course Codes etc. and my program has to list these cells with radiobuttons for selecting a course. The excel part has no problem, the program reads the data and put them in a string arrays.It puts every column into a different String array, so i have 6 string arrays (every one has about 600 records) . I pass them to GUI for printing, selecting etc.

    My problem starts here: I thought a solution like; Create jradiobuttons and jlabels with using index numbers of arrays, list them like an excel appearance. i need to create labels and radio buttons dynamically, because the excel file is not static, the information in it can change in time. I tried some ways to create Radiobuttons and Labels dynamically but i can't ! I used GroupLayout for creation. As a result, i need a solution or a new idea. Maybe i am thinking in a hard way, because i am a little newbie.

    I thought using tables but it seems very amateur to me.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Creating Dynamically Radio Buttons, Labels

    What have you got so far? What are the exact issues you're facing?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    The exact issue is "How to create radiobuttons and labels dynamically ?", i want to create radiobuttons and labels during the execution. But, i don't know how to do.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Creating Dynamically Radio Buttons, Labels

    Well what have you tried? Have you tried adding new components? Did you get any issues when attempting it?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    I am asking what is the logic behind creating labels and buttons during the execution time. I added my code, this is adding only one row, i need to add 600 more rows. But can't figure out how to do it.

    public void createListDynamically(String[] code, String[] title){
     
     
     
             for(int i=0;i<=code.length;i++){
     
                          String indexStr = String.valueOf(i);
     
                          jRadioList.add(new JRadioButton(""));
                          jLabelCode.add(new JLabel(code[i]));
                          jLabelTitle.add(new JLabel(title[i]));
     
                          jRadioList.get(i).setName(indexStr); //set name        
                          jLabelCode.get(i).setName(indexStr); //set name
                          jLabelTitle.get(i).setName(indexStr); //set name
     
                          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel1);
                            jPanel1.setLayout(layout);
     
                            layout.setAutoCreateGaps(true);
                            layout.setAutoCreateContainerGaps(true);
     
                            layout.setHorizontalGroup(layout.createSequentialGroup()
                                .addComponent(jRadioList.get(i))
                                .addComponent(jLabelCode.get(i))
                                .addComponent(jLabelTitle.get(i))
                           );
     
                            layout.setVerticalGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(jRadioList.get(i))
                                .addComponent(jLabelCode.get(i))
                                .addComponent(jLabelTitle.get(i)))
                            );
                      }

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Creating Dynamically Radio Buttons, Labels

    Well I can't really envisage what you're wanting to do, but what happens when you call revalidate() after adding all the components?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    Untitled.jpg

    What i want to do is in the image, but my code adds only first row.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    my code adds only first row.
    Can you show what your program creates?

    Add printlns in your code to show where and what is being executed. Maybe it only executes once.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    Ok, i will give a simple explanation: i want to create 700 jradiobuttons and 700 jlabels. The appearance must be like a given SS which i put my previous post. So i am asking to you how can i create 700 buttons ?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    how can i create 700 buttons ?
    use a loop:
    for(int i=0; i < 700; i++) {    // create 700 buttons
       JButton btn = new JButton();  // create new button
    }

    The appearance must be like a given SS
    Can you explain what SS is?

    If you want an answer that relates to your code, you will have to post your code.

  11. #11
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    SS means ScreenShot

    I almost solved the problem, but still need some changes. I am putting a screenshot and the code.

    Now the problem is horizontal gaps, i am studying on that, but still you can give an advice to me

    SS.jpg

    public void CreateListDynamically(String[] courseCodes, String[] courseTitles, String[] courseCredits, String[] courseOffto, String[] courseIstructors, String[] courseCampuses){
     
            System.out.println(courseCodes.length);
     
             for(int i=0;i<=600;i++){
     
                          String indexStr = String.valueOf(i);
     
                          jRadioLists.add(new JRadioButton(""));
                          jLabelCodes.add(new JLabel(courseCodes[i]));
                          jLabelTitles.add(new JLabel(courseTitles[i]));
                          jLabelCredits.add(new JLabel(courseCredits[i]));
                          jLabelOffo.add(new JLabel(courseOffto[i]));
                          jLabelInsructors.add(new JLabel(courseIstructors[i]));
                          jLabelCampuses.add(new JLabel(courseCampuses[i]));
     
                          jRadioLists.get(i).setName(indexStr); //set name        
                          jLabelCodes.get(i).setName(indexStr); 
                          jLabelTitles.get(i).setName(indexStr); 
                          jLabelCredits.get(i).setName(indexStr); 
                          jLabelOffo.get(i).setName(indexStr); 
                          jLabelInsructors.get(i).setName(indexStr); 
                          jLabelCampuses.get(i).setName(indexStr); 
     
     
                      }
     
              jPanel1.setLayout(new GridLayout(0, 7, 18, 1));
     
              for (int i = 0; i < 600; i++)  
              {
                jPanel1.add(jRadioLists.get(i));    
                jPanel1.add(jLabelCodes.get(i));  
                jPanel1.add(jLabelTitles.get(i));
                jPanel1.add(jLabelCredits.get(i));
                jPanel1.add(jLabelOffo.get(i));
                jPanel1.add(jLabelInsructors.get(i));
                jPanel1.add(jLabelCampuses.get(i));
     
                buttonGroup.add(jRadioLists.get(i)); //set button group
              } 
     
              jPanel1.setBorder(BorderFactory.createTitledBorder("Ders Listesi"));
     
        }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    Now the problem is horizontal gaps
    Where are the gaps?

  13. #13
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    There are too much space beteween columns, i am trying to set 18 px but not effecting to GUI.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    Have you tried setting the sizes of the components you are using to build the GUI?

  15. #15
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    Quote Originally Posted by Norm View Post
    Have you tried setting the sizes of the components you are using to build the GUI?
    I thought that but grid layout is not allowed. So i will use gridbag layout, it is more complex and more flexible as read from an article. When i changed the code i will post again...Btw thank you for your attention.

  16. #16
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Dynamically Radio Buttons, Labels

    ds.jpg

    With gridbag layout it is easy to make a mistake/bug, it is so complex. So again i decided to use simple gird layout. I addedd 7 more panels to my main panel, and put the data into them seperately. I can arrange the horizontal gaps between columns by hand with netbeans. The problem is solved.

Similar Threads

  1. labels,textfields,buttons
    By balu424525 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 26th, 2011, 07:44 AM
  2. Dynamically Creating User Selection through ResultSet
    By anmaston in forum Java Theory & Questions
    Replies: 3
    Last Post: April 10th, 2011, 11:08 AM
  3. Help with radio buttons and results.
    By Bewitched1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 3rd, 2011, 03:01 PM
  4. radio buttons stop working
    By tabutcher in forum AWT / Java Swing
    Replies: 2
    Last Post: March 5th, 2010, 08:28 AM
  5. [SOLVED] Using html Radio Buttons with Servlets
    By oss_2008 in forum Java Servlet
    Replies: 2
    Last Post: June 25th, 2009, 05:39 AM

Tags for this Thread