ScrollPane Flow Layout

Hi Guys,

I am at my wits end as to how I can solve this. I really can't find a solution, I hope you can help.

I have a gui layout thus:

JTabbedPane -> JScrollPane -> JPanel -> JPanel (instead of awt.canvas)

I have multiple java2d 'graphs' that I wish to layout on this scrollpane left to right which then wrap. If the screen is full, vertical scrollbars are triggered. So I think flow layout is the closest to this as I really must maintain the original component size. Grid layout etc resize the graphs making it look wrong.

The problem is that with flow layout, it scrolls infinitely horizontally. The only way I can get to to stop scrolling harizontally is by calling setPreferedSize(). When I do this however it seems to disable the scroll bars.

I have tried all combinations of layout managers to try and give me the same effect - to no avail. I have tried setting the scrollpane display policies and again this has no effect.

So I suppose I can see two possibilites:

1- find some way of getting the scrollpane to wrap without setPreferredSize()

2- find a way of getting the scroll bars working, with setPreferredSize()

My code, looks like this:

scrollPaneContents_ = new JPanel();

scrollPaneContents_.setPreferredSize(new Dimension(scrollPane_.getWidth(),scrollPane_.getHeight()));

scrollPaneContents_.setLayout(new FlowLayout(FlowLayout.CENTER));

scrollPane_ = new JScrollPane(scrollPaneContents_);

//scrollPane_ = new JScrollPane(scrollPaneContents_/*,

//scrollPane_.VERTICAL_SCROLLBAR_AS_NEEDED,scrollPane_.HORIZONTAL_SCROLLBAR_NEVER*/);

//layout.setConstraints(scrollPane_, layoutConstraints);

// I want it to scroll to the width less and continue downwards forever

scrollPaneContents_.setVisible(true);

tabView_.add("Tab Name",scrollPane_);

tabView_.refresh();

view_.refresh();

scrollPaneContents_.add(graphView_);

As you can see I have commented some out to try every combination :)

Any suggestions?

TIA

[2090 byte] By [PervBoya] at [2007-9-30 0:43:29]
# 1

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

public class GridBagAddition

{

public static void main(String[] args)

{

DisplayPanel displayPanel = new DisplayPanel();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new JScrollPane(displayPanel));

f.getContentPane().add(displayPanel.getButtonPanel(), "South");

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

class DisplayPanel extends JPanel

{

JButton addButton;

GridBagConstraints gbc;

int[] gridwidths = {

GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER

};

int graphCount = 0;

public DisplayPanel()

{

addButton = new JButton("add graph");

setLayout(new GridBagLayout());

gbc = new GridBagConstraints();

gbc.weightx = 1.0;

gbc.weighty = 1.0;

gbc.insets = new Insets(10,5,10,5);

}

public JPanel getButtonPanel()

{

addButton.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

gbc.gridwidth = gridwidths[graphCount++ % gridwidths.length];

add(new GraphPanel(), gbc);

revalidate();

}

});

JPanel panel = new JPanel();

panel.add(addButton);

return panel;

}

}

class GraphPanel extends JPanel

{

final int PAD = 20;

public GraphPanel()

{

setPreferredSize(new Dimension(200,175));

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

int width = getWidth();

int height = getHeight();

int x0 = PAD;

int x1 = width - PAD;

int y0 = height - PAD;

int y1 = PAD;

g2.draw(new Line2D.Double(x0, y0, x0, y1));

g2.draw(new Line2D.Double(x0, y0, x1, y0));

}

}

74philipa at 2007-7-16 5:15:43 > top of Java-index,Archived Forums,Swing...
# 2

Many thanks for you reply.

I am getting there now but your code still fails to address the problem of width. It only seems to create two collumns. I want a flow layout, where they are packed into as much space as possible then wrap. I can't see the difference between this and gridlayout, other than they are centrally alligned.

Am I missing something?

Many thanks

PervBoya at 2007-7-16 5:15:43 > top of Java-index,Archived Forums,Swing...
# 3

Ok,

The only way I managed to get round this was to create me own custom swing component that extends scroll pane and uses a grid layout. The layout of the components are dynamically calculated based on each child components width. Not very nice but a usable solution non the less.

Thanks for your help

PervBoya at 2007-7-16 5:15:43 > top of Java-index,Archived Forums,Swing...