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
Suggestions:
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.
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.
null
layout unless absolutely necessaryJFrame.setVisible(boolean)
should be the last callHere 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();
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With