Solved

aboslute postioning and JScrollPanel

Posted on 2002-06-18
10
809 Views
Last Modified: 2007-11-27
Can someone give a reason why the scroll bars in the top panel of the below code, are not functional, dispite the fact the panel in the JScrollPane hold labels beyound the view of the scroll panel.
Dose the fact the bottom panel use absolute postioning.

I'm playing at the moment. To get a feel how to implement the application i'm writting. Absolute postioning is required, as the layout managers are not flexible.

The code compiles.

here is the code

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;


class TestMultiButtons extends JFrame
{
    Dimension size;
   
    public TestMultiButtons(int hight, int width)
    {
        super("Test multi buttons");
     
     size = new Dimension(width, hight);
     JPanel topPanel = new JPanel();
     topPanel.setPreferredSize(new Dimension(900, 1));
     topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
     for (int i = 0; i < 100  ; i++)
     {
         JLabel label = new JLabel("hello");
         topPanel.add(label);
     }
     JScrollPane topScrollPane = new JScrollPane
         (topPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
     
     JPanel pane = new JPanel();
     pane.setPreferredSize(new Dimension(300, 150));
     //pane.setMinimumSize(new Dimension(600, 300));
     pane.setLayout(null);
     
     JButton blueButton = new JButton();
     blueButton.setBackground(Color.blue);
     pane.add(blueButton);

     
     JButton redButton = new JButton();
     redButton.setBackground(Color.red);
     pane.add(redButton);
     
     Insets insets = pane.getInsets();
     blueButton.setBounds(insets.left, insets.top, size.width, size.height);
     redButton.setBounds
          (insets.left +30, insets.top + size.height + 1, size.width, size.height);
     
     JSplitPane splitPanel = new JSplitPane
          (JSplitPane.VERTICAL_SPLIT, topScrollPane, pane);
     splitPanel.setOneTouchExpandable(true);
     splitPanel.setDividerLocation(50);
     setContentPane(splitPanel);
     //setResizable(false);
     pack();
     setVisible(true);
    }


    public static void main(String[] args)
    {
     TestMultiButtons mutiButtons = new TestMultiButtons(10,200);
     mutiButtons.addWindowListener(new WindowAdapter()
     {
          public void windowClosing(WindowEvent event)
          { System.exit(0); }
     });
    }
}
0
Comment
Question by:fauxn
  • 5
  • 5
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7086849
It cause you set the preferred size of the panel:

topPanel.setPreferredSize(new Dimension(900, 1));

Remove this line and your sb's should appear.
0
 

Author Comment

by:fauxn
ID: 7088346
ok that worked, but the height of the application is now the full height of the screen. ??

Is it ignoring the setPreferredSize statement??

When setting a splitpane is it better setPreferredSize for the splitpane or for each of the internal components?

Can you eplain why removing of the line:
topPanel.setPreferredSize(new Dimension(900, 1));

works
Noel
0
 

Author Comment

by:fauxn
ID: 7088361
here is the modified code:

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;


class TestMultiButtons extends JFrame
{
   Dimension size;
   
   public TestMultiButtons(int hight, int width)
   {
       super("Test multi buttons");
   
    size = new Dimension(width, hight);
    JPanel topPanel = new JPanel();
    //topPanel.setPreferredSize(new Dimension(900, 1));
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
    for (int i = 0; i < 100  ; i++)
    {
        JLabel label = new JLabel("hello");
        topPanel.add(label);
    }
    JScrollPane topScrollPane = new JScrollPane
        (topPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
   
    JPanel pane = new JPanel();
    pane.setPreferredSize(new Dimension(50, 100));
    //pane.setMinimumSize(new Dimension(600, 300));
    pane.setLayout(null);
   
    JButton blueButton = new JButton();
    blueButton.setBackground(Color.blue);
    pane.add(blueButton);

   
    JButton redButton = new JButton();
    redButton.setBackground(Color.red);
    pane.add(redButton);
   
    Insets insets = pane.getInsets();
    blueButton.setBounds(insets.left, insets.top, size.width, size.height);
    redButton.setBounds
         (insets.left +30, insets.top + size.height + 1, size.width, size.height);
   
    JSplitPane splitPanel = new JSplitPane
         (JSplitPane.VERTICAL_SPLIT, topScrollPane, pane);
    splitPanel.setOneTouchExpandable(true);
    splitPanel.setDividerLocation(50);
     setContentPane(splitPanel);
    //setResizable(false);
    pack();
    setVisible(true);
   }
     
   public static void main(String[] args)
   {
    TestMultiButtons mutiButtons = new TestMultiButtons(10,200);
    mutiButtons.addWindowListener(new WindowAdapter()
    {
         public void windowClosing(WindowEvent event)
         { System.exit(0); }
    });
   }
}
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 92

Expert Comment

by:objects
ID: 7091251
> but the height of the application is now the full height of the screen. ??

That's because you pack() the frame causing it to resize child components to their preferred size.

> Can you eplain why removing of the line work.
> topPanel.setPreferredSize(new Dimension(900, 1));

Because it sets the preferred height of the panel to 1 pixel. So vertical scrollbars will only be required if the viewport is smaller than 1 pixel.
If you don't explicitly set the preferred size, then it is calculated based on it's children.


0
 

Author Comment

by:fauxn
ID: 7094356
hmmm...
But the prefered size of the bottom panel is:
pane.setPreferredSize(new Dimension(50, 100));
why is the top panel tall enough to fit the two scroll buttons in, yet the bottom panel is the height of the screen, even though it should be 100 px?

So if both panels, top and bottom are given the the same prefered width, but different hight, the window looks of, its set to the sum of the childs prefered heights. But the scroll bars for the top are visible and work, however, when you move the split bar down the buttons don't work, even though there are still labels to see. If you move the split bar further down the buttons still don't work, but the labels become visiable, as the panel size grows. Can you explain this, I'm stumped.

Thanks for your help

here's the code:

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;


class TestMultiButtons extends JFrame
{
    Dimension size;
   
