views:

53

answers:

1

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.

A: 

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();
}
ZloiAdun