如何在游标指向特定行时获取JTable中的行和列? [英] how to get the row and the column in a JTable when the cursor is pointed at a specific row?
问题描述
我正在创建一个包含基本数据的表,这里是我的框架
当我点击那行,它会弹出一个新的框架,包含所有的数据,它的工作原理,我可以正确地获得行和列,当我单击一行,
>
但这里是我想做的:
当我将光标置于表行上时,它会自动获取行和该数据的列,并且我想显示该数据的图片,就像在fb上,当您将光标放置在配置文件上,2秒后,它将显示该用户的配置文件。请帮助,只是一个新手:)
你可以做的是覆盖 prepareRenderer
方法的
JTable
并为每个单元格设置一个工具提示。然后使用一些html作为工具提示,如来自AndrewThompson的此答案
我使用这个图片这个网址 http://i.stack.imgur.com/Bbnyg.jpg
(这个 toUri()。toUll()
创建该URL。在任何情况下,html需要由< img src =
中的URL组成。您可以根据 row / column
值切换它们。
以下是示例。
>
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 ='
+http://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
您可以从类别路径中获取资源的网址,例如
URL url = getClass()。getResource(/ path / to / image.png);
final String html
=< html>< body>
+< img src ='
+ url
+'width = 150 height = 150&
如果是来自文件系统的文件,可以这样做
URL url = new File(path / to / image.png)toURI()。
I am creating a table that contains a basic data , here is my frame
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 ,
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 :)
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 http://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.
Here is the example.
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='"
+ "http://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();
这篇关于如何在游标指向特定行时获取JTable中的行和列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!