JTableHeader 的工具提示未显示其列的宽度
Tooltips of JTableHeader is not showing the width of its column
我正在尝试让 JTableHeader
的工具提示显示它所属的列的宽度,但它不起作用。工具提示未显示。
这是我的 JTableHeader
:
myTable.setModel(new DefaultTableModel(new Object[][] {{ label1, label2, label3, label4} },
new String[] { "foo", "bar", "foo bar", "bar foo" }));
final JTableHeader header = new TableHeaderTooltipWidth(myTable.getColumnModel());
header.setReorderingAllowed(false);
header.setResizingAllowed(true);
TableHeaderTooltipWidth
是 JTableHeader
的子类,在阅读 Official Tutorial How to use Tool tips.
之后,我重写了方法 public String getToolTipText(MouseEvent e)
package gui.paneles.elementos;
import java.awt.event.MouseEvent;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
public class TableHeaderTooltipWidth extends JTableHeader {
private static final long serialVersionUID = 1L;
public TableHeaderTooltipWidth() {
}
public TableHeaderTooltipWidth(TableColumnModel model) {
super();
}
@Override
public String getToolTipText(MouseEvent e) {
int col = columnAtPoint(e.getPoint());
return String.valueOf(this.getColumnModel().getColumn(col).getPreferredWidth());
}
}
但是工具提示没有显示。任何帮助,将不胜感激。谢谢!
你需要使用鼠标协调并根据你的计算鼠标的位置 table header 需要一个好的数学方程来做到这一点
..., after reading the Official Tutorial How to use Tool tips.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableHeaderTooltipWidthTest {
public JComponent makeUI() {
TableModel model = new DefaultTableModel(
new Object[][] {{ "label1", "label2", "label3", "label4"} },
new String[] { "foo", "bar", "foo bar", "bar foo" });
// How to Use Tables (The Java™ Tutorials)
// Specifying Tool Tips for Column Headers
// https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#headertooltip
JTable table = new JTable(model) {
//Implement table header tool tips.
@Override protected JTableHeader createDefaultTableHeader() {
return new JTableHeader(columnModel) {
@Override public String getToolTipText(MouseEvent e) {
int col = columnAtPoint(e.getPoint());
return String.valueOf(getColumnModel().getColumn(col).getPreferredWidth());
//int index = columnModel.getColumnIndexAtX(e.getPoint().x);
//int realIndex = columnModel.getColumn(index).getModelIndex();
//return String.valueOf(columnModel.getColumn(realIndex).getPreferredWidth());
}
};
}
};
return new JScrollPane(table);
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TableHeaderTooltipWidthTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
我正在尝试让 JTableHeader
的工具提示显示它所属的列的宽度,但它不起作用。工具提示未显示。
这是我的 JTableHeader
:
myTable.setModel(new DefaultTableModel(new Object[][] {{ label1, label2, label3, label4} },
new String[] { "foo", "bar", "foo bar", "bar foo" }));
final JTableHeader header = new TableHeaderTooltipWidth(myTable.getColumnModel());
header.setReorderingAllowed(false);
header.setResizingAllowed(true);
TableHeaderTooltipWidth
是 JTableHeader
的子类,在阅读 Official Tutorial How to use Tool tips.
public String getToolTipText(MouseEvent e)
package gui.paneles.elementos;
import java.awt.event.MouseEvent;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
public class TableHeaderTooltipWidth extends JTableHeader {
private static final long serialVersionUID = 1L;
public TableHeaderTooltipWidth() {
}
public TableHeaderTooltipWidth(TableColumnModel model) {
super();
}
@Override
public String getToolTipText(MouseEvent e) {
int col = columnAtPoint(e.getPoint());
return String.valueOf(this.getColumnModel().getColumn(col).getPreferredWidth());
}
}
但是工具提示没有显示。任何帮助,将不胜感激。谢谢!
你需要使用鼠标协调并根据你的计算鼠标的位置 table header 需要一个好的数学方程来做到这一点
..., after reading the Official Tutorial How to use Tool tips.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableHeaderTooltipWidthTest {
public JComponent makeUI() {
TableModel model = new DefaultTableModel(
new Object[][] {{ "label1", "label2", "label3", "label4"} },
new String[] { "foo", "bar", "foo bar", "bar foo" });
// How to Use Tables (The Java™ Tutorials)
// Specifying Tool Tips for Column Headers
// https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#headertooltip
JTable table = new JTable(model) {
//Implement table header tool tips.
@Override protected JTableHeader createDefaultTableHeader() {
return new JTableHeader(columnModel) {
@Override public String getToolTipText(MouseEvent e) {
int col = columnAtPoint(e.getPoint());
return String.valueOf(getColumnModel().getColumn(col).getPreferredWidth());
//int index = columnModel.getColumnIndexAtX(e.getPoint().x);
//int realIndex = columnModel.getColumn(index).getModelIndex();
//return String.valueOf(columnModel.getColumn(realIndex).getPreferredWidth());
}
};
}
};
return new JScrollPane(table);
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TableHeaderTooltipWidthTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}