Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: JTable Popup

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default JTable Popup

    I have a jtable with a mouse over event where if the mouse is over the comment column of the table, a popup appears with the comment in the pop up. However the entire comment is on one line and some users have said that some comments are to long and go off the screen. I am wondering what I can add to my code to make the comment show on multiple lines. The users are also asking for a longer amount of time before the popup disappears but I am not sure how to change that either. Attached is a image of what I am talking about.

    example.jpg

    Any help would be appeciated, thanks.

    This is the current code used to create the popup

    new javax.swing.JTable()
    {
    public String getToolTipText(java.awt.event.MouseEvent e) {
    String tip = null;
    java.awt.Point p = e.getPoint();
    int rowIndex = rowAtPoint(p);
    int colIndex = columnAtPoint(p);
    int realColumnIndex = convertColumnIndexToModel(colIndex);

    if (realColumnIndex == 3) { // comment column
    tip = (String) getValueAt(rowIndex, colIndex);

    }

    return tip;
    }

    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JTable Popup

    Try using html, the paragraph element for new lines
    myComponent.setToolTipText("<html><body><p>This is</p><p> a new line</p></body></html>");

  3. The Following User Says Thank You to copeg For This Useful Post:

    banny7 (August 30th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: JTable Popup

    No that didn't work. Thanks though.

  5. #4
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: JTable Popup

    is the new line escape not working? "first line \n new line"

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: JTable Popup

    Yes, the \n just appears in the popup now it doesn't go down to the next line

  7. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: JTable Popup

    I have also added a system out to the code to see if it works correctly in the console and it does.

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JTable Popup

    Quote Originally Posted by banny7 View Post
    No that didn't work. Thanks though.
    What did you try? Post an SSCCE that reproduces 'what didn't work'

  9. The Following User Says Thank You to copeg For This Useful Post:

    banny7 (August 30th, 2011)

  10. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: JTable Popup

    I added the following using substrings to try and get new lines just to see if it would work, I know that they don't work with the spacing and I have other things I need to do a better job of but I more worried about getting a new line right now. The system out before the return gives me the new line but the popup message is still on one line.

    new javax.swing.JTable()
        {
            public String getToolTipText(java.awt.event.MouseEvent e) {
                String tip = null;
                java.awt.Point p = e.getPoint();
                int rowIndex = rowAtPoint(p);
                int colIndex = columnAtPoint(p);
                int realColumnIndex = convertColumnIndexToModel(colIndex);
     
                if (realColumnIndex == 6) { // comment column
                    tip = (String) getValueAt(rowIndex, colIndex);
     
                } 
     
                int numberOfLines = 0;
                String newTip = "";
     
                //If tip is shorter than 50 chars add a new line leave on one line
                if (tip.length() > 50) {
     
     
                    numberOfLines = (int) (tip.length() / 50);
                    //numberOfLines will most likely not be a whole number so add 1 just in case
                    numberOfLines += 1;
     
     
                    for (int i = 0; i < numberOfLines; i++) {
     
                        if (tip.length() > 50) {
                            //tip is greater then 50 add a new line. also check to ensure the remaining tip is atleast 50 chars long
                            newTip += tip.substring(0, 50) + " \n";
                            tip = tip.substring(50, tip.length());
     
                        } else {
                            //the remaining tip is not 50 chars long just add the remainder to new tip
                            newTip += tip.substring(0, tip.length());
                        }
     
                    }
     
                } else {
                    newTip = tip;
                }
     
                System.out.println("newTip in the new code is = "+ newTip);
                return newTip;
            }
     
        }

  11. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JTable Popup

    Where is the code trying the HTML I suggested above?

  12. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: JTable Popup

    oo sorry about that i figured out how to get it to work. I am using netbeans and the code was in the custom creation code under the poperties. It seems that when the code is generated by net beans it uses the setToolTipText with the returned string so when i added it in the section it was being recalled after the return. So after I realized this I added tags to the string as below, which is now working. You'll have to excuse me, I have only been working with java professionally for a few months. Thanks for your help.

     
    for (int i = 0; i < numberOfLines; i++) {
     
                        if (tip.length() > 50) {
                            //tip is greater then 50 add a new line. also check to ensure the remaining tip is atleast 50 chars long
                            newTip += "<p>"+tip.substring(0, 50) + " </p>";
                            tip = tip.substring(50, tip.length());
     
                        } else {
                            //the remaining tip is not 50 chars long just add the remainder to new tip
                            newTip += tip.substring(0, tip.length()) + "</p>";
                        }
     
                    }

    PS: Do you know if there is a way to extend the timer for the popup?

  13. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: JTable Popup

    This is my final code, including making sure the line ends on a space, incase you were woundering. Thanks again

    new javax.swing.JTable()
        {
            public String getToolTipText(java.awt.event.MouseEvent e) {
                String tip = null;
                java.awt.Point p = e.getPoint();
                int rowIndex = rowAtPoint(p);
                int colIndex = columnAtPoint(p);
                int realColumnIndex = convertColumnIndexToModel(colIndex);
     
                if (realColumnIndex == 3) { // comment column
                    tip = (String) getValueAt(rowIndex, colIndex);
     
                } 
     
     
                int numberOfLines = 0;
                String newTip = "<html><body>";
     
                //If tip is greater than 50 chars add a new line else leave on one line
                if (tip.length() > 50) {
     
     
                numberOfLines = (int) (tip.length() / 50);
                //numberOfLines will most likely not be a whole number so add 1 just in case
                numberOfLines += 1;
     
     
                    for (int i = 0; i < numberOfLines; i++) {
     
                        if (tip.length() > 50) {
     
                            //Find the space in the line. Do this so the word is not spilt on two lines
                            int indexOfSpace = 50;
                            while(indexOfSpace < tip.length() && tip.charAt(indexOfSpace) != ' '){
                                indexOfSpace++;
                            }
     
                            //tip is greater then 50 add a new line. Also change tip to start at indexOfSpace
                            newTip += "<p>"+tip.substring(0, indexOfSpace) + " </p>";
                            tip = tip.substring(indexOfSpace, tip.length());
     
                        } else {
                            //the remaining tip is not 50 chars long just add the remainder to new tip
                            newTip += "<p>"+tip.substring(0, tip.length()) + "</p>";
                        }
                        //Add closing tags
                        newTip += "</body></html>";
                    }
     
                } else {
                    //tip was less than 50 chars leave on one line
                    newTip = tip;
                }
     
                return newTip;
            }
     
        }

Similar Threads

  1. Can't edit JTextField from within a popup window
    By One in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 7th, 2011, 04:29 AM
  2. jTable popup box
    By _lithium_ in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2011, 12:05 PM
  3. jsp popup
    By hussain4hunt in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 1st, 2011, 09:27 AM
  4. popup menu
    By antonio69 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 04:24 AM
  5. Replies: 1
    Last Post: October 23rd, 2009, 03:17 PM