Results 1 to 13 of 13
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default JLabel Array not showing in JFrame

    I am trying to add a row of JLabels to a GridLayout using the following code, but nothing shows up when i run it. I get no exceptions and the code compiles fine:

    Java Code:
    OneColumnWindow availableSeatsWindow = new OneColumnWindow(300,100);
                    JPanel aPanel = new JPanel(new GridLayout(1, A.length));
                    JLabel[] labelA = new JLabel[A.length];
                    int index = 0;
                    while (index < A.length){
                        labelA[index] = new JLabel();
                        index++;
                    }
                    index = 0;
                    while (index < labelA.length){
                        labelA[index].setText("A" + index+1);
                        if (A[index].isBookedFriday){
                            labelA[index].setBackground(Color.red);
                            labelA[index].setText(labelA[index].getText() + A[index].allocatedToFriday);
                        }
                        else{
                            labelA[index].setBackground(Color.green);
                        }
                        aPanel.add(labelA[index]);
                        index++;
                    }
    OneColumnWindow is merely a JFrame with a JPanel called bodyPanel with the GridLayout.


    Explanation of the code:
    I create a JPanel with GridLayout to insert into the JFrame
    I create an array of JLabels of same length
    I initialise each of the JLabels
    In a while statement, I add text and colour to the appropriate JLabels and add them from 0 to maximum to the JPanel

    But nothing shows up when I run it.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JLabel Array not showing in JFrame

    What is A? Are you certain A is non-zero in length?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: JLabel Array not showing in JFrame

    oooops, forgot to explain A.

    A is an array of Seat objects (I created the Seat class, it is merely a class with 6 basic variables in it).

    A is definitely not zero in length, it is actually 14 in length.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JLabel Array not showing in JFrame

    It is hard go guess what is going on without all the code (A minimal compilable version). When you say nothing shows up are you talking about no frame? An empty frame? Did you setVisible(true) in the frame? Did you repaint after you initialized the various containers?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: JLabel Array not showing in JFrame

    Quote Originally Posted by jim829 View Post
    It is hard go guess what is going on without all the code (A minimal compilable version). When you say nothing shows up are you talking about no frame? An empty frame? Did you setVisible(true) in the frame? Did you repaint after you initialized the various containers?

    Regards,
    Jim
    I understand that it can be difficult to understand as you are not familiar with my program. I have made a short compilable version which should work similarly. Once again I have the exact same issue. The JLabels are not showing up at all.

    Please note, the array is not a String array in my real program and many of the other things have been changed just to make this compilable. But the concept is the same.

    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Test {
        
        static String[] A;
        
        public static void main(String args[]){
            
            A = new String[5];
            A[0] = "0";
            A[1] = "1";
            A[2] = "2";
            A[3] = "3";
            A[4] = "4";
                 
            
            JFrame availableSeatsWindow = new JFrame();
                    JPanel aPanel = new JPanel(new GridLayout(1, A.length));
                    JLabel[] labelA = new JLabel[A.length];
                    int index = 0;
                    while (index < A.length){
                        labelA[index] = new JLabel();
                        index++;
                    }
                    index = 0;
                    while (index < labelA.length){
                        labelA[index].setText("A" + index+1);
                        if (A[index].equals("1") || A[index].equals("4")){
                            labelA[index].setBackground(Color.red);
                            labelA[index].setText(labelA[index].getText());
                        }
                        else{
                            labelA[index].setBackground(Color.green);
                        }
                        aPanel.add(labelA[index]);
                        index++;
                    }
                    
             availableSeatsWindow.setVisible(true);
        }
    }
    Last edited by kkid; 04-12-2013 at 09:23 PM.

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: JLabel Array not showing in JFrame

    This should produce a row of 5 JLabels, each with text in them which is the same as the String value. If the value inside the label is 1 or 4, the background should be red, otherwise it should be green.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JLabel Array not showing in JFrame

    The main problem was you didn't add the JPanel to the frame. I added the following:

    Java Code:
    JFrame availableSeatsWindow = new JFrame();  // yours 
     availableSeatsWindow.setPreferredSize(new Dimension(500, 500));  //mine
     // rest of your code
    
    //  following are last three lines of test
          availableSeatsWindow.add(aPanel); //mine
          availableSeatsWindow.pack(); //mine 
          availableSeatsWindow.setVisible(true);  //yours
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JLabel Array not showing in JFrame

    And just in case. The background colors won't show up unless you setOpaque(true) on each label.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: JLabel Array not showing in JFrame

    oh, well I feel silly

    Thanks


    Why doesn't the background colour thing work?
    I've cleaned it up a little for the example to make it more obvious. When it is 1 or 4, it should be red, otherwise it should be green.

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Test {
        
        static String[] A;
        
        public static void main(String args[]){
            
            A = new String[5];
            A[0] = "0";
            A[1] = "1";
            A[2] = "2";
            A[3] = "3";
            A[4] = "4";
                 
            
            JFrame availableSeatsWindow = new JFrame();
            availableSeatsWindow.setPreferredSize(new Dimension(500, 500));
                    JPanel aPanel = new JPanel(new GridLayout(1, A.length));
                    JLabel[] labelA = new JLabel[A.length];
                    int index = 0;
                    while (index < A.length){
                        labelA[index] = new JLabel();
                        index++;
                    }
                    index = 0;
                    while (index < labelA.length){
                        labelA[index].setText(A[index]);
                        if (A[index].equals("1") || A[index].equals("4")){
                            labelA[index].setBackground(Color.red);
                        }
                        else{
                            labelA[index].setBackground(Color.green);
                        }
                        aPanel.add(labelA[index]);
                        index++;
                    }
             availableSeatsWindow.add(aPanel);
             availableSeatsWindow.pack();
             availableSeatsWindow.setVisible(true);
        }
    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JLabel Array not showing in JFrame

    I anticpated your question and already responded. setOpaque(true) on each label.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: JLabel Array not showing in JFrame

    Quote Originally Posted by jim829 View Post
    I anticpated your question and already responded. setOpaque(true) on each label.

    Regards,
    Jim
    Ah, I think we posted at the same time.

    Thanks, works perfectly now. You really are a Java wizard :)

  12. #12
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: JLabel Array not showing in JFrame

    It's only fair that I show you the end result:


  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JLabel Array not showing in JFrame

    Glad I was able to help.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Make a JLabel (and obviously how to add it to a JFrame...)
    By watachiaieto in forum AWT / Swing
    Replies: 6
    Last Post: 05-23-2012, 09:18 AM
  2. Replies: 5
    Last Post: 12-04-2010, 12:30 AM
  3. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  4. [SOLVED] JLabel not showing on JPanel
    By onefootswill in forum New To Java
    Replies: 11
    Last Post: 08-23-2008, 01:32 PM
  5. help me with JFrame and JLabel
    By michcio in forum New To Java
    Replies: 5
    Last Post: 11-20-2007, 07:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •