Results 1 to 7 of 7
- 04-17-2010, 12:08 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
My JOptionPane Scrolls to Bottom!
Hi. I have a JOptionPane which includes a JTextArea. I did this as I wanted to be able to control the size of the JOptionPane and have a scrolling feature. However, the problem is if the string which is imported into the text area is bigger than the defined areas then this causes the pane to scroll downwards so you see the bottom of the text imported. What I want is to see the very start of the string and the user then does not need to scroll upwards.
Can anyone help?
Many thanks.
I show my code below:
JTextArea area = new JTextArea();
area.setText(astringthatisimported);
area.setRows(25);
area.setColumns(40);
area.setEditable(false);
area.setLineWrap(true);
area.setWrapStyleWord(true);
JScrollPane scrollpane = new JScrollPane(area);
Object[] array = {new JLabel("This is the contents of your file."),
scrollpane, };
JOptionPane.showConfirmDialog(null, array, "This is the contents of your file.", JOptionPane.OK_CANCEL_OPTION);
- 04-17-2010, 12:10 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
try to do set the cursor index to the beginning in the text area, smth like
area.setSelectedIndex(0) or whatever the api is
- 04-17-2010, 12:34 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
Thank you very much for your post.
I tried doing this. However, I got the following error:
softeng2button.java:300: cannot find symbol
symbol : method setSelectedIndex(int)
location: class javax.swing.JTextArea
area.setSelectedIndex(0);
^
1 error
You write:
area.setSelectedIndex(0) or whatever the api is
I am not sure what you mean by api. Also I assume SelectedIndex is java coding and not the name of a field that I have defined?
- 04-17-2010, 12:37 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
I meant for you to look at JTextArea (Java 2 Platform SE v1.4.2) and see what exactly the methods are called.
Try "setSelectionStart()" and "setSelectionEnd()"
- 04-17-2010, 12:48 AM #5
setCaretPosition
db
- 04-17-2010, 12:49 AM #6
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
With setSelectionStart() I get the same message as before!
softeng2button.java:301: setSelectionStart(int) in javax.swing.text.JTextComponent cannot be applied to ()
area.setSelectionStart();
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
I note the API page makes no reference to setSelectedIndex(0)
However for setSelectionStart it says:
setSelectionStart
public void setSelectionStart(int selectionStart)
Sets the selection start to the specified position. The new starting point is constrained to be before or at the current selection end.
This is available for backward compatibility to code that called this method on java.awt.TextComponent. This is implemented to forward to the Caret implementation which is where the actual selection is maintained.
Parameters:
selectionStart - the start position of the text >= 0
When it says public void setSelectionStart(int selectionStart) does this mean that I have to somewhere include this line in the program?
- 04-17-2010, 12:58 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
Thank you. Using the line:
area.setCaretPosition(0);
..solved the issue!!!!
*** BEWARE SITE Please wait while the application is loading... CONTAINS VIRUS ***
It did not work and McAfee shows a message that it had to remove and clean a virus!
Similar Threads
-
Bottom-Up or Top-Down
By 3xpr1ment in forum New To JavaReplies: 5Last Post: 03-31-2010, 09:18 AM -
How to make an icon in bottom-right of screen
By hunterbdb in forum AWT / SwingReplies: 1Last Post: 08-13-2009, 08:28 AM -
Error Message:org.xml.sax.SAXParseException: Value "bottom" is not one of the enumera
By Deepa in forum New To JavaReplies: 2Last Post: 12-02-2008, 11:06 AM -
Detect when the user scrolls a text control
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:21 PM -
JButtons in a straight line on the BOTTOM
By paul in forum AWT / SwingReplies: 0Last Post: 07-16-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks