Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - using JScrollPane

My first attempt at using a JScrollPane with a JTextArea is not going to plan -

JFrame window = new JFrame("Scroll test");       
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setLayout(null);
window.setVisible(true);
window.setBounds(500, 200, 700, 700);

JTextArea textArea = new JTextArea(5,30);
textArea.setBounds(18, 0, 682, 500);
textArea.setEditable(false);

JScrollPane textScroll = new JScrollPane(textArea,    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

textScroll.setBounds(0, 0, 18, 500);
window.add(textScroll);

Basically thats it, I get a scrollpane down the left hand side but it is empty of one of those block things you can drag, even when I fill the text area with crap so it expands beyond the bounds.... Thanks

like image 430
user1714941 Avatar asked Dec 27 '22 16:12

user1714941


2 Answers

Suggestions:

  • Don't set the JTextArea's bounds or preferredSize as either will limit it from growing preventing functional scrollbars from appearing.
  • Better would be to give the JTextArea a column and row count that makes sense.
  • Don't use null layout or absolute positioning. Much better to use nested containers that each use sensible and easy to use layout managers.
  • Don't set the preferredSize of the JScrollPane as someone else here is suggesting. Again, all that is needed is to use sensible numbers for the JTextArea's column and row attributes.
  • Instead let the component's own preferred size and the layout managers do the heavy work for you.

Edit: You state,

I need to use absolute because I will eventually have more in my JFrame, layout managers dont give me the freedom I need to position everything right

My reply: you're only saying that because you are unfamiliar with the full use and power of layout managers and how they make creating complex GUI's much simpler than absolute positioning does. Trust me as a Swing user from way back, you are as wrong as wrong can be with that statement. Ask any Swing expert and they will tell you the same. For instance, imagine creating a complex layout with absolute positioning and then later realizing that you must add one more JRadioButton to a set. If you're doing absolute positioning, you'll have to adjust the size of your GUI, reset the position of all components whose position will be affected by the addition. With use of layout managers, in particular with nesting layouts and their containers inside of one another, all you'll likely need to do is change one line of your code. Another situation: with absolute positioning you run the risk of extremely ugly GUI's if the program is run on different platforms, something that layout managers will take care of all for you.

like image 124
Hovercraft Full Of Eels Avatar answered Jan 09 '23 12:01

Hovercraft Full Of Eels


The problem is that you are setting the size of the scrollpane to a width of 18. Considering that usual vertical scrollbars take about 20px, you can't see much.

  1. Don't use null layout unless absolutely necessary
  2. Don't force bounds/size etc...
  3. The call to JFrame.setVisible(boolean) should be the last call

Here is an example that should get you started:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TestScrollPane {

    protected void initUI() {
        JFrame window = new JFrame(TestScrollPane.class.getSimpleName());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        JTextArea textArea = new JTextArea(25, 30);

        JScrollPane textScroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        window.add(textScroll);
        window.pack();
        window.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestScrollPane().initUI();
            }
        });
    }
}
like image 23
Guillaume Polet Avatar answered Jan 09 '23 12:01

Guillaume Polet