1

I'm trying to set the size and location of a JScrollPane in a JFrame, and it's JViewport is a JTextArea.

The following is the code I am using:

private static JFrame frame = new JFrame();

public static void main(String... args) {
    frame.setTitle("Friend App");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 600);
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setLayout(null);
    scrollPane.setLocation(30, 30);
    scrollPane.setSize(new Dimension(100, 100));
    frame.add(scrollPane);
    textArea.append("test");
    frame.setVisible(true);

}

What else am I supposed to do so I can resize and set the location of this JScrollPane? Am I using the best approach here?

Note: scrollPane.setLayout(null) does nothing, frame.pack() is not what I want.

durron597
  • 31,968
  • 17
  • 99
  • 158
AMDG
  • 1,118
  • 1
  • 13
  • 29
  • 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 Aug 07 '15 at 13:06
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) 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 Aug 07 '15 at 13:06
  • @AndrewThompson There wouldn't happen to be a CSSLayout where I can simply apply css to specific components via XML would there? Or is that JavaFX? – AMDG Aug 07 '15 at 13:49
  • *"Or is that JavaFX?"* Yep. – Andrew Thompson Aug 08 '15 at 00:11
  • Would someone be willing to make an answer to this question to use JavaFX to show an HTML 5/CSS 3 LayoutManager? Then again I could make an implementation of LayoutManager that loads CSS and HTML and applies them to JComponents... – AMDG Aug 11 '15 at 00:52
  • *"Would someone be willing to make an answer to this question to use JavaFX to show an HTML 5/CSS 3 LayoutManager?"* A Couple of points. Given the age of this question, I doubt many new people will get to see it, and I was the only person 'notified' of new activity. But that is really a separate question that should be put on a separate thread. Be warned though, that requests for people to post code examples are usually frowned upon and met with derision. – Andrew Thompson Aug 11 '15 at 02:43

1 Answers1

4

The scrollPane is added to JFrame's content pane which has BorderLayout by default. The BorderLayout ignores location and size of component.

You should use another LayoutManager (alternatively you can use null layout for the content pane but it's not good way).

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I should change the layout of the JFrame instead of the component then right? Or simply add a JComponent to the frame to preserve the frame's layout I would think is better? – AMDG Aug 07 '15 at 12:49
  • Yes. To let component have desired position set proper layout to the component's container (in your case JFrame's content pane) – StanislavL Aug 07 '15 at 13:01