0

Basically my program has a JTabbedPane and each tab has a JTextPane in a JScrollPane. Somehow but always text starts from the bottom. How can I fix that?

I tried to put this, but it didn't work.

JScrollBar vertical = scrollPane.getVerticalScrollBar();

JScrollPane scrollPane = new JScrollPane();
scrollPane.setEnabled(false);
scrollPane.setBounds(10, 36, 973, 492);
panel.add(scrollPane);
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue(vertical.getMaximum()); 

JTextPane textPane = new JTextPane();
textPane.setEditable(false);
textPane.setFont(new Font("Calibri Light", Font.BOLD, 20));
textPane.setText("blahblahbalhtonsoftexthere");
scrollPane.setViewportView(textPane);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Terranova
  • 1
  • 1
  • You can use `scrollPane.getViewport().setViewPosition(new Point(0,0));` as shown here: [JScrollPane automatically sets ViewPort position to (0,0)](https://stackoverflow.com/questions/18142976/jscrollpane-automatically-sets-viewport-position-to-0-0) Otherwise you can use `yourScrollBar.setValue(0);` to make it scroll to a [specific value](https://docs.oracle.com/javase/8/docs/api/javax/swing/JScrollBar.html#setValue-int-) between the min and max scroll bar limits. – sorifiend Jun 02 '21 at 22:07
  • If you want to insert text into the text pane, you might need to insert the text directly into the `Document` – MadProgrammer Jun 02 '21 at 22:11
  • naaah, didnt help tried this again JScrollBar vertical = scrollPane.getVerticalScrollBar(); vertical.setValue(vertical.getMaximum()); didnt work again – Terranova Jun 02 '21 at 22:21
  • Show us a small working example of your code, it sounds like things are not set correctly (How did you insert and fill the JTextPane?). also, are you sure that you want to scroll right to the bottom using `vertical.getMaximum()`? – sorifiend Jun 02 '21 at 23:06
  • @sorifiend it will sound ridiculous, but i just inputed it in a code with a WindowBuilder – Terranova Jun 02 '21 at 23:09
  • 1
    We can't really help more if you can't show us the code. You will be able to open/inspect the generated code and paste the relevant bits here. Also, check that there is no other auto-generated code that is overriding the manual code you inserted by changing the viewport or scroll location after your manual code. – sorifiend Jun 02 '21 at 23:13
  • @sorifiend alright i added it – Terranova Jun 02 '21 at 23:35
  • 1
    1) *"i added it"* No, not an uncompilable code snippet, and not a dump of all the code, but a [mre] that is the shortest code necessary to reproduce the result. So no tabbed pane, only a single text pane in a scroll pane, and a loop to produce a long enough `String` (it can be gibberish, but keep it clean) to show the effect. 2) `setBounds(10, 36, 973, 492);` **No!** 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 .. – Andrew Thompson Jun 03 '21 at 00:57
  • .. [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) `scrollPane.setEnabled(false);` Why set the scroll pane to be disabled? This is something I've not seen in over two decades of Swing development! – Andrew Thompson Jun 03 '21 at 00:58
  • *"ScrollPane Text always starts in the bottom"* BTW - DYM that it scrolls down to the end of the text? That's a known behavior, but some seem to think you mean the lines of text are reversed (going up the the text pane). – Andrew Thompson Jun 03 '21 at 01:12
  • Right, I see your issue, take a look here to solve it: https://stackoverflow.com/questions/291115/java-swing-using-jscrollpane-and-having-it-scroll-back-to-top Either of the top two answers will work, but the easy way is to add this `textPane.setCaretPosition(0);` after setting the text. – sorifiend Jun 03 '21 at 01:29
  • @sorifiend THANK YOU VERY MUCH! – Terranova Jun 03 '21 at 23:46

0 Answers0