Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-2008, 06:00 AM
Member
 
Join Date: Jun 2008
Posts: 2
stevenc49 is on a distinguished road
Multi-line Tooltip inside a JTable's Cell
Hi, I have a JTable and I got it to display tooltips depending on which cell the mouse hovers over. However, my tooltips are all one line and I would like to make it multi-lined (like a JTextArea). Some of the sites that I have found said that tooltips can display simple html but inserting <br> or even \n into my strings doesn't work for my program.

Could someone please take a look at my code and see what I can do? Thanks.

i
Code:
import java.awt.Font; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class MultiLineToolTipInsideJTableCell extends JFrame { public static void main(String[] args) { new MultiLineToolTipInsideJTableCell(); } public MultiLineToolTipInsideJTableCell() { //initilize this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); //JTable JTable commentTable = new JTable() { //Implement table cell tool tips. public String getToolTipText(MouseEvent e) { String tip = null; java.awt.Point p = e.getPoint(); int rowIndex = rowAtPoint(p); int colIndex = columnAtPoint(p); int realColumnIndex = convertColumnIndexToModel(colIndex); try { if (realColumnIndex == 2 && rowIndex!=0) { //comment row, exclude heading tip = getValueAt(rowIndex, colIndex).toString(); } } catch (RuntimeException e1) { //catch null pointer exception if mouse is over an empty line } return tip; } }; commentTable.setFont(new Font("Dialog", 0, 11)); commentTable.setAutoscrolls(true); commentTable.setOpaque(true); commentTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); commentTable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "USER", "TIME", "COMMENT" }) { @SuppressWarnings("unchecked") Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class}; boolean[] canEdit = new boolean[] { false, false, false}; @SuppressWarnings("unchecked") public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); ((DefaultTableModel) commentTable.getModel()).setRowCount(5); commentTable.getModel().setValueAt("USERNAME", 0, 0); commentTable.getModel().setValueAt("DATE", 0, 1); commentTable.getModel().setValueAt("COMMENTS", 0, 2); //generate table's content here for (int i=1; i<=3; i++) { commentTable.getModel().setValueAt("User"+i, i, 0); commentTable.getModel().setValueAt("Date"+i, i, 1); //commentTable.getModel().setValueAt("short comment"+i, i, 2); commentTable.getModel().setValueAt("A very loooooooooooooooooooooooooooooooooong comment. Would like to split it up into multiple lines."+i, i, 2); } //finalize panel.add(commentTable); this.add(panel); this.setSize(400, 300); this.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-29-2008, 06:12 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Code:
tip = "<html>"; ... tip += some_data + "<br>"; ... tip += more_data; ... return tip.
You can use a <center> tag if you like.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-29-2008, 06:15 AM
Member
 
Join Date: Jun 2008
Posts: 2
stevenc49 is on a distinguished road
Awesome. That's what I needed. Thanks!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Working with Tooltip Text Java Tip javax.swing 0 06-26-2008 09:41 PM
Tooltip Preethi New To Java 5 06-11-2008 01:37 PM
Problem to ToolTip in Java 3d roshithmca AWT / Swing 1 02-05-2008 05:46 AM
How to create ToolTip in Java 3d roshithmca AWT / Swing 0 02-04-2008 08:57 AM
Reading in data from file line by line bluekswing New To Java 1 10-02-2007 02:19 AM


All times are GMT +3. The time now is 02:37 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org