user3073234
user3073234

Reputation: 771

Java Swing JLabel not appearing

The JLabel nameLabel will not appear on the gui. Ive tried to use a SwingWorker so concurrency isnt an issue. When the Jlabel is added to the West section of the BorderLayout of the internal JPanel the panel makes room for the label but the label doesnt actually appear. If anyone has had this problem or knows how to fix it i would be very appreciative

thanks

public class Frame_2 
{
    JButton done = new JButton("Next Page");
    JLabel topLabel = new JLabel("2. Contact Information");

    JTextField nameInput = new JTextField();
    JTextField mailingAddressInput = new JTextField();
    JTextField cityStateInput = new JTextField();
    JTextField telephoneInput = new JTextField();
    JTextField faxInput = new JTextField();
    JTextField eMailInput = new JTextField();

    JLabel nameLabel = new JLabel("Name:");

    JPanel internal = new JPanel();
    JPanel northGrid = new JPanel();


    int keepTrack = 0;

    String name;
    String mailingAddress;
    String cityState;
    String telephone;
    String fax;
    String eMail;

    public void buildFrame_2(JFrame frame)
    {
        nameLabel.setText("Name:");
        nameInput.setText("Name:");
        mailingAddressInput.setText("Mailing Address:");
        cityStateInput.setText("City, State, Zip Code:");
        telephoneInput.setText("Telephone:");
        faxInput.setText("Fax:");
        eMailInput.setText("E-mail:");
        internal.setLayout(new BorderLayout());
        topLabel.setHorizontalAlignment(SwingConstants.CENTER);
        frame.setLayout(new BorderLayout());
        northGrid.setLayout(new GridLayout(12,2));

        nameInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    name = nameInput.getText();
                    System.out.println(name);
                    nameInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    


        mailingAddressInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    mailingAddress = mailingAddressInput.getText();
                    System.out.println(mailingAddress);
                    mailingAddressInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));        
        cityStateInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    cityState = cityStateInput.getText();
                    System.out.println(cityState);
                    cityStateInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    
        telephoneInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    telephone = telephoneInput.getText();
                    System.out.println(telephone);
                    telephoneInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    
        faxInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    fax = faxInput.getText();
                    System.out.println(fax);
                    faxInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    
        eMailInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    eMail = eMailInput.getText();
                    System.out.println(eMail);
                    eMailInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    

        northGrid.add(nameInput);
        northGrid.add(mailingAddressInput);
        northGrid.add(cityStateInput);
        northGrid.add(telephoneInput);
        northGrid.add(faxInput);
        northGrid.add(eMailInput);

        internal.add(nameLabel, BorderLayout.WEST);

        //internal.add(northGrid, BorderLayout.CENTER);



        frame.add(internal, BorderLayout.CENTER);
        frame.add(done, BorderLayout.SOUTH);
        frame.add(topLabel, BorderLayout.NORTH);

    }

Upvotes: 0

Views: 1010

Answers (1)

Hollis Waite
Hollis Waite

Reputation: 1079

Frame with visible label

Using the below code, label shows up fine for me.

JFrame f = new JFrame();
Frame_2 f2 = new Frame_2();
f2.buildFrame_2(f);
f.pack();
f.setVisible(true);

However, if the pack() call is removed, the frame defaults to its minimum size so that contents cannot be seen.

Unpacked frame

My guess is that you just need to resize frame. However, you really ought to provide an SSCCE if you want more accurate answers.

Upvotes: 1

Related Questions