Solved

aboslute postioning and JScrollPanel

Posted on 2002-06-18
10
790 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now