我有一个JTable,我想在一列中使用ComboBox编辑器,在另一列中使用TextArea。
当输入的TextArea包含多个文本行时,我成功调整了行高,但是当使用ComboBox Editor(在同一行中)选择单元格时,ComboBox下拉看起来有点"垂直拉伸& #34;即它填满整行高度。
这是怎么回事,或者是否有某种方法来设置ComboBox下拉标记的最大或首选高度,所以无论行高如何,所有行都相同?
以下对此进行了演示(感谢Dr Heinz at JavaSpecialists)
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumnModel;
public class ComboAndTextAreaTest extends JFrame {
private final JTable table = new JTable(10, 2);
public ComboAndTextAreaTest() {
super(System.getProperty("java.vm.version"));
Vector boxVals = new Vector();
boxVals.add("First");
boxVals.add("Second");
boxVals.add("Third");
final JComboBox box = new JComboBox(boxVals);
box.setSelectedIndex(0);
final TableCellEditor boxEditor = new DefaultCellEditor(box);
box.setPreferredSize(new Dimension());
TableColumnModel cmodel = table.getColumnModel();
// First column is ComboBox based
cmodel.getColumn(0).setCellRenderer(new DefaultTableCellRenderer());
cmodel.getColumn(0).setCellEditor(boxEditor);
// Second column is Text Area
cmodel.getColumn(1).setCellRenderer(new TextAreaRenderer());
cmodel.getColumn(1).setCellEditor(new TextAreaEditor());
// The following doesn't appear to have any effect
int cw = cmodel.getColumn(0).getPreferredWidth();
Dimension pd = new Dimension(cw, 18);
box.setMaximumSize(pd);
String test = "The quick brown fox jumps over the lazy dog.";
for (int row = 0; row < 10; row++) {
table.setValueAt(boxVals.get(row % 3), row, 0);
table.setValueAt(test, row, 1);
}
test = test + "\n" + test + test + test + "\n" + test + test;
table.setValueAt(test, 4, 1);
getContentPane().add(new JScrollPane(table));
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new ComboAndTextAreaTest();
}
}
编辑:为了清楚起见(关于渲染器的评论之后),以下是稍微修改的示例,删除了定制的TextArea处理。这个问题也适用于这种情况。
以下代码:
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumnModel;
public class ComboAndTextAreaTest extends JFrame {
private final JTable table = new JTable(10, 2);
public ComboAndTextAreaTest() {
super(System.getProperty("java.vm.version"));
Vector boxVals = new Vector();
boxVals.add("First");
boxVals.add("Second");
boxVals.add("Third");
final JComboBox box = new JComboBox(boxVals);
box.setSelectedIndex(0);
final TableCellEditor boxEditor = new DefaultCellEditor(box);
TableColumnModel cmodel = table.getColumnModel();
// First column is ComboBox based
cmodel.getColumn(0).setCellRenderer(new DefaultTableCellRenderer());
cmodel.getColumn(0).setCellEditor(boxEditor);
cmodel.getColumn(0).setMaxWidth(100);
// Second column is just text field
cmodel.getColumn(1).setCellRenderer(new DefaultTableCellRenderer());
cmodel.getColumn(1).setCellEditor(new DefaultCellEditor(new JTextField()));
cmodel.getColumn(1).setPreferredWidth(200);
String test = "The quick brown fox jumps over the lazy dog.";
for (int row = 0; row < 10; row++) {
table.setValueAt(boxVals.get(row % boxVals.size()), row, 0);
table.setValueAt(test, row, 1);
}
// special processing on row 4 to show row height and affect on combobox
String test2 = "This row set with greater height - combo drop down in prev cell affected during edit";
table.setValueAt(test2, 4, 1);
table.setRowHeight(4, 50);
getContentPane().add(new JScrollPane(table));
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new ComboAndTextAreaTest();
}
}
答案 0 :(得分:0)
@kleopatra(在问题评论中)回答问题“这是怎么回事?”基本上已经回答了这个问题“...... [是的]一个编辑器的大小总是按照单元格的大小调整,(差不多,除了非常肮脏的技巧:-)你无能为力。解决方法可能是自定义编辑器,fi一个包含组合的面板。虽然有一些工作,但是“。