0

I am creating a table that contains a basic data , here is my frame enter image description here

when i click that row , it will popUp a new frame, that contains all data , it works well , i can properly get the row and the column when i click a row ,

enter image description here

but here is what i want to do :

when i set the cursor over the table row , it will automatically get the row and the column of that data on the table , and i want to display that data's picture , just like on the fb , when you set your cursor on a profile , after 2 secs, it will display the profile of that user. please help , im just a newbie :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
dens14345
  • 192
  • 1
  • 4
  • 17

1 Answers1

4

What you can do is override the prepareRenderer method of the JTable and set a tool tip for each cell. Then use some html for the tool tip as seen in this answer from AndrewThompson

I use this image this url https://i.stack.imgur.com/Bbnyg.jpg from this site (this question), but you will probably want to use a resource from your system or class path and use toUri().toUrl() to create the url. In any case, the html needs to consist of a URL in the <img src=. You can switch them based on the row/column value.

enter image description here

Here is the example.

enter image description here

import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class TestTableTooltip {

    String html
            = "<html><body>"
            + "<img src='"
            + "https://i.stack.imgur.com/Bbnyg.jpg"
            + "' width=160 height=120> ";

    public TestTableTooltip() {
        String[] cols = {"COL", "COL", "COL"};
        String[][] data = {
            {"Hello", "Hello", "Hello"},
            {"Hello", "Hello", "Hello"},
            {"Hello", "Hello", "Hello"}
        };
        DefaultTableModel model = new DefaultTableModel(data, cols);
        JTable table = new JTable(model) {
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if (c instanceof JComponent) {
                    JComponent jc = (JComponent) c;
                    jc.setToolTipText(html + "<br/>"
                        + getValueAt(row, column).toString()
                        + ":  row, col (" + row + ", " + column + ")"
                        + "</body></html>");
                }
                return c;
            }
        };

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestTableTooltip();
            }
        });
    }
}

UPDATE

You can get a url from a resource (in your class path) like this

  URL url = getClass().getResource("/path/to/image.png");
  final String html
        = "<html><body>"
        + "<img src='"
        + url
        + "' width=150 height=150> ";

If it is a file from the file system, you can do this

  URL url = new File("path/to/image.png").toURI().toURL();
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • *"I use this image this url http://i.stack.imgur.com/Bbnyg.jpg from this site"* Huh.. it's cute. What answer/question is it in? – Andrew Thompson Mar 07 '14 at 09:30
  • @AndrewThompson **this** answer :P – Paul Samsotha Mar 07 '14 at 09:31
  • Blast! I was hoping it was a) generated in code, and declared as 'open license' b) was one of a series of characters, or that character doing different things. -- (All to include in the main [hot links to these images are welcome](http://stackoverflow.com/a/19209651/418556) Q&A). – Andrew Thompson Mar 07 '14 at 09:34
  • @peeskillet sir , I dont understand how to put a resource image on my computer to that hmtl format :D – dens14345 Mar 07 '14 at 11:00
  • @peeskillet thanks for the update =) one last question, this may be out of topic , but i cant find a way on doing it , each data on the row on my table , they have an image that is save in the database, if i point my cursor on that row , how can i get the row number and the column number, so that i can create a query on the database on which picture to set on the "setTooltipText()" so that whenever i hover my cursor to that specific row, the right picture for that record will appear . – dens14345 Mar 07 '14 at 12:02
  • 1
    @dens14345 Honestly, to try and query a relational database on a tooltip seems a little overboard and may not give you the performance you need. That being said You need for your image file names to somehow represent the value that's in the cell. Like is the value is "Pedro", you will have a `Pedro.png` file and you can just parse/concatenate (do what you must). But somehow, the value needs to somehow direct you to the correct image. You may even want to take a look at [this answer](http://stackoverflow.com/a/8333959/2587435) to set a single tooltip for an entire row. – Paul Samsotha Mar 07 '14 at 12:07