3

Why My JComponent is not displayed on top of backgound JFrame?

Please check the following code:

class CounterFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private MyPanel myComponent = new MyPanel();
    private JLabel contentPane = new JLabel(new ImageIcon(getClass()
            .getResource("background/2.jpg")));

    CounterFrame() {
        contentPane.setLayout(new GridBagLayout());
        setContentPane(contentPane);
        add(myComponent);
    }

    }

    class MyPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Font myFont;
    private String target;
    private String raised = "200000";
    private Image background;

    public MyPanel() {

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D twoD = (Graphics2D) g;

        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        twoD.setRenderingHints(rh);

        File f = new File("fonts/event.ttf");
        try {
            myFont = Font.createFont(Font.TRUETYPE_FONT, f);
            myFont = myFont.deriveFont(90f);
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        twoD.setColor(Color.BLACK);
        twoD.setFont(myFont);

        twoD.drawString(raised,5, 90);
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
skystar7
  • 4,419
  • 11
  • 38
  • 41
  • Why exactly you want to add a `JPanel` to a 'JLabel', why not vice-versa ? – nIcE cOw Apr 13 '12 at 07:00
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) `File f = new File("fonts/event.ttf");` a) Don't load resources in the paint methods! b) If this is is an embedded application resource, it should be accessed by URL. – Andrew Thompson Apr 13 '12 at 07:43
  • @nIcEcOw I have a short source which might demonstrate one (trivial) use for it. Ask the question and notify me, and I'll add it as an answer. :) – Andrew Thompson Apr 13 '12 at 11:08
  • @AndrewThompson : Ahha just did that, waiting for some helpful thoughts on this :-) – nIcE cOw Apr 13 '12 at 12:27

2 Answers2

6

Seems to work fine here (in this SSCCE variant of the code).

Working code

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class CounterFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private MyPanel myComponent = new MyPanel();
    private JLabel contentPane;

    CounterFrame() {
        try {
            URL url = new URL("http://pscode.org/media/stromlo2.jpg");
            contentPane = new JLabel(new ImageIcon(url));
        } catch(Throwable t) {
            t.printStackTrace();
        }
        contentPane.setLayout(new GridBagLayout());
        setContentPane(contentPane);
        add(myComponent);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                CounterFrame rc = new CounterFrame();
                rc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                rc.pack();
                rc.setVisible(true);
            }

        });
    }
    }

    class MyPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private String target;
    private String raised = "200000";
    private Image background;

    public MyPanel() {
        setPreferredSize(new Dimension(200,100));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D twoD = (Graphics2D) g;

        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        twoD.setRenderingHints(rh);

        twoD.setColor(Color.BLACK);

        twoD.drawString(raised,5, 90);
    }
}

The only conclusion I can draw from this is that:

  1. Your resource is not being found.
  2. You need to learn basic debugging skills. In this case, specificailly 'checking the presumptions that what is happening in each step is actually working'. 'Triple level' statements such as the following should be broken down to 3 statements, with you checking each of the 3 results using System.out.println() or a debugger.

Debug unfriendly!

new JLabel(new ImageIcon(getClass()
        .getResource("background/2.jpg")));

And a note for future. For better help sooner, post an SSCCE.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Try to use contentPane.add(myComponent); rather than add(myComponent);

mKorbel
  • 109,525
  • 20
  • 134
  • 319
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • @kleopatra. you are wrong. in his code contentPane = new JLabel(new ImageIcon(getClass() .getResource("background/2.jpg"))); – StanislavL Apr 13 '12 at 09:55
  • What a weird setup that I missed :-). Nevertheless those two methods are adding to the same component - which is that label after setting it as contentPane. Or what else am I missing? – kleopatra Apr 13 '12 at 10:00