Show Graphics under Swing components (URGENT)

URGENT !!!

I have been a program which creates and dispays some graphics. I have used a JInternalFrame. Inside of this, I have put a JPanel into a JScrollPane. Finally, I have put into the JPanel some JPanels which are the graphics. I have been those ghaphics overriden the paint(Graphics g) method in the last JPanels.

The problem are that when I run the program and generate the graphics with the appropriate data I only see a JInternalFrame with the JPanel that I put into the JScrollBar but the JScrollBar and the JPanels of the graphics don't are displayed. Then, if I maximize and minimize the JInternalFrame, the JPanels with the graphics appear but the JScrollPane don't have effect (the scrollbars don't work). But if I maximize and minimize the JInternalFrame again, at the end, all are shown (scrollbars and graphics). Why it don't work at the beginning?

I have tried with validate(), repaint(), setVisible(true) even revalidate() methods of the JInternalFrame, JPanels and JScrollBar and that to be good for nothig. Also I capture the ComponentEvent of the Panel inside of the JScrollBar and I repaint all his components (the graphics) but it is useless. Now, I don't know that I can to do, I am desperate.

Please, help me !!!

Here I show you the structure of that part of the program:

class VentanaInterna extends JInternalFrame

{

...

public VentanaInterna(String nombre)

{

super(nombre,

true, // "resizable"

true, // "closable"

true, // "maximizable"

true);// "iconifiable"

setVisible(true);

setSize(300,300);

setLocation(xOffset*openFrameCount, yOffset*openFrameCount);

...

}

public void agregarComponente(Component nc)

{

Container cv = new Container();

cv = getContentPane();

cv.add(nc);

setContentPane(cv);

cv.validate();

}

...

}

class Pizarra

{

private JPanel panel;

private FlowLayout fl_panel;

private JScrollPane jsp;

private VentanaInterna v_graficas;

Pizarra()

{

panel = new JPanel();

panel.setBackground(Color.white);

fl_panel = new FlowLayout();

fl_panel.setHgap(0);

panel.setLayout(fl_panel);

jsp = new JScrollPane(panel);

v_graficas =

Engine.mainWindow.nuevaVentana("FBD - Grficas",

Color.white);

v_graficas.agregarComponente(jsp);

panel.addComponentListener(new CLRepaint());

}

public void agregarGrafica(Grafica g)

{

panel.add(g);

panel.validate();

jsp.validate();

}

class CLRepaint implements ComponentListener

{

public void componentHidden(ComponentEvent e)

{

Component[] c = panel.getComponents();

for (int i = 0; i < panel.getComponentCount(); i++)

c.repaint();

}

public void componentMoved(ComponentEvent e)

{

...

}

public void componentResized(ComponentEvent e)

{

...

}

public void componentShown(ComponentEvent e)

{

...

}

}

}

class Grafica extends JPanel

{

...

repaint(Graphics g)

{

...

}

}

Thank you very very much.

Felipe

[3435 byte] By [tigre13] at [2007-9-26 3:01:15]
# 1

Hi Felipe,

try to set the size in the constructor for the component that display the graphics with:

setPreferredSize(new Dimension(width, height));

setMaximumSize(getPreferredSize());

setMinimumSize(getPreferredSize());

with width and height are the size of the component.

Juergen

hoffy4 at 2007-6-29 10:59:08 > top of Java-index,Archived Forums,Java Programming...
# 2

Thank you, Juergen. With your solution the program go something better, but I have to move the window (JInternalFrame) yet because all components are not show. After I create the graphics, when I try display this ones, I see a window and inside "rare things" (graphics with the background color like than the JFrame and the graphics are superposed). Then, when I move the window all are show good (scrollbars and graphics).

I think that, perhaps, the problem could to be that I apply setPrefferredSize(), setMaximumSize(), setMinimumSize() even setSize() into the paint() method of the JPanels that I use to represent the graphics, but I need to do the program of this form becouse I have to modify the graphics dynamicaly and, at the beginning, I don't know the final size that the graphics (JPanels) will have got. Also, in this last case, when I modify some graphic's feature and then repaint the JPanel the same probrem arise.

Please, that someone help me! I need a solution yet!

Thank you.

Felipe

tigre13 at 2007-6-29 10:59:08 > top of Java-index,Archived Forums,Java Programming...
# 3

I've encountered a similar problem. My first solution

was to have a setSize() call done at the end of my

paint() method (after the graphics had been drawn).

However, this causes "flickering scrollbars".

To solve that, I instead overrode the setSize()

methods (there are two to override), and did a

super.setSize() upon instantion of the JPanel. I

was using a fixed size canvas (for drawing), so

I did need to change the size elsewhere, but if

you did, you would need to call super.setSize()

to update the panel's size.

Good luck,

Marc

mbeau at 2007-6-29 10:59:09 > top of Java-index,Archived Forums,Java Programming...
# 4
By the way, the overridden setSize() methodsdo nothing.Marc
mbeau at 2007-6-29 10:59:09 > top of Java-index,Archived Forums,Java Programming...
# 5

I believe this problem is similar to one I had recently. Any component that you add to a JScrollPane should implement Scrollable. JPanel does not implement Scrollable.

The ScrollablePicture class which is part of the ScrollDemo application in the JDK 1.31 documentation, which is located at

http://java.sun.com/docs/books/tutorial/uiswing/components/example-swing/ScrollablePicture.java

is an example of how to implement Scrollable.

btom at 2007-6-29 10:59:09 > top of Java-index,Archived Forums,Java Programming...