1

I am trying to get pictures to appear on a JPanel and have previously tried using JLabels but that didn't work so now I am trying to use the paintComponent method. My code consists of making a window with a frame and adding a JPanel to the frame. Then in my actionPerformed method called by using a timer calls repaint I don't receive output from the System.out.println method. Any way I can get this working?

public void createWindow(){

    frame.add(panel);
    frame.addComponentListener(this);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setSize(xSize, ySize);
    frame.setLocation(0, 0);

    }

@Override                 
public void paintComponent(Graphics g) {

    System.out.println("Method Called");
    super.paintComponent(g);
    g.drawString("Code has painted", 10, 100);

    }
  • 2
    `have previously tried using JLabels but that didn't work` - a JLabel is the simplest component in Swing. There is no reason you can't use an ImageIcon with a JLabel. You should learn how to do this first BEFORE you do custom painting. Don't reinvent the wheel!!! In fact I showed you how to do this 3 weeks ago: http://stackoverflow.com/a/18927663/131872. As well the Swing tutorial has plenty of examples. – camickr Oct 11 '13 at 16:43
  • 1
    And please make calls like `frame.setVisible(true)` at the end, once all the components have been added to the container, and the container has realized it's size fully. – nIcE cOw Oct 11 '13 at 16:44
  • For the reason I didn't do JLabels it was because it wouldn't let me resize or relocate any labels –  Oct 11 '13 at 16:53
  • 1
    @CurtisFloras: the layout managers usually handle that for you, unless you're talking about resizing the images as well. – Hovercraft Full Of Eels Oct 11 '13 at 17:02
  • @CurtisFloras, `it wouldn't let me resize or relocate any labels` and once again I gave you a solution in your posting: http://stackoverflow.com/q/19170601/131872. You should not be managing the location of the component, that is the job of the layout manager. There are other solutions as well. For example you can add an EmptyBorder to the JLabel to determine the postion of the icon in the label. – camickr Oct 11 '13 at 19:39

1 Answers1

2

Your code doesn't show us the problem other than that you're not adding this to the JFrame. For the paintComponent method to be called, the object that holds the method must be added to the GUI, it must be visible. Your code doesn't show this.

In other words, change this:

public void createWindow(){
    frame.add(panel);  // what is panel? do you override *its* paintComponent?
    frame.addComponentListener(this);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setSize(xSize, ySize);
    frame.setLocation(0, 0);
}

to this:

public void createWindow(){
    frame.add(this);  // ******  Here you go ****
    frame.addComponentListener(this);  // Not sure what this is for
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setVisible(true);
    // frame.setSize(xSize, ySize); // *** avoid this guy
    frame.setLocation(0, 0);
}

Also you state:

I am trying to get pictures to appear on a JPanel and have previously tried using JLabels but that didn't work

But using a JLabel should work just fine and is usually the easier way to do this, especially if the image doesn't have to re-size. Consider showing us this code attempt.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373