0

I have a JScrollPane (with both scrollbars optional (should not cause the problem) Inside of the ScrollPane is a panel with BoxLayout and X_Axis - align. (it contains arbitrary number of Panels with fixed (prefference)Size. The Problem is that the ScrollPane will be much wider than necessary (Horizontal Scrollbar scrolls through "grey screen"). With Y_Axis align it works as it should. Relevant code:

final JPanel forSpecific = new JPanel();

    final JScrollPane scrollSpecific = new JScrollPane(forSpecific,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        forSpecific.setLayout(new BoxLayout(forSpecific,BoxLayout.X_AXIS));

I have no idea whats the poblem and did not find any solution...

EDITED: sry it took some time. The original code was to complex to extract some sscce.. i wrote a test-class. This example works coorect.. but i dont know whats different.. package getdata;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class Sscce {

public static void gui(){
    final JFrame rootframe = new JFrame("Time Series Mining");
    final JPanel mainPanel = new JPanel(new BorderLayout());
    rootframe.setSize(new Dimension(400,400));
    rootframe.setContentPane(mainPanel);
    mainPanel.setLayout(new BorderLayout());
    JPanel center=new JPanel(new GridLayout(2,1));
    JPanel forSpecific=new JPanel();
    forSpecific.setLayout(new BoxLayout(forSpecific, BoxLayout.X_AXIS));
    JPanel test1 = new JPanel();
    test1.setPreferredSize(new Dimension(1000,1000));
    forSpecific.add(test1);
    test1.setBackground(Color.white);
    final JScrollPane scrollSpecific = new JScrollPane(forSpecific);
    center.add(scrollSpecific);
    rootframe.add(center, BorderLayout.CENTER);
    rootframe.setVisible(true);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jan S
  • 117
  • 3
  • 14
  • how do you define `forSpecific`? – akf Jun 07 '13 at 23:42
  • it would be best to include a SSCCE – akf Jun 08 '13 at 00:00
  • and added to its parent (A JPanel With GridLayout(2,1).. as second entry: centerPanel.add(scrollSpecific); – Jan S Jun 08 '13 at 00:02
  • @JanS, means noting without a `SSCCE!`. Most of the time as you attempt to create the SSCCE you will find your problem. – camickr Jun 08 '13 at 00:03
  • However.. I shanged the BoxLayout with "X-directon" to FlowLayout. Makes sence until it is nearly same. Can i trust in this Layout to have a look on childcomponents XAlinement,YAlignment and prefered size?.. I hate jawa gui... – Jan S Jun 08 '13 at 01:26
  • `This example works coorect` - which shows that if you code your program correctly it will work. Only you can solve your problem because we don't know what your code looks like. As I have already stated, generally you will have problems if you hard code the preferred size of a component. So that would be the first place to look. – camickr Jun 08 '13 at 01:44

1 Answers1

2
//final JScrollPane scrollSpecific = new JScrollPane(forSpecific,
//    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
//    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
final JScrollPane scrollSpecific = new JScrollPane(forSpecific);

Not the problem but the "scrollbar as needed" is the default. You don't need to specify this.

Inside of the ScrollPane is a panel with BoxLayout and X_Axis - align. (it contains arbitrary number of Panels with fixed (prefference)Size.

What is a fixed size?

  • the "main" panel added to the scrollpane
  • the "child" panels added to the main panel

In any case the size should not be fixed, the layout manager should determine the preferred size. Or if you are creating a custom component then you should override the getPreferredSize() method to return the proper size so the layout manager can do its job.

camickr
  • 321,443
  • 19
  • 166
  • 288