Search in sources :

Example 86 with JTableHeader

use of javax.swing.table.JTableHeader in project openhospital-core by informatici.

the class TableSorter method addMouseListenerToHeaderInTable.

// There is no-where else to put this.
// Add a mouse listener to the Table to trigger a table sort
// when a column heading is clicked in the JTable.
public void addMouseListenerToHeaderInTable(JTable table) {
    final TableSorter sorter = this;
    final JTable tableView = table;
    tableView.setColumnSelectionAllowed(false);
    MouseAdapter listMouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = tableView.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int column = tableView.convertColumnIndexToModel(viewColumn);
            if (e.getClickCount() == 1 && column != -1) {
                // System.out.println("Sorting ...");
                int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK;
                boolean ascending = (shiftPressed == 0);
                sorter.sortByColumn(column, ascending);
            }
        }
    };
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
Also used : MouseEvent(java.awt.event.MouseEvent) JTable(javax.swing.JTable) MouseAdapter(java.awt.event.MouseAdapter) TableColumnModel(javax.swing.table.TableColumnModel) JTableHeader(javax.swing.table.JTableHeader)

Example 87 with JTableHeader

use of javax.swing.table.JTableHeader in project mucommander by mucommander.

the class FileTableHeaderRenderer method getTableCellRendererComponent.

// //////////////////////
// Overridden methods //
// //////////////////////
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    // Note: the label is returned by DefaultTableHeaderRenderer#getTableCellRendererComponent() is in fact this
    JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if (table != null) {
        JTableHeader header = table.getTableHeader();
        if (header != null) {
            label.setForeground(header.getForeground());
            label.setBackground(header.getBackground());
            label.setFont(header.getFont());
        }
        FileTable fileTable = (FileTable) table;
        if (fileTable.getSortInfo().getCriterion() == Column.valueOf(fileTable.convertColumnIndexToModel(column))) {
            // This header is the currently selected one
            label.setIcon(fileTable.getSortInfo().getAscendingOrder() ? ASCENDING_ICON : DESCENDING_ICON);
        } else {
            // The renderer component acts as a rubber-stamp, therefore the icon value needs to be set to null explicitely
            // as it might still hold a previous value
            label.setIcon(null);
        }
    }
    // Use borders made specifically for table headers
    Border border = UIManager.getBorder("TableHeader.cellBorder");
    label.setBorder(border);
    // Add a tooltip as headers are sometimes too small for the text to fit entirely
    label.setToolTipText((String) value);
    return label;
}
Also used : JTableHeader(javax.swing.table.JTableHeader) Border(javax.swing.border.Border)

Example 88 with JTableHeader

use of javax.swing.table.JTableHeader in project workcraft by workcraft.

the class FlatHeaderRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    JTableHeader tableHeader = table.getTableHeader();
    if (tableHeader != null) {
        label.setForeground(tableHeader.getForeground());
        label.setBackground(tableHeader.getBackground());
    }
    label.setText(value == null ? "" : value.toString());
    boolean fits = GuiUtils.getLabelTextWidth(label) < GuiUtils.getTableColumnTextWidth(table, col);
    label.setToolTipText(fits ? null : label.getText());
    return label;
}
Also used : JTableHeader(javax.swing.table.JTableHeader)

Example 89 with JTableHeader

use of javax.swing.table.JTableHeader in project domain_hunter_pro by bit4woo.

the class LineTable method addClickSort.

// 还是没能解决添加数据时排序报错的问题
@Deprecated
public void addClickSort() {
    // 双击header头进行排序
    tableRowSorter = new TableRowSorter<LineTableModel>(lineTableModel);
    setRowSorter(tableRowSorter);
    JTableHeader header = this.getTableHeader();
    header.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                if (LineTable.this.getModel() != null) {
                    // //当Jtable中无数据时,jtable.getRowSorter()是nul
                    // https://bugs.openjdk.java.net/browse/JDK-6386900
                    // 当model中还在添加数据时,如果进行排序,就会导致出错
                    // 获得列位置
                    int col = ((LineTable) e.getSource()).columnAtPoint(e.getPoint());
                    List<RowSorter.SortKey> keys = (List<RowSorter.SortKey>) LineTable.this.getRowSorter().getSortKeys();
                    keys.add(new RowSorter.SortKey(col, SortOrder.ASCENDING));
                    tableRowSorter.setSortKeys(keys);
                }
            } catch (Exception e1) {
                e1.printStackTrace(stderr);
            }
        }
    });
}
Also used : TableRowSorter(javax.swing.table.TableRowSorter) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) JTableHeader(javax.swing.table.JTableHeader)

Example 90 with JTableHeader

use of javax.swing.table.JTableHeader in project HO by akasolace.

the class TableSorter method addMouseListenerToHeaderInTable.

/**
 * Registers mouse listener on <code>jtable</code> to handle sorting.
 *
 * @param jtable Table to add listener to.
 */
public final void addMouseListenerToHeaderInTable(JTable jtable) {
    final JTable tableView = jtable;
    final JTableHeader jtableheader = tableView.getTableHeader();
    // Listener already present.
    if (jtableheader.getComponentListeners().length > 0) {
        return;
    }
    tableView.setColumnSelectionAllowed(false);
    final MouseAdapter mouseadapter = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent mouseevent) {
            final TableColumnModel tablecolumnmodel = tableView.getColumnModel();
            final int i = tablecolumnmodel.getColumnIndexAtX(mouseevent.getX());
            final int j = tableView.convertColumnIndexToModel(i);
            if ((mouseevent.getClickCount() == 1) && (j != -1)) {
                boolean flag = ascending;
                // If column sorted is different to the current one, sort by natural order.
                if (currentColumn != j) {
                    flag = isAscending(getModel(), j);
                }
                if (thirdColSort == i) {
                    isThirdSort = false;
                    iThirdSort++;
                    if (iThirdSort == 3) {
                        isThirdSort = true;
                        flag = true;
                        iThirdSort = 0;
                    }
                }
                if (currentColumn == j) {
                    flag = !flag;
                }
                TableSorter.this.sortByColumn(j, flag);
            }
        }
    };
    jtableheader.addMouseListener(mouseadapter);
}
Also used : MouseEvent(java.awt.event.MouseEvent) JTable(javax.swing.JTable) MouseAdapter(java.awt.event.MouseAdapter) JTableHeader(javax.swing.table.JTableHeader) TableColumnModel(javax.swing.table.TableColumnModel)

Aggregations

JTableHeader (javax.swing.table.JTableHeader)240 TableColumn (javax.swing.table.TableColumn)59 MouseEvent (java.awt.event.MouseEvent)56 JTable (javax.swing.JTable)53 TableColumnModel (javax.swing.table.TableColumnModel)45 TableCellRenderer (javax.swing.table.TableCellRenderer)38 Component (java.awt.Component)35 MouseAdapter (java.awt.event.MouseAdapter)32 DefaultTableModel (javax.swing.table.DefaultTableModel)29 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)28 Color (java.awt.Color)25 JPanel (javax.swing.JPanel)25 JScrollPane (javax.swing.JScrollPane)25 JLabel (javax.swing.JLabel)23 Point (java.awt.Point)22 Dimension (java.awt.Dimension)20 BorderLayout (java.awt.BorderLayout)18 JComponent (javax.swing.JComponent)17 TableModel (javax.swing.table.TableModel)17 JButton (javax.swing.JButton)14