user3358377
user3358377

Reputation: 145

JScrollPane in Eclipse by using palette

I am trying to make a JFrame scrollable by using Palette. In Netbeans if I make a panel with dimensions (300, 500) and a ScrollPane with dimensions (200,200) then if I drag and drop the panel into the ScrollPane it creates automatically the bars.

In eclipse I tried it with the same way and I cannot make it. Moreover the final code in eclipse after the attempt is the following:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;


public class InsertWaterRawData extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InsertWaterRawData frame = new InsertWaterRawData();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public InsertWaterRawData() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 577, 383);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(236, 87, 200, 200);
        contentPane.add(scrollPane);

        JPanel panel = new JPanel();
        scrollPane.setViewportView(panel);
        panel.setLayout(null);
    }

}

Is there any way to make it with the palette ??

Thanks in advance

Upvotes: 0

Views: 1386

Answers (1)

camickr
camickr

Reputation: 324118

panel.setLayout(null);

Don't use a null layout!!!

The scrollbars will appear automatically when the preferred size of the panel is greater than the size of the scrollpane.

When you use a layout manager the preferred size of the panel will be calculated automatically as you add components to the panel.

If you are doing custom painting on your panel, then you need to override the getPreferredSize() method of your panel to return an appropriate size.

Based on the code you posted there is no need for scrollbars because no components have been added to the panel.

Upvotes: 1

Related Questions