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 2 of 2

Thread: Java Netbeans Swing - want to create x jlabels by entering a number

  1. #1
    Junior Member
    Join Date
    Mar 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Java Netbeans Swing - want to create x jlabels by entering a number

    Hello guys,

    I have one iFrame where I can enter a number x. After that, it creates a new JDialog. In this Jdialog, I want to create x JPanels in a matrix, but it does not work. This is my code:


    public TeilnehmerDialog(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
            myViewController = new ViewController();
     
           int eingabe = myViewController.getTeilnehmer();
           String test = Integer.toString(eingabe);
           String prnr = "Prüfungsnummer: "; 
           String name = "Name: ";
     
     
     
           JLabel[] jlabelsprnr = new JLabel[eingabe]; 
     
           for (int i = 0; i < eingabe; i++) {
               jlabelsprnr[i] = new JLabel(prnr);
           }
     
     
     
           int GRID_ROWS = 2; 
           int GRID_COLS = 2; 
     
     
           GridBagLayout gbl = new GridBagLayout(); 
            setLayout(gbl); 
            GridBagConstraints gbc = new GridBagConstraints(); 
            gbc.weightx = 0.2; 
            gbc.weighty = 0.25; 
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(5,5,5,5); //space that a container must levea at ech of its edges (to the border)
            gbc.gridwidth = 1; 
            gbc.gridheight = 1;  
     
            int index = 0; 
     
     
                      for (int i = index; i < eingabe; i++) {   
     
                          for (int row = 0; row < GRID_ROWS; row++) {
     
                               for (int col = 0; col < GRID_COLS;  col++) {
     
                                  gbl.setConstraints(jlabelsprnr[index], gbc);
                                  add(jlabelsprnr[index]);
                                  jlabelsprnr[index].setVisible(true);
                                  System.out.println(jlabelsprnr[index].getText());
     
                        //   index++;
                            } 
                        }
                    }
    }

    and this is my output:

    run:
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer: 
    Prüfungsnummer:
    for entering number 4!

    Thank you

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Netbeans Swing - want to create x jlabels by entering a number

    How can the code be compiled and executed for testing? I do not see a main method that is needed to execute a java program.

    Is there anything wrong with the program's output? Please explain.

    it does not work
    Please explain what the program does and what you want to change about how it executes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to use Offline Navigable Map in Netbeans IDE with Java Swing
    By Seyit in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 16th, 2021, 11:15 AM
  2. How to Print JFrame content using NetBeans Java Swing
    By vk020 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 12th, 2014, 04:25 AM
  3. Change JAVA Swing GUI Window in NetBeans
    By tazeunite00 in forum Java IDEs
    Replies: 4
    Last Post: August 29th, 2014, 02:12 AM
  4. Change JAVA Swing GUI Window in NetBeans
    By tazeunite00 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2014, 12:58 AM
  5. Problem with java swing JButton in Netbeans IDE
    By vrp in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 12th, 2011, 11:38 PM