本文介绍了如何在jTable中使jList可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在jTable中使jList可选并且使jScrollPane滚动.
How to make the jList selectable and jScrollPane scrollable inside a jTable.
这是我的表格代码:
private JTable getCalendarTable() {
if (calendarTable == null) {
calendarTable = new JTable() {
public boolean isCellEditable(int nRow, int nCol) {
if (nRow % 2 != 0) {
return true;
} else
return false;
}
};
DefaultTableModel mtblCalendar = (DefaultTableModel) calendarTable
.getModel();
String[] headers = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat" };
for (int i = 0; i < 7; i++) {
mtblCalendar.addColumn(headers[i]);
}
calendarTable.setCellSelectionEnabled(true);
calendarTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
calendarTable.setRowHeight(60);
mtblCalendar.setColumnCount(7);
mtblCalendar.setRowCount(12);
GregorianCalendar cal = new GregorianCalendar();
realDay = cal.get(GregorianCalendar.DAY_OF_MONTH); // Get day
realMonth = cal.get(GregorianCalendar.MONTH); // Get month
realYear = cal.get(GregorianCalendar.YEAR); // Get year
currentMonth = realMonth; // Match month and year
currentYear = realYear;
refreshCalendar(currentMonth, currentYear);
}
return calendarTable;
}
class tblCalendarRenderer extends JTextArea implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean selected, boolean focused, int row,
int column) {
this.setText(value == null ? "" : value.toString());
this.setLineWrap(true);
this.setWrapStyleWord(true);
if (column == 0 || column == 6) { // Week-end
setBackground(new Color(255, 220, 220));
} else { // Week
setBackground(new Color(255, 255, 255));
}
if (row % 2 == 0) {
if (value != null) {
if (Integer.parseInt(value.toString()) == realDay
&& currentMonth == realMonth
&& currentYear == realYear) { // Today
setBackground(new Color(220, 220, 255));
}
}
} else {
if (value != null) {
// Here is just an test data I want to make sure the jList is working. When
// the date has event, show jList
JList list = new JList(new Object[] { "werwre", "fsdfsd",
"details", "computer", "folder", "computer" });
list.setVisibleRowCount(4);
JScrollPane pane = new JScrollPane(list);
list.setCellRenderer(new Incorenderer());
return pane;
}
}
return this;
}
}
}
当日期发生事件时,将出现JList和JScrollPanel.但是jList是不可选择的,滚动面板是不可滚动的.
The JList and JScrollPanel appeared when the date has event. But the jList was unselectable and the scrollpanel was unscrollable.
推荐答案
您需要使用在TableCellRenderer
实现中绘制JList
并将其用作编辑器的代码,即:
You need to use your code that draws a JList
in a TableCellRenderer
implementation and use it as an editor, i.e.:
class CalendarCellEditor extends AbstractCellEditor implements TableCellEditor {
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
if (value != null) {
// Here is just an test data I want to make sure the jList is
// working. When
// the date has event, show jList
JList list = new JList(new Object[] { "werwre", "fsdfsd",
"details", "computer", "folder", "computer" });
list.setVisibleRowCount(4);
JScrollPane pane = new JScrollPane(list);
list.setCellRenderer(new Incorenderer());
return pane;
} else {
// TODO return whatever you need
return null;
}
}
public Object getCellEditorValue() {
// TODO return whatever you need
return 1;
}
}
并通过在匿名内部JTable类中重写以下方法,将此编辑器添加到表中:
And add this editor to your table by overriding the following method in you anonymous inner JTable class:
public TableCellEditor getCellEditor(int row, int column) {
return new CalendarCellEditor();
}
这篇关于如何在jTable中使jList可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!