Java - using JScrollPane

My first attempt at using JScrollPane with 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, I get this scrollpane on the left side, but it does not contain one of those blocks that you can drag even when I fill the text area with shit, so it expands beyond ... Thanks

+4
source share
2 answers

Suggestions:

  • Do not set JTextArea or preferredSize borders, as they limit this to preventing the appearance of functional scrollbars.
  • It would be better to give JTextArea the number of columns and rows that makes sense.
  • Do not use zero layout or absolute positioning. It is much better to use nested containers in which everyone uses reasonable and easy-to-use layout managers.
  • Do not set your preferred JScrollPane size as someone else suggests. Again, all that is needed is to use reasonable numbers for JTextArea columns and row attributes.
  • Instead, let your own custom size and layout managers do the hard work for you.

Change You indicate

I need to use the absolute, because in the end I will have more in my JFrame, layout managers will not give me freedom, I need everything to be correct.

My answer is: you only say that since you are not familiar with the full use and authority of layout managers and how they make creating a complex GUI much easier than absolute positioning. Believe me, as a Swing user from the other side, you are mistaken, as it could be wrong with this statement. Ask any Swing expert and they will tell you the same thing. For example, imagine creating a complex layout with absolute positioning, and then realizing that you have to add another JRadioButton to the set. If you are doing absolute positioning, you will need to adjust the size of your GUI, reset the position of all components whose position will be affected by the addition. When using layout managers, particularly with nesting layouts and their containers inside each other, all you most likely need to do is change one line of your code. Another situation: with absolute positioning, you risk an extremely ugly graphical interface, if the program is running on different platforms, that layout managers will take care of all of you.

+6
source

The problem is that you set the scroll size to a width of 18. Given that normal vertical scroll bars occupy about 20 pixels, you cannot see much.

  • Do not use null layout if absolutely necessary
  • Do not force borders / size, etc.
  • The call to JFrame.setVisible(boolean) should be the last call

Here is an example that should run you:

 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(); } }); } } 
+4
source

Source: https://habr.com/ru/post/1437425/


All Articles