    public TestMultiButtons(int hight, int width)
    {
        super("Test multi buttons");
     
     size = new Dimension(width, hight);
     JPanel topPanel = new JPanel();
     topPanel.setPreferredSize(new Dimension(900, 100));
     topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
     for (int i = 0; i < 900  ; i++)
     {
         JLabel label = new JLabel("hello");
         topPanel.add(label);
     }
     JScrollPane topScrollPane = new JScrollPane
         (topPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
     
     JPanel pane = new JPanel();
     pane.setPreferredSize(new Dimension(900, 150));
     //pane.setMinimumSize(new Dimension(600, 300));
     pane.setLayout(null);
     Insets insets = pane.getInsets();
     
     JLabel greenLabel = new JLabel();
     //greenLabel.setForeground(Color.green);
     greenLabel.setBackground(Color.green);
     greenLabel.setOpaque(true);
     pane.add(greenLabel);
     greenLabel.setBounds
          (insets.left +30, insets.top + size.height * 2 + 3, size.width, size.height);
     
     JButton blueButton = new JButton();
     blueButton.setBackground(Color.blue);
     pane.add(blueButton);
     
     JButton redButton = new JButton();
     redButton.setBackground(Color.red);
     pane.add(redButton);
     
     
     blueButton.setBounds(insets.left, insets.top, size.width, size.height);
     redButton.setBounds
          (insets.left +30, insets.top + size.height + 1, size.width, size.height);
     
     JSplitPane splitPanel = new JSplitPane
          (JSplitPane.VERTICAL_SPLIT, topScrollPane, pane);
     splitPanel.setOneTouchExpandable(true);
     splitPanel.setDividerLocation(50);
     setContentPane(splitPanel);
     //setResizable(false);
     pack();
     setVisible(true);
    }


    public static void main(String[] args)
    {
     TestMultiButtons mutiButtons = new TestMultiButtons(10,200);
     mutiButtons.addWindowListener(new WindowAdapter()
     {
          public void windowClosing(WindowEvent event)
          { System.exit(0); }
     });
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 7094375
The actual height of the panel, and it's preferredSize are two different thing. The preferred, minimum and maximum size of a component are (maybe) used by the layout manager when it is laying out its components.

The scrollbar will stop functioning when the viewport becomes bigger than the preferred size.
0
 

Author Comment

by:fauxn
ID: 7094456
So setting the min, max and preferred sizes is recomended for all child panels ?

>The scrollbar will stop functioning when the viewport >becomes bigger than the preferred size.
How is this overcome? -> dont set a preferred size?
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7094568
> So setting the min, max and preferred sizes is
> recomended for all child panels ?

Generally not needed, unless you have special requirements.

> dont set a preferred size?

Not unless you have a reason to
0
 

Author Comment

by:fauxn
ID: 7094601
Thanks for your help.
Dose absolute postioning have any impact on the JScrollPane?
0
 
LVL 92

Expert Comment

by:objects
ID: 7094609
Depends how the absolute position is used, but I'll give a tentative answer of no :)  Though I very very rarely ever use absolute positioning.

Thanks for the points.

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 59
maven project error 5 55
rhino JavaScript import, load 25 82
why cannot we forward request once the response is committed 2 42
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question