Re: JScrollPane content sizing



MRe wrote:
Hi,

I'm new to Java GUI programming, and it's been a constant struggle
to write a GUI that looks like it was on purpose. Well, JScrollPane
has been giving me more trouble than it's worth, so as a last resort,
maybe someone could just tell me how to fix it?..

What I want to do is have a JScrollPane containing a JPanel. The
JPanel should automatically size to the width of the JScrollPane's
viewport, and to the minimum height of the contents of the JPanel. The
idea being that the JPanel's controls spring to fit the width of the
JScrollPane, (never showing a horizontal scroll bar), but be compact
vertically, showing a vertical scrollbar when the JScrollPane is too
small.

Hope that makes some sense; any help greatly appreciated

Thank you,
Kind regards,
Eliott

Layouts are one of the most difficult things to learn in Java. All layout managers respond to different conditions, some minimum/maximum/preferred size, some the size of the container and some both.

Remember that if you add a component to a ScrollPane constructor that component is the ViewPort and will follow the rules of the ScrollPaneLayout. It appears to be very similar the the center of a BorderLayout in action.

I wrote a little test program that shows the size of a JPanel in a JScrollPane. The JScrollPane only shows vertical scroll bars, never horizontal. The JScrollPaneLayout will allow the JPanel to expand larger than its preferred or maximum size (just as BorderLayout would).
It can never be smaller than the minimum width but if the JScrollPane has been resized smaller than that minimum size some of the JPanel is not visible.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test5 extends JPanel {
public test5() {
}

public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.WHITE);
g.drawString("Width: " + Integer.toString(getWidth()),10,20);
g.drawString("Height: " + Integer.toString(getHeight()),10,40);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

test5 p = new test5();
p.setBackground(Color.RED);
p.setMinimumSize(new Dimension(100,75));
p.setPreferredSize(new Dimension(200,150));
p.setMaximumSize(new Dimension(400,300));

JScrollPane sp = new JScrollPane(p,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

f.add(sp);

f.pack();
f.setVisible(true);
}
});
}
}

If you want to get more specific about how you want your layout to look and what components are on your JPanel, we can probably get you closer to your goal.

--

Knute Johnson
email s/knute/nospam/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.



Relevant Pages

  • =?iso-8859-1?q?Re:_JPanel_l=E4uft_=FCber_JScrollPane_-_kein_scrollen?=
    ... public class Fehler extends JFrame implements ActionListener, ... private JPanel jp_Rechts2 = new JPanel; ... private JScrollPane jscrollpaneL = new JScrollPane; ... Next by Date: ...
    (de.comp.lang.java)
  • Re: Problem mit JScrollPane und einem Bild in JTabbedPane
    ... > Ich versuche momentan in einem JTabbedPane ein Bild mit JScrollPane zu ... > private JPanel plInfo = new JPanel; ...
    (de.comp.lang.java)
  • Layout Problem mit 2 JScrollPanes nebeneinander
    ... ich schlage mich jetzt seit etwa 2 Abenden mit einem Problem bei der Darstellung von 2 JScrollPane herum, ... TestDialog1 dlg = new TestDialog1; ... JPanel actions = new JPanel; ... Component comp = super.getListCellRendererComponent; ...
    (de.comp.lang.java)
  • JScrollPane doesnt scroll a JPanel?
    ... I've never had problems with JScrollPane like I'm running into here... ... I am trying to create a Swing UI for VNC, ... JPanel. ... The inner JPanel implements getPreferredSize, ...
    (comp.lang.java.gui)
  • Re: positioning within a JScrollPane
    ... the JPanel within it is shorter than the full height, ... JScrollPane. ... MyPanel panel = new MyPanel; ... ScrollableJPanelDemo sjpd = new ScrollableJPanelDemo; ...
    (comp.lang.java.gui)