Eduardo
Eduardo

Reputation: 21048

In Java Swing when I add my JScrollPane to my panel nothing appears

After some years working in Java in the back end I ended up working in a project to build a GUI and Java Swing is driving me crazy.

So I am doing some tests with the JScrollPane, because I have a JPanel that is too big to fit in the screen. In the following example I am adding couple buttons to a JPanel and then creating a JScrollPane with the JPanel, but nothing appears in the screen.

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestScrollPane extends JDialog {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            TestScrollPane dialog = new TestScrollPane();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public TestScrollPane() {
        setBounds(100, 100, 857, 541);
        getContentPane().setLayout(null);
        {
            JPanel panel = new JPanel();
            panel.setBounds(131, 167, 141, 221);
            getContentPane().add(panel);
            panel.setLayout(null);
            {
                JButton btnNewButton = new JButton("New button");
                btnNewButton.setBounds(0, 0, 115, 29);
                panel.add(btnNewButton);
            }
            {
                JButton btnNewButton_1 = new JButton("New button");
                btnNewButton_1.setBounds(26, 192, 115, 29);
                panel.add(btnNewButton_1);
            }

             JScrollPane jsp = new JScrollPane(panel);
             getContentPane().add(jsp);

        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setBounds(0, 446, 835, 39);
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }       
    }

}

I don't understand why is not appearing. I create the JPanel I add the buttons, and the the JScrollPane, which I add to the window. I am using WindwBuilder Pro that's why the code looks so weird.

Thanks.

Upvotes: 1

Views: 101

Answers (2)

user85421
user85421

Reputation: 29680

The components of a panel must be lay out by code if not using a layout manager, that is, when using setLayout(null). Most components start with dimension zero and will therefore not be displayed.

In above code there is missing the position and dimension of the scroll pane: jsp.setBounds(...) like done with the other components.

Normally it is not recommend to lay out the components yourself, better use a LayoutManager (e.g. GridBagLayout, BorderLayout,...) for that.

See Oracle's tutorial: Lesson: Laying Out Components Within a Container

Upvotes: 1

Ihor Radchenko
Ihor Radchenko

Reputation: 31

I have changed

getContentPane().setLayout(null);
panel.setLayout(null);

to

getContentPane().setLayout(new FlowLayout());
panel.setLayout(new FlowLayout());

Now I see all 4 buttons.

Upvotes: 2

Related Questions