0

I want to set every element location with .setLocation(x, y)

I need a JPanel in a JScrollPane. But when I add components to JPanel, only buttons are showing. But not JLabel.

Method below is calling in JFrame constructor:

private void initGUI_test() {
    this.setSize(950, 700);
    this.setResizable(false);
    this.getContentPane().setLayout(null);

    JScrollPane mainScroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    JPanel container = new JPanel();

    mainScroll.setSize(900,500);
    mainScroll.setLocation(0,100);
    mainScroll.setBorder(BorderFactory.createLineBorder(Color.blue));
    mainScroll.add(container);

    container.setLayout(null);
    container.setLocation(0, 0);
    container.setSize(900, 500);

    JLabel rowA = new JLabel();
    rowA.setSize(180, 26);
    rowA.setLocation(10, 100);
    rowA.setText("Row A");

    JButton loadButton = new JButton();
    loadButton.setSize(180, 34);
    loadButton.setLocation(290, 110);
    loadButton.setText("Load file");

    container.add(rowA);
    container.add(loadButton);

    this.getContentPane().add(mainScroll);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
arrowman
  • 424
  • 6
  • 19
  • 2
    **DON'T** use a `null layout`! Swing was designed to work with different platforms, screen resolutions, pixel perfect GUI's are really hard to maintain, instead you should use a layout manager or [combinations of them](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and empty borders for [space between components](http://stackoverflow.com/questions/17874717/providing-white-space-in-a-swing-gui/17874718#17874718). – Frakcool Jul 04 '16 at 17:04
  • 1
    See [Null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout in swing](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more information about why you shouldn't use it. Once you've read the above links, please post a [mcve] that we can copy-paste and see the same issue as you. – Frakcool Jul 04 '16 at 17:05
  • Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Jul 04 '16 at 23:59

1 Answers1

3

Although I agree completely with @Frakcool about null layout, the problem you are facing has a different source. You should not add components directly into JScrollPane, but into JScrollPane's ViewPort.

The line mainScroll.add(container); should be mainScroll.getViewport().add(container);

ahoxha
  • 1,919
  • 11
  • 21
  • Components are showing, thanks. But scrolls don't work. Could You solve this problem yet, please? – arrowman Jul 04 '16 at 20:55
  • 1
    This will be solved only after you give up this nonsense of using a `null` layout. The GUI is broken until then & anything you do to it is just a hack to hide the root problem. – Andrew Thompson Jul 04 '16 at 23:59
  • @AndrewThompson Ok, so what do You think about Absolute layout? Is this wrong too? – arrowman Jul 05 '16 at 10:48
  • *"..Absolute layout? Is this wrong too?"* ***Worse*** than a `null` layout in that (behind the scenes) it ***is a*** `null` layout, but it is pretending to be a layout manager that takes into account everything the typical layout managers take care of. – Andrew Thompson Jul 05 '16 at 12:08
  • Ok, so I must learn any none intuitive layout based on borders:-( Giant calamity... Thank You! – arrowman Jul 06 '16 at 10:34