- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 JTable 上实现 JToolTip,但是我遇到了一些问题。我在以下链接中找到了如何执行此操作的 Oracle 示例:
public class test extends JPanel {
private boolean DEBUG = false;
protected String[] columnToolTips = {null,
null,
"The person's favorite sport to participate in",
"The number of years the person has played the sport",
"If checked, the person eats no meat"};
public test() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel()) {
//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);
if (realColumnIndex == 2) { //Sport column
tip = "This person's favorite sport to "
+ "participate in is: "
+ getValueAt(rowIndex, colIndex);
} else if (realColumnIndex == 4) { //Veggie column
TableModel model = getModel();
String firstName = (String)model.getValueAt(rowIndex,0);
String lastName = (String)model.getValueAt(rowIndex,1);
Boolean veggie = (Boolean)model.getValueAt(rowIndex,4);
if (Boolean.TRUE.equals(veggie)) {
tip = firstName + " " + lastName
+ " is a vegetarian";
} else {
tip = firstName + " " + lastName
+ " is not a vegetarian";
}
} else {
//You can omit this part if you know you don't
//have any renderers that supply their own tool
//tips.
tip = super.getToolTipText(e);
}
return tip;
}
//Implement table header tool tips.
protected JTableHeader createDefaultTableHeader() {
return new JTableHeader(columnModel) {
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int index = columnModel.getColumnIndexAtX(p.x);
int realIndex = columnModel.getColumn(index).getModelIndex();
return columnToolTips[realIndex];
}
};
}
};
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableToolTipsDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new test();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
但是,当我编译并运行它时,没有显示任何工具提示?
任何关于为什么的帮助都会很棒,
谢谢。
最佳答案
根据您的代码片段getToolTipText()
方法仅返回选择性列的工具提示,即(2,4),对于标题,您有一个数组columnToolTips
其中值仅指定 3,4,5 列。
所以,简而言之,您的代码运行得很好。请进行适当的更改,然后您就可以开始了。
关于java - JTable 上的 JToolTip - 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14054112/
我想使用该类而不是调用 JComponent setToolTipText 方法,而是调用代码下面不显示工具提示: JButton btn = new JButtn("SAVE"); JToolTip
我想要一个JLabel,当鼠标悬停时,会出现一个小描述,并带有一个小按钮,可以将您发送到完整的描述。我创建了一个自定义工具提示,并添加了小描述和按钮,但我找不到如何使 JToolTip 保持打开状态。
我正在编写一个显示一些生物信息学数据的自定义组件,并且我希望能够显示有关当用户按住某个键时鼠标所在位置的附加信息。对于工具提示来说,这似乎是一项显而易见的工作,但有一些问题似乎阻止了它的工作。首先,我
更具体一点:我构建了一个简单的多线程 TCPServer (Java),它与客户端 (C#/mono) 通信。根据要求,客户端截取屏幕截图并将其发送到服务器。然后屏幕截图显示在 JTable 中。我想
我试图在 JPanel 上创建一个 JToolTip。这个想法是,用户在 JPanel (mouseMove) 上移动,光标后面将是工具提示。但我不知道如何将工具提示设置为JPanel。我的意思是,我
我有一些代码可以在用户操作后在屏幕上生成 JToolTip。我希望用户能够通过单击或按某个键(例如 ESC)来关闭提示。我可以让鼠标部分工作,但我不知道如何/在哪里捕获关键事件。我怀疑这与注意力集中有
我正在尝试在 JTable 上实现 JToolTip,但是我遇到了一些问题。我在以下链接中找到了如何执行此操作的 Oracle 示例: http://docs.oracle.com/javase/tu
Java Swing 问题。 我有一个显示图形的 JPanel。当我将鼠标移到该图上时,我希望某些信息显示在随鼠标移动的类似工具提示的小部件上。我怎样才能最好地实现它? 如果我知道如何将自定义 JCo
我有一个组件,我想在其中显示自定义 jtooltip。那很容易,只需更改 getTooltip 方法即可。位置和文本类似。 但是我也想更改计时器。如果鼠标悬停在组件的 cellrenderer 上,则
在使用 Nimbus LookAndFeel 的基于 Swing 的 Java 应用程序中,我尝试设置工具提示的背景颜色。所以我创建了一个 JToolTip 的子类,并通过覆盖 createToolT
有没有办法定义一个全局样式表,用于我整个应用程序中的所有 JToolTip?如果不是,我如何为我只知道它是一个 swing 组件 (JComponent) 的单个组件定义工具提示的样式表? 最佳答案
在 Windows 上,如果单击表格工具提示 (JToolTip),它将删除工具提示并选择表格中的行。然而在 Mac 上,它似乎只是删除了工具提示,并且没有选择表中的行。 该代码仅使用标准 JTabl
我是一名优秀的程序员,十分优秀!