1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Eduardo
  • 19,928
  • 23
  • 65
  • 73
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jul 30 '17 at 20:14

2 Answers2

2

I have changed

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

to

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

Now I see all 4 buttons.

1

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

user85421
  • 28,957
  • 10
  • 64
  • 87