Solved

aboslute postioning and JScrollPanel

Posted on 2002-06-18
10
829 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

623 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