Results 1 to 3 of 3
- 06-29-2008, 04:00 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 2
- Rep Power
- 0
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.
iJava 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); } }
- 06-29-2008, 04:12 AM #2
You can use a <center> tag if you like.Java Code:tip = "<html>"; ... tip += some_data + "<br>"; ... tip += more_data; ... return tip.
- 06-29-2008, 04:15 AM #3
Member
- Join Date
- Jun 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Working with Tooltip Text
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:41 PM -
Tooltip
By Preethi in forum New To JavaReplies: 5Last Post: 06-11-2008, 11:37 AM -
Problem to ToolTip in Java 3d
By roshithmca in forum AWT / SwingReplies: 1Last Post: 02-05-2008, 03:46 AM -
How to create ToolTip in Java 3d
By roshithmca in forum AWT / SwingReplies: 0Last Post: 02-04-2008, 06:57 AM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks