Hello其他程序员
表一直是我的克星,但是由于stackoverflow,我的大多数问题都解决了。但不是这个,我无法找到解决方案。我想在我的JTable中使用JComboBox并希望有
编辑时不仅仅是项目的工具文本,
但是,当不编辑时,还有当前Cell本身的工具提示文本
有很多例子说明如何为每个项目设置工具提示并且它们有效,但似乎没有任何例子显示如何为Top"事物"在JCombBox本身。这里难以描述的是一个sscce。字符串的单元格显示工具提示,ComboBox的单元格不显示任何。
除此之外,如果你有时间,请解释,我实际上做错了什么,以及为什么这是错的。如果您需要进一步的信息,请不要犹豫。
编辑:我进一步减少了我的代码并删除了字符串列的渲染器,它显示了我想要的内容,但我猜你知道。TestComboBoxRenderer.java
import java.awt.Component;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
public class TestComboBoxRenderer extends BasicComboBoxRenderer {
private static final long serialVersionUID = -8316048934422548198L;
@Override
public Component getListCellRendererComponent(@SuppressWarnings("rawtypes") JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// Select the Item specific Tooltip
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
// Setting item specific tooltips
list.setToolTipText(value.toString());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
// These are not working
// super.setToolTipText("nice try");
// This also does not work
// this.setToolTipText("nice try");
setFont(list.getFont());
setText((value == null) ? "" : value.toString());
return this;
}
}
TestWindow.java
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.*;
public class TestWindow extends JFrame {
private static final long serialVersionUID = 7058494790322990937L;
// Nested Enum for the ComboBox
public enum BoxEnums {
Itemone, Itemtwo, Itemthree
}
@SuppressWarnings("unchecked") //
public TestWindow(){
// GUI Stuff
setSize(200, 100);
setLayout(new BorderLayout());
setTitle("Window");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ToolTipManager.sharedInstance().setInitialDelay(0);
ToolTipManager.sharedInstance().setDismissDelay(5000);
// Construct the table
JTable table = new JTable();
JScrollPane tablepanel = new JScrollPane();
tablepanel.setViewportView(table); // Only Fools add()
getContentPane().add(tablepanel, BorderLayout.CENTER);
table.setRowHeight(22);
// Construct the ComboBox Renderer
JComboBox<BoxEnums> cbox = new JComboBox<BoxEnums>(BoxEnums.values());
TestComboBoxRenderer comboBoxRenderer = new TestComboBoxRenderer();
cbox.setRenderer(comboBoxRenderer);
// Construct the tablemodel where a line is an ArrayList<Object> and the
// table is an ArrayList of line ArrayLists
TestTableModel model = new TestTableModel();
ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>();
ArrayList<Object> lineone = new ArrayList<Object>();
lineone.add("String 1");
lineone.add(BoxEnums.Itemone);
data.add(lineone);
model.setdata(data);
table.setModel(model);
// Set the renderer for column 1
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(cbox));
setVisible(true);
}
public static void main(String[] args) {
new TestWindow();
}
}
TestTableModel.java
import java.util.ArrayList;
import javax.swing.table.*;
class TestTableModel extends AbstractTableModel {
private static final long serialVersionUID = 8520572284680167375L;
// I have a TableModel that does a lot of thing, which are not really
// needed in this example
private String[] columnNames = {"A String", "A Combobox"};
ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>();
public TestTableModel() {
super();
}
// Just change set of data, since in my programm all managment
// is done outside
public void setdata(ArrayList<ArrayList<Object>> data){
this.data = data;
}
public void setValueAt(Object value, int row, int col) {
data.get(row).set(col, value);
}
public int getColumnCount() {
return columnNames.length;
}
public Object getValueAt(int row, int col) {
return data.get(row).get(col);
}
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public int getRowCount() { return data.size(); }
public String getColumnName(int col) { return columnNames[col]; }
public boolean isCellEditable(int row, int col) { return true; }
}