Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

aboslute postioning and JScrollPanel

Posted on 2002-06-18
10
Medium Priority
?
836 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

885 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