gpt4 book ai didi

java - 如何将从表头生成的工具提示添加到我的 JTable 单元格?

转载 作者:行者123 更新时间:2023-11-30 03:16:23 25 4
gpt4 key购买 nike

我有一个 JTable,我正在尝试添加一个标题工具提示,该提示将显示每个标题单元格的表标题中的字符串。我可以设置标题单元格的值:

colModel.getColumn(currentColumn).setHeaderValue(time + "(s)");

其中 colModel:

TableColumnModel colModel = audioTable.getColumnModel();

enter image description here enter image description here

为此,我添加了此监听器:

audioTable = new JTable(modelAudio);
JTableHeader header = audioTable.getTableHeader();
header.addMouseMotionListener(new MouseMotionListener() {

@Override
public void mouseMoved(MouseEvent e) {
System.out.println("mouseMoved");
// TODO Auto-generated method stub

}

@Override
public void mouseDragged(MouseEvent e) {
System.out.println("mouseDragged");
// TODO Auto-generated method stub

}
});

我看到 mouseMoved 在此 NPE 之间被一遍又一遍地打印:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthTableHeaderUI$HeaderRenderer.getTableCellRendererComponent(Unknown Source)
at javax.swing.table.JTableHeader.getToolTipText(Unknown Source)
at javax.swing.ToolTipManager$insideTimerAction.actionPerformed(Unknown Source)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我不确定如何获得更长的堆栈跟踪,这就是我创建表的方式:

modelAudio = new DefaultTableModel();
modelAudio.addColumn(" ");
audioTable = new JTable(modelAudio);

我想做的棘手的事情是动态添加列,因此每次用户填充当前列时我都会添加一列,如下所示:

TableColumn tc = new TableColumn(modelAudio.getColumnCount());
tc.setHeaderValue(" ");
audioTable.addColumn(tc);
modelAudio.addColumn(tc);

我添加列的方式是否是导致此 NPE 的问题?

最佳答案

首先,您没有包含足够的代码让我弄清楚为什么会出现 NullPointerException,但幸运的是,这没关系,因为您正在以错误的方式处理问题。

您根本不需要使用MouseMotionListener。 Swing 已经支持 ToolTip 行为,您所需要做的就是访问单元格渲染器本身。只需将渲染器设置为一个值,该值将在渲染组件之前设置 ToolTipText 属性。

您没有包含足够的代码来向我展示如何在您的应用程序中执行此操作,但我可以向您展示如何在 tutorial app from Oracle: 中执行此操作。在初始设置中,执行以下操作:

final TableCellRenderer renderer = table.getDefaultRenderer(Object.class);
table.setDefaultRenderer(Object.class, new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component component = renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
String columnHeader = table.getColumnModel().getColumn(column).getHeaderValue().toString();
((JComponent) component).setToolTipText(columnHeader);
return component;
}
});

以下是演示的完整代码,删除了一些不需要的代码:

import javax.swing.*;
import javax.swing.table.TableCellRenderer;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;

public class SimpleTableDemo extends JPanel {

public SimpleTableDemo() {
super(new GridLayout(1, 0));

String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years",
"Vegetarian" };

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) } };

final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

final TableCellRenderer renderer = table.getDefaultRenderer(Object.class);
table.setDefaultRenderer(Object.class, new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component component = renderer.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
String columnHeader = table.getColumnModel().getColumn(column)
.getHeaderValue().toString();
((JComponent) component).setToolTipText(columnHeader);
return component;
}
});

// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

// Add the scroll pane to this panel.
add(scrollPane);
}

private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
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();
}
});
}
}

还有一个屏幕截图(显然屏幕截图中没有捕获我的鼠标):

enter image description here

<小时/>

您可以使用 table.getTableHeader().getDefaultRenderer()ColumnHeader 执行类似的操作:

final TableCellRenderer header = table.getTableHeader().getDefaultRenderer();
table.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component tableCellRendererComponent = header.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
String columnHeader = table.getColumnModel().getColumn(column)
.getHeaderValue().toString();
((JComponent) tableCellRendererComponent).setToolTipText(columnHeader);
return tableCellRendererComponent;
}
});

关于java - 如何将从表头生成的工具提示添加到我的 JTable 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32485629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com