Owner Draw JComponent Problem

Hi,

This is the test code.

public class comp extends JComponent {

@Override

public void paintComponent(Graphics g) {

g.drawRect(10, 10, 100, 100);

}

}

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

JFrame fr = new JFrame("test");

fr.setVisible(true);

fr.setSize(200, 200);

JPanel pan = new JPanel();

comp c = new comp();

pan.add(c);

//fr.getContentPane().add(c);// it works

fr.getContentPane().add(pan);// doesn't work

c.repaint();

fr.validate();

}

}

I don't understand why java doesn't draw the rect if I put it in a JPanel. Could you give me a hint how to do this ?

[764 byte] By [mihaipopescua] at [2007-11-27 6:15:27]
# 1

I think that it draws, but the component may be too small to see. It might work if you make your component bigger. Try setting prefered size to something bigger than the rectangle.

Something like

fr.setSize(200, 200);

JPanel pan = new JPanel();

comp c = new comp();

c.setPreferredSize(new Dimension(150, 150));

Message was edited by:

petes1234

petes1234a at 2007-7-12 17:26:07 > top of Java-index,Java Essentials,New To Java...
# 2

thanks petes for your good answer and it works with the previous code, but

my mistake was that I simplified the code too much to replicate the problem... I wanna make it like this:

public class pane extends JPanel {

public void putComponent() {

comp c = new comp();

add(c);

c.setPreferredSize(new Dimension(120, 120));

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

JFrame fr = new JFrame("test");

fr.setVisible(true);

fr.setSize(200, 200);

pane pan = new pane();

fr.getContentPane().add(pan);

pan.putComponent();

}

Like this is still doesn't work. If you have another idea please drop a line.

Thank you

mihaipopescua at 2007-7-12 17:26:07 > top of Java-index,Java Essentials,New To Java...
# 3
sorry, I forgot to put a repaint() call in putComponent() proc.I'll come back with another code, because I set the preferred size in my original program and it still doesn't work.also, sorry for not putting code in it's special tag. This is the first time I use this
mihaipopescua at 2007-7-12 17:26:07 > top of Java-index,Java Essentials,New To Java...
# 4

no need for "repaint" here I think. Try something like this:

import java.awt.*;

import javax.swing.*;

class Test extends JFrame

{

public Test()

{

super("test");

setDefaultCloseOperation(EXIT_ON_CLOSE);

setPreferredSize(new Dimension(300, 300));

Pane myPane = new Pane();

myPane.putComponent();

getContentPane().add(myPane);

}

class Pane extends JPanel

{

public void putComponent()

{

Comp c = new Comp();

add(c);

c.setPreferredSize(new Dimension(120,120));

}

}

class Comp extends JComponent

{

@Override

public void paintComponent(Graphics g)

{

super.paintComponent(g);

g.drawRect(10, 10, 100, 100);

}

}

private static void createGUI()

{

Test t = new Test();

t.pack();

t.setVisible(true);

}

public static void main(String[] args)

{

javax.swing.SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

createGUI();

}

});

}

}

Message was edited by:

petes1234

petes1234a at 2007-7-12 17:26:07 > top of Java-index,Java Essentials,New To Java...
# 5
thanks petes, though I resolved the problem using also setSize() method.Do you know the difference between these two if they had the same effect ?
mihaipopescua at 2007-7-12 17:26:07 > top of Java-index,Java Essentials,New To Java...