Jump to content

GUI problem, components not displaying?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Just started GUI programming with Java, here's the code I'm having a bit of trouble with:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package picturechanger;

import java.awt.Dimension;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;

/**
 *
 * @author 
 */
public class PictureChangerGui extends JFrame{

    private JPanel mainPanel;
    private JButton mainButton;
    private JLabel mainLabel; 

    public PictureChangerGui() {

        // Define the characteristics of the window
        this.setSize(200, 200);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("Picture Changer");

        // Create the components
        mainPanel = new JPanel(new GridLayout(2,1));
        mainButton = new JButton("Change Pictures");
        mainLabel = new JLabel();
        
        // Add listeners
        ButtonListener bl = new ButtonListener();
        mainButton.addActionListener(bl);

        // Add the components
        mainPanel.add(mainButton);
        mainPanel.add(mainLabel);

        this.getContentPane().add(mainPanel);

    }
        
    private class ButtonListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            ImageIcon icon1 = createImageIcon("face1.png", "");
            ImageIcon icon2 = createImageIcon("face2.png", "");
            ImageIcon icon3 = createImageIcon("face3.png", "");
            Random generator = new Random();
            for (int idx = 0; idx <= 1; ++idx){
                int randomInt = generator.nextInt(4);
                if (randomInt <= 1) {
                    mainLabel.setIcon(icon1);
                }
                else if (randomInt <= 2 && randomInt > 1) {
                    mainLabel.setIcon(icon2);
                }
                else {
                    mainLabel.setIcon(icon3);
                }

            }
        }
        
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
                                           String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}
}

This is suppossed to let the user click on a button and then, whenever they click it the image in the label is updated randomly with one of three images stored in files "face1.png" ... "face3.png".

This is code that started out as partial code by a lecturer which we had to complete in a Lab which is why it may seem slightly unreadable, he doesn't comment much.

The problem I have is, when I run the program (I have a main class that constructs a new GUI and runs it) the window appears but none of the components appear. Can anyone see something wrong with the code?

I'm working on a Mac with netbeans by the way, in case there are any known problems with that...

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
It works for me (by adding
 public static void main(String[] args) {
        PictureChangerGui gui = new PictureChangerGui();
    }
but you said you do that in another class so..
by working i mean
i see a button (big one, covering 50% of the window) and a label with a random picture if i click the button.

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Uhh... it's working here. You have to press the button for an image to show up, true, but nonetheless it works fine. O_o

All I did was copy the class you had, then put in a main function that did SwingUtilities.invokeLater(new Runnable(){public void run(){new PictureChangerGui();}}); and everything was peachy. You said none of the components appear, do you mean that the button itself doesn't even appear with the window?

@Oxano: Don't you use SwingUtilities.invokeLater() for GUI changes? :P
Wow I changed my sig!

#4
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Yeah, literally the button doesn't even appear...

Could you post your main method so I could try that, see if my lecturer's main method fails?

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
And you do enter the constructor if you debug?
-----------------------------------------------
just enter
 public static void main(String[] args) {
        PictureChangerGui gui = new PictureChangerGui();
    }
INSIDE your PictureChangerGui class, like an extra method.

#6
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Jesus christ. My lecturer made a typo somehow when saving some file names which messed everything up. Lucky I spotted it. The components now display, but when I click the button I get a message saying the files can't be found. I'm sure the files are in the correct place, the root directory of the project. Is this a problem with the code do you think?

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
@ZipOnTrousers: This actually depends entirely on where the class is physically located in the file system. Place the images in the same place as the .class files and Java should manage to find them, at least it does when I run Java in the command line. :P
Wow I changed my sig!

#8
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Thank you Zeke. I'm losing faith in the UK educational system. This lecturer doesn't have a clue...