tags:

views:

214

answers:

5

Hello , I have written a code in java using swing.So that i will have a JscrollPane added to JPanel n then I will add buttons of fixed size to JPanel in verticle fashion

    JPanel panel=new JPanel();
    panel.setBackground(Color.WHITE);

    int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; 
    JScrollPane jsp=new JScrollPane(panel,v,h);
    jsp.setPreferredSize(new Dimension(600,600));
    jsp.setBounds(150,670,850,200);
    frame.add(jsp);

then I am adding buttons to it at run time.

     for(i=0;i<n;i++) 
    {
         button[i]=new JButton();
         button[i].setBounds(20,y,120,120);
         button[i].setSize(120,120);
         button[i].setToolTipText(file[i].toString());       
         button[i].setIcon(Icon);
         panel.add(button[i]);   
         y=y+140;
     }

I want to add a button one below another...(i.e I wnt a verticle scrollbar)

i.e. button1

 button2

   '

   '

and above code is giving me buttons in a line (i.e. I am getting horizontal scrollbar) i.e. button1 button2 . . .

another problem is the size of button I am setting using setsize is not affecting button size at all...

can anybody help me???

A: 

You have to set LayoutManager for JPanel or use Box(BoxLayout.Y_AXIS) instead.

skyman
+2  A: 

You need to define an appropriate LayoutManager for your JPanel, which is responsible for how the Components added to it are positioned. The default LayoutManager is FlowLayout, which lays out Components left-to-right. For laying out Components vertically you should consider using BoxLayout or GridBagLayout.

Adamski
A: 

For the size of buttons use preferredSize

For your layout problem you need to change the layout manager to one that does a vertical layout. For playing around purposes you can use BoxLayout like this:

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
willcodejavaforfood
A: 

You must use an appropriate Layoutmanager like GridLayout, Boxlayout or GridBagLayout for the panel.
It depends what else you want to put into the panel.

GridLayout is easier to use IMO:

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));  // any number of rows, 1 column
...
    panel.add(button[i]);

BoxLayout is almost as easy:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
...
    panel.add(button[i]);

GridBagLayout is more powerful, allowing more than one column, components spanning more than one cell, ... needs a GridBagConstraints to add the elements:

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints(
    0, RELATIVE,    // x = 0, y = below previous element
    1, 1,           // cell width = 1, cell height = 1
    0.0, 0.0        // how to distribute space: weightx = 0.0, weighty = 0,0 
    GridBagConstraints.CENTER,  // anchor
    GridBagConstraints.BOTH,    // fill
    new Insets(0, 0, 0, 0),     // cell insets
    0, 0);          // internal padding
...
    panel.add(button[i], constraints);

Have a look at this tutorial: Laying Out Components Within a Container (The visual guide is a good start point)

EDIT:
you can also lay out the components by hand, that is, specify the location and size of each component in the container. For this you must set the LayoutManager to null so the default manager gets removed.

JPanel panel = new JPanel();
panel.setLayout(null);
...
    button[i].setLocation(x, y);
    button[i].setSize(width, heigth);
    // OR button[i].setBounds(x, y, width, height);
    panel.add(button[i]);
Carlos Heuberger
A: 

This is much easier if you let the layout manager do its work.

In Swing, the way the components are layout over other component ( a panel for instance ) is using a layout manager.

It is used to avoid having to compute the coordinates of all the components against each other each time the container component resizes, or a new component is added.

There are different layout mangers, the one that you need here is BoxLayout.

By using this layout you don't need to specify the button position, nor its size. The layout manager query each component and use that information to place them in the correct position and size.

For instance the following frame

alt text

Was created this ( modified version of your ) code:

import javax.swing.*;
import java.awt.*;
public class ScrollTest {

    private JPanel panel;
    private Icon[] icons = new Icon[3];


    public void main() {
        panel =new JPanel();

        // Use top to bottom layout in a column
        panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ));


        panel.setBackground(Color.WHITE);

        int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
        int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; 
        JScrollPane jsp=new JScrollPane(panel,v,h);
        jsp.setPreferredSize(new Dimension(600,600));
        jsp.setBounds(150,670,850,200);
        JFrame frame = new JFrame();
        frame.add(jsp); 

        // my addition to load sample icons
        loadImages();
        // simulate dynamic buttons 
        addButtons();

        frame.pack();
        frame.setVisible( true );

    }
    void loadImages() {
        icons[0] = new ImageIcon( "a.png" );
        icons[1] = new ImageIcon( "b.png" );
        icons[2] = new ImageIcon( "c.png" );
    }

    void addButtons() {
        for( int i = 0 ; i < icons.length ; i++ ) {
           JButton button = new JButton();
           Icon icon = icons[i];
           button.setIcon( icon );
           // Set the button size to be the same as the icon size
           // The preferred size is used by the layout manager
           // to know what the component "better" size is.
           button.setPreferredSize( new Dimension( icon.getIconWidth(),
                                                   icon.getIconHeight() ) );
           // This is IMPORTANT. The maximum size is used bythe layout manager 
           // to know "how big" could this component be.
           button.setMaximumSize( button.getPreferredSize() );
           panel.add( button );
        }
}
public static void main( String ... args ) { 
    new ScrollTest().main();
}

}

I hope this helps.

OscarRyz