如何使 jTable 内的 jList 可选
如何使 jTable 内的 jList 可选择且 jScrollPane 可滚动。
这是我的表代码:
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 不可选择,scrollpanel 不可滚动。
How to make the jList selectable and jScrollPane scrollable inside a jTable.
This is my table code :
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;
}
}
}
The JList and JScrollPanel appeared when the date has event. But the jList was unselectable and the scrollpanel was unscrollable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用在
TableCellRenderer
实现中绘制JList
的代码,并将其用作编辑器,即:并通过重写以下方法将此编辑器添加到您的表中你匿名内部 JTable 类:
You need to use your code that draws a
JList
in aTableCellRenderer
implementation and use it as an editor, i.e.:And add this editor to your table by overriding the following method in you anonymous inner JTable class: