如何使 jTable 内的 jList 可选

发布于 2024-09-10 04:03:36 字数 3019 浏览 6 评论 0原文

如何使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-09-17 04:03:36

您需要使用在 TableCellRenderer 实现中绘制 JList 的代码,并将其用作编辑器,即:

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 类:

public TableCellEditor getCellEditor(int row, int column) {
     return new CalendarCellEditor();
}

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

}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文