yogesh meghnani
yogesh meghnani

Reputation: 739

How to use any other layout manager for jscrollpane with it instead of using null layout

I had created this view by using null layout:

enter image description here

This is the Code i had used to create above layout

public class Page1311 extends JPanel {
//  private JTable table;
//  public JScrollPane pane=null;
    public JPanel panel=null;
    public JButton back=null;
    /**
     * Create the panel.
     */
    public Page1311() {
        setLayout(null);
        back = new JButton("back");
        back.setFont(new Font("Arial", Font.PLAIN, 20));
        back.setBounds(10,10, 150, 27);
        add(back);
        List<List<String>> list=new ArrayList<List<String>>();
        for(int j=0;j<=5;j++)
        {
            List<String> list1=new ArrayList<>();
            list1.add("Honda Showroom"+j);
            list1.add("Mandsaur");
            list1.add("25 Chakrawati Colony Railway Station Road");
            list1.add("Activa");
            list1.add("2017");
            list1.add("Honda");
            list.add(list1);
        }
        getLayout(list,this);
        back = new JButton("back");
        back.setFont(new Font("Arial", Font.PLAIN, 20));
        back.setBounds(10,10, 150, 27);
        add(back);
    }
    public static void getLayout(List<List<String>> list,JPanel pane)
    {
        int i=0;
        int x=100;
        int y=100;
        int height=20;
        int width=200;
        int size=list.size();
        JLabel[] lblSName=new JLabel[size];
        JLabel[] lblSAddress=new JLabel[size];
        JLabel[] lblSCity=new JLabel[size];
        JLabel[] lblVName=new JLabel[size];
        JLabel[] lblVVersion=new JLabel[size];
        JLabel[] lblVCompanies=new JLabel[size];
        JButton[] lblGo=new JButton[size];
        Iterator<List<String>> it=list.iterator();
        while(it.hasNext())
        {
            System.out.println(x+" "+y+" "+width+" "+height+" "+i);
            Iterator iit=it.next().iterator(); 
            lblSName[i]= new JLabel();
            lblSName[i].setText("Name:"+iit.next());
            lblSName[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            lblSName[i].setBounds(x,y,width,height);
            pane.add(lblSName[i]);
            lblSCity[i] = new JLabel();
            lblSCity[i].setText("City:"+iit.next());
            lblSCity[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println((x+240)+" "+y+" "+width+" "+height+" "+i);
            lblSCity[i].setBounds(x+240,y,width,height);
            pane.add(lblSCity[i]);
            lblSAddress[i]= new JLabel();
            lblSAddress[i].setText("Address:"+iit.next());
            lblSAddress[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println((x+470)+" "+y+" "+(width+256)+" "+height+" "+i);
            lblSAddress[i].setBounds(x+470,y, width+256, height);
            pane.add(lblSAddress[i]);
            lblVName[i]= new JLabel();
            lblVName[i].setText("Vehicle Name:"+iit.next());
            lblVName[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println(x+" "+(y+35)+" "+width+" "+height+" "+i);
            lblVName[i].setBounds(x,y+35,width, height);
            pane.add(lblVName[i]);
            lblVVersion[i] = new JLabel();
            lblVVersion[i].setText("Vehicle Version:"+iit.next());
            lblVVersion[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println((x+240)+" "+y+35+" "+width+" "+height+" "+i);
            lblVVersion[i].setBounds(x+240,y+35, width, height);
            pane.add(lblVVersion[i]);
            lblVCompanies[i]= new JLabel();
            lblVCompanies[i].setText("Vehicle Companies:"+iit.next());
            lblVCompanies[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
            System.out.println((x+470)+" "+(y+35)+" "+(width+256)+" "+height+" "+i);
            lblVCompanies[i].setBounds(x+470,y+35, width+256,height);
            pane.add(lblVCompanies[i]);

            lblGo[i]= new JButton("Go ");
            lblGo[i].setFont(new Font("Monotype Corsiva", Font.ITALIC, 15));
            System.out.println(x+" "+(y+70)+" "+(width-130)+" "+height+" "+i);
            lblGo[i].setBounds(x,y+70,width-130, height);
            pane.add(lblGo[i]);
            i++;
            y=y+160;
            System.out.println("new height"+y);
        }
    }
public static void main(String[] args) {
    JFrame frame=new JFrame();
    frame.add(new Page1311());
    frame.setExtendedState(frame.MAXIMIZED_BOTH);
    frame.setLocation(0, 0);
    frame.setVisible(true);
}
}

But now i found that i cant use jscrollpane with null layout manager.So i want to create same layout by using any other layout manager.Can any one please help with with this or you can provide me any other way to use jscrollpane with null layout manager. Thanks in advance

Upvotes: 1

Views: 1092

Answers (4)

Andrew Thompson
Andrew Thompson

Reputation: 168825

I'd use a combination of a GridBagLayout for each 'show room' panel, then a single column GridLayout panel to stack the collection of show room panels in a single container. The second panel would go in the scroll pane.

enter image description here

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

public class ShowRoomLayout {

    private JComponent ui = null;

    ShowRoomLayout() {
        initUI();
    }

    private JPanel getShowRoomPanel(int num) {
        JPanel p = new JPanel(new GridBagLayout());
        p.setBorder(new TitledBorder("GridBagLayout"));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;

        p.add(new JLabel("Name:Honda Showroom" + num), gbc);

        gbc.gridx = 1;
        p.add(new JLabel("City:Mandsaur"), gbc);

        gbc.gridx = 2;
        p.add(new JLabel("Address:25 Chakrawati Colony Railway Station Road"), gbc);

        gbc.gridy = 1;
        gbc.gridx = 0;
        p.add(new JLabel("Vehicle Name:Activa"), gbc);

        gbc.gridx = 1;
        p.add(new JLabel("Vehicle Version:2017"), gbc);

        gbc.gridx = 2;
        p.add(new JLabel("Vehicle Companies:Honda"), gbc);

        gbc.gridy = 2;
        gbc.gridx = 0;
        p.add(new JButton("Go"), gbc);

        return p;
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        JPanel pList = new JPanel(new GridLayout(0, 1, 3, 3));
        pList.setBorder(new TitledBorder("GridLayout"));

        for (int ii = 1; ii < 21; ii++) {
            pList.add(getShowRoomPanel(ii));
        }

        JScrollPane scrollPane = new JScrollPane(pList,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        ui.add(scrollPane);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ShowRoomLayout o = new ShowRoomLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                Dimension d = f.getSize();
                f.setSize(new Dimension(d.width, 400));
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 3

matt
matt

Reputation: 12347

If your layout uses a null layout manager, you need to tell the scroll pane the size of your component

    JPanel pane = new JPanel(){
        Dimension d = new Dimension(400, 400);

        @Override
        public Dimension getMinimumSize(){
            return d;
        }
        @Override
        public Dimension getPreferredSize(){
            return d;
        }
        @Override
        public Dimension getMaximumSize(){
            return d;
        }

        @Override
        public void paintComponent(Graphics g){
            g.setColor(Color.RED);
            g.fill3DRect(25, 25, 350, 350, true);
        }
    };
    pane.setLayout(null);

    JFrame frame = new JFrame("check");
    frame.setSize(200, 200);
    frame.add(new JScrollPane(pane));
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

For this, I set the size of the pane to be 400, 400. The scroll pane will respect this. If you comment out the overriden methods, you'll see the scroll doesn't work anymore.

For OP to apply this technique they would just have to change the line where they add the panel to the JFrame.

frame.add(new JScrollPane(new Page1311()));

Since Page1311 doesn't use a layout manager then they need to override, getMin/Max/Preferred. as I did in my example. That would wrap the custom JPanel with a scroll pane, and the content would be scrollable.

Upvotes: 0

Maia
Maia

Reputation: 71

Another way without the need to set any layout manager for the JScrollPane is the method

JScrollPane.setViewportView(Component);

This is my preferred way. No size or layout handling on the fly.

If you want to place several Components to the same JScrollPane, just put a JPanel in between. The JPanel can have your preferred Layout.

Upvotes: 0

Alexey Romanov
Alexey Romanov

Reputation: 170733

JScrollPane has its own layout: ScrollPaneLayout. If you want to use any other, you don't set it on the scroll pane itself, you put a JPanel inside it and set the layout on that panel.

Upvotes: 0

Related Questions