Swing, beginner question

J

Jelena Marin

Have put in a lot of hours trying to get this work including a lot of
searching for the solution on the Internet but have been unable to find a
solution.

I have a Java program that generates graphics on a JPanel. The graphics
are not fully displayed because they overlay each other with this BoxLayout
that I am using. The window layout should look like this:
http://img146.imageshack.us/my.php?image=windowxl0.jpg.

Idea is to put every single graphics on separate panel; panel 2 and 3 must
be always visible when scrolling, so when I scroll to the right only panels
'1' and '3' are moving, and when scrolling down, only the '1' and '2' are
moving. How can I achieve that? Which layout?

This is the part of the code (third panel is not yet included) which is not
workig properly: only the 1/5 of the panel 2 is visible. When I put
everything in JScrollPane I get blank window...


public class Applet extends JApplet
{
public void init ()
{
JPanel graphics1 = new JPanel();
JPanel graphics2 = new JPanel();
graphics1.setLayout (new BoxLayout (graphics1, BoxLayout.LINE_AXIS));
graphics2.setLayout (new BoxLayout (graphics2, BoxLayout.LINE_AXIS));

graphics1.add (new drawGraphics1()); //
graphics2.add (new drawGraphics2()); // drawing only lines with
paintComponent

JPanel panel = new JPanel();
panel.setLayout (new BoxLayout (panel, BoxLayout.LINE_AXIS));
panel.add (graphics1);
panel.add (graphics2);
JScrollPane scroller = new JScrollPane (panel);

Container cp = getContentPane();
cp.add (panel); // panel 2 is shrinked
//cp.add (scroller); //blank window

public void init ()
{
...
}
}

public static void main (String[] args)
{
...
}
 
C

cp

For the JScrollPane you need to set the preferred size e.g.
scroller.setPreferredSize(size x, size y);
 
D

danharrisandrews

Hi Jelena,

My guess is that your Component objects "drawGraphics1" and
"drawGraphics2" should be setting their preferred size (e.g.
setPreferredSize(new Dimension(100, 400);). Please note that not all
layout managers treat minimum, maximum, and preferred size the same
way. Have a look here for more information on layout managers
(http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html).


Cheers,

Dan Andrews

- - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - -
 
J

Jelena Marin

For the JScrollPane you need to set the preferred size e.g.
scroller.setPreferredSize(size x, size y);

scroller.setPreferredSize(new Dimension (400, 400));

Nope, that doesn't help, still have blank window. :(
 
J

Jelena Marin

Hi Jelena,

My guess is that your Component objects "drawGraphics1" and
"drawGraphics2" should be setting their preferred size (e.g.

But when I define new JFrame, and then put everything in frame, and after
that I put that frame in applet container, everything works perfectly?! I
don't understand this at all. :(
 
K

Knute Johnson

Jelena said:
But when I define new JFrame, and then put everything in frame, and after
that I put that frame in applet container, everything works perfectly?! I
don't understand this at all. :(

Are you using setSize() on the JFrame? It's all tied up in the layout
manager and the sizing, some of which is automatic and some isn't. Try
putting it all in another JPanel that has BorderLayout and that has a
set size.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top