0

This is the code for my GUI:

public TLGUI(){

    final int x=500,y=600;

    JFrame frame=new JFrame();
    frame.setSize(x, y);
    frame.setVisible(true);
    frame.setResizable(false);

    JLabel labelTL=new JLabel("This is a test label");
    JScrollPane pane = new JScrollPane(labelTL,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    JPanel panel=new JPanel();
    panel.add(labelTL);
    frame.add(panel);
    frame.add(pane);

}

I have a huge problem right now regarding the line marked with the ** **.

This code does indeed add a scrollbar to my window, but the problem is that if I place it in front of the TLFrame.add(panel), I wont see it at all (I guess the panel is covering it in this order), and as soon as I turn it around, I can see a scrollbar but the whole frame other than the scrollbar is grey (I suppose the scrollbar is covering the panels contents here).

However, I want both of them to be visible at once, of course. Because my Label is bigger than the Frame, I want to be able to scroll down at least.

  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is a `CONSTANT_ALL_UPPER`) and use it consistently. – Andrew Thompson Feb 02 '15 at 19:49
  • Where does that code add anything to the scroll pane? – Andrew Thompson Feb 02 '15 at 19:51
  • First of all, thanks for the quick answer, but I don't quite understand you. As far as my resources taught me about scrollpanes, you just add them to your window and that's it. Is there more to do? –  Feb 02 '15 at 19:56
  • *"..thanks for the quick answer,.."* It's a comment, not an answer. ;) *"As far as my resources taught me about scrollpanes, you just add them to your window and that's it."* Seems you could use some new resources. By design, a scroll pane must have a component to scroll (otherwise, what is it supposed to display?)! – Andrew Thompson Feb 02 '15 at 19:59
  • Okay, thank you. I figured out how to add a Component. If I want it to scroll labelTL, I would simply add that as a Component? Or would I have to use the whole container? –  Feb 02 '15 at 20:07
  • Add it to what? If you mean the 'scroll pane' then you need to realize that a scroll pane takes exactly one component. Now, having said that, the 'one component' might be a `JPanel` that itself *contains other components.* – Andrew Thompson Feb 02 '15 at 20:08
  • Add it as a parameter to the JScrollPane. Currently I changed it to : JScrollPane pane = new JScrollPane(labelTL,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); I'm sorry to bother you but I'm really stuck on this right now. –  Feb 02 '15 at 20:11
  • *"..but I'm really stuck on this right now."* But I'm really waiting to see that MCVE before I go further into the code. – Andrew Thompson Feb 02 '15 at 20:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70063/discussion-between-stefan-schranz-and-andrew-thompson). –  Feb 02 '15 at 20:34
  • 3
    Don't use JScrollPane#add, this is not how you work with JScrollane. You're also adding to components to the same layout location on the frame, which means that one will cover the other – MadProgrammer Feb 02 '15 at 20:46
  • *"Let us continue this discussion in chat."* Let's not. I don't 'chat'. – Andrew Thompson Feb 02 '15 at 20:48
  • 2
    FYI setResizable will effect the frames border size, you should call this before calling setSize, but you should also be relying on pack instead – MadProgrammer Feb 02 '15 at 20:50
  • Thanks for the information, @MadProgrammer! The first one helped me to find a solution! –  Feb 02 '15 at 21:40

0 Answers0