Results 1 to 3 of 3
  1. #1
    stevenc49 is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default 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
    Java 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);
    	}
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    tip = "<html>";
    ...
    tip += some_data + "<br>";
    ...
    tip += more_data;
    ...
    return tip.
    You can use a <center> tag if you like.

  3. #3
    stevenc49 is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Working with Tooltip Text
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:41 PM
  2. Tooltip
    By Preethi in forum New To Java
    Replies: 5
    Last Post: 06-11-2008, 11:37 AM
  3. Problem to ToolTip in Java 3d
    By roshithmca in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 03:46 AM
  4. How to create ToolTip in Java 3d
    By roshithmca in forum AWT / Swing
    Replies: 0
    Last Post: 02-04-2008, 06:57 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

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