Solved

aboslute postioning and JScrollPanel

Posted on 2002-06-18
10
815 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 214
How to convert from xls to xlsx using java 7 56
Facing this issue for maven proxy setting 2 21
add projects t working set in maven 2 24
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

820 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