Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Question 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);

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    try to do set the cursor index to the beginning in the text area, smth like

    area.setSelectedIndex(0) or whatever the api is

  3. #3
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    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?

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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()"

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    setCaretPosition

    db

  6. #6
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    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?

  7. #7
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Smile

    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

  1. Bottom-Up or Top-Down
    By 3xpr1ment in forum New To Java
    Replies: 5
    Last Post: 03-31-2010, 09:18 AM
  2. How to make an icon in bottom-right of screen
    By hunterbdb in forum AWT / Swing
    Replies: 1
    Last Post: 08-13-2009, 08:28 AM
  3. Replies: 2
    Last Post: 12-02-2008, 11:06 AM
  4. Replies: 0
    Last Post: 07-25-2008, 02:21 PM
  5. JButtons in a straight line on the BOTTOM
    By paul in forum AWT / Swing
    Replies: 0
    Last Post: 07-16-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •