5 Replies Latest reply: Feb 7, 2012 4:11 AM by Kleopatra RSS

    Custom tooltip on table

    pedro.riky
      Hi all
      i want to open a tooltip wher the user rollover my jtable row.
      I try to use MouseMotionAdapter to open my tooltip whe mouse is moved and i use PrimaNotaMouseListener to dispose my windows.
      The tooltip works fine, but if the user change row in continuation the JVM crash down, the error is located in sun.awt.windows.WToolkit.eventLoop().
      It seems like a bug, but i don't find any solution.So my question is:is it the best solution to customize tooltip?
      I can't override public String getToolTipText(MouseEvent mouseEvent) on JTable because i don't want to override the method every time i need this function, but i want to set "somethings" that allow me to implements this.
      I cant post sscce because i use custom component and some function, but i think someone can tell me if the right way.
      public class MYMouseListener implements MouseListener
      {
      
           private Window window;
      
           public MYMouseListener(Window window)
           {
                this.window = window;
           }
      
           public void mousePressed(MouseEvent e)
           {
           }
      
           public void mouseReleased(MouseEvent e)
           {
           }
      
           public void mouseEntered(MouseEvent e)
           {
           }
      
           public void mouseExited(MouseEvent e)
           {
                if (window != null)
                {
                     window.dispose();
                }
           }
      
           public void mouseClicked(MouseEvent e)
           {
           }
      
      }
      public class MYMouseAdapter extends MouseMotionAdapter
      {
      
           private Window window;
           private int oldVoSel = -1;
      
           public MYMouseAdapter(Window window)
           {
                this.window = window;
           }
      
           public void mouseMoved(MouseEvent e)
           {
                try
                {
                     manageWindow(e);
                } catch (Exception e1)
                {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();
                }
           }
      
           void manageWindow(MouseEvent e) throws Exception
           {
                
                try
                {
                
                TableSelectionComponent aTable = (TableSelectionComponent) e.getSource();
                int itsRow = aTable.rowAtPoint(e.getPoint());
      
                
                AbstractVO abstractVO = (AbstractVO) aTable.
                          getArrayData().get(itsRow);
                
                if (abstractVO.hashCode() != oldVoSel || oldVoSel == -1)
                {
                     String text = getText();
      
                     if (text != null)
                     {
                          if (window != null)
                          {
                               window.dispose();
                               window.removeAll();
                          }
                          window.setLocation(e.getPoint());
      
                          JTextPane jTextPane = new JTextPane();
                          jTextPane.setContentType("text/html");
                          jTextPane.setText(text);
      
                          jTextPane.setEditable(false);
      
                          window.add(jTextPane);
                          window.setFocusable(false);
                          window.setAlwaysOnTop(true);
      
      
                          int tableRowsCount = countRows(text);
      
                          window.setPreferredSize(new Dimension(600,
                               (tableRowsCount * 20) + 300));
                          window.setSize(new Dimension(600, (tableRowsCount * 20) + 300));
                          GuiUtil.centerFrame(window);
      
                          window.setVisible(true);
                     }
                }
                else
                {
                     window.setLocation(e.getPoint());
                     window.setVisible(true);
                }
                
                oldVoSel = abstractVO.hashCode();
                }catch(Error w)
                {
                     w.printStackTrace();
                }
           }
      
           private int countRows(String text)
           {
                String regex = "<TR>";
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(text);
                int count = 0;
                while (m.find())
                {
                     count++;
                }
                return count;
           }
      
      }
        • 1. Re: Custom tooltip on table
          915714
          You meant like this bro :
          import javax.swing.*;
          
          public class Table extends JFrame
              {        
                  JPanel jp = new JPanel();
                  String [][] baris={{"JTable","JTable"},{"JTable","JTable"},{"JTable","JTable"},{"JTable","JTable"}};
                  
                  String[] kolom={"JTable","JTable"};
                  
                  JTable t = new JTable(baris,kolom);
                  JScrollPane sp =new JScrollPane(t);
                  
                  public Table ()
                      {
                          super("JTable");
                          setSize(250,250);
                          jp.setLayout(null);
                          
                          sp.setBounds(50,70,150,80);
                          t.setToolTipText("JToolTips\n");
                          
                          jp.add(sp);
                          
                          getContentPane().add(jp);
                          setVisible(true);
                          
                          setLocationRelativeTo(null);
                          setResizable(false);               
          
                      }
                      
                  public static void main ( String [] arguments )
                      {
                          new Table();
                      }                                                  
                  }
          Edited by: 912711 on Feb 6, 2012 5:49 PM
          • 2. Re: Custom tooltip on table
            pedro.riky
            Hi
            the problem is that the data are variables because is come from data base, and the ro data is only the summary and i have to display the detail data on my tooltip.
            • 3. Re: Custom tooltip on table
              pedro.riky
              Solved!! The problem was :
              else
                             {
                                  window.setLocation(e.getPoint());
                                  window.setVisible(true);
                             }
              If i comments the setVisible it works!!
              • 4. Re: Custom tooltip on table
                Kleopatra
                do. not. use. null. layout. never. ever.

                Cheers
                Jeanette
                • 5. Re: Custom tooltip on table
                  Kleopatra
                  why do want to re-invent the wheel? Set the tooltip on the renderering component, something like
                  class MyRenderer extends DefaultTableCellRenderer {
                           @Override
                           public Component getTableCellRendererComponent(....) {
                                 super(...);
                                 setToolTipText(createToolTipText(table, value, row, column);
                           }
                         
                           private String createToolTipText( ... ) {
                                // create the html as needed
                           }
                  }
                  Cheers
                  Jeanette