Java Code Examples for javax.swing.table.JTableHeader#repaint()

The following examples show how to use javax.swing.table.JTableHeader#repaint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SortableTableHeaderListener.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Handle a mouse press event - if the user is NOT resizing a column and NOT dragging a column
 * then give visual feedback that the column header has been pressed.
 *
 * @param e  the mouse event.
 */
public void mousePressed(final MouseEvent e) {

    final JTableHeader header = (JTableHeader) e.getComponent();

    if (header.getResizingColumn() == null) {  // resizing takes precedence over sorting
        if (header.getDraggedDistance() < 1) {   // dragging also takes precedence over sorting
            final int columnIndex = header.columnAtPoint(e.getPoint());
            final int modelColumnIndex 
                = header.getTable().convertColumnIndexToModel(columnIndex);
            if (this.model.isSortable(modelColumnIndex)) {
                this.sortColumnIndex = header.getTable().convertColumnIndexToModel(columnIndex);
                this.renderer.setPressedColumn(this.sortColumnIndex);
                header.repaint();
                if (header.getTable().isEditing()) {
                    header.getTable().getCellEditor().stopCellEditing();
                }
            }
            else {
                this.sortColumnIndex = -1;
            }
        }
    }

}
 
Example 2
Source File: SortableTableHeaderListener.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * When the user releases the mouse button, we attempt to sort the table.
 *
 * @param e  the mouse event.
 */
public void mouseReleased(final MouseEvent e) {

    final JTableHeader header = (JTableHeader) e.getComponent();

    if (header.getResizingColumn() == null) {  // resizing takes precedence over sorting
        if (this.sortColumnIndex != -1) {
            final SortableTableModel model = (SortableTableModel) header.getTable().getModel();
            final boolean ascending = !model.isAscending();
            model.setAscending(ascending);
            model.sortByColumn(this.sortColumnIndex, ascending);

            this.renderer.setPressedColumn(-1);       // clear
            header.repaint();
        }
    }
}
 
Example 3
Source File: SortableTableModel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  JTableHeader h = (JTableHeader) e.getComponent();
  TableColumnModel columnModel = h.getColumnModel();
  int viewColumn = columnModel.getColumnIndexAtX(e.getX());
  if (viewColumn < 0) {
    return;
  }
  TableCellRenderer tcr = h.getDefaultRenderer();
  int column = columnModel.getColumn(viewColumn).getModelIndex();
  if (column != -1 && tcr instanceof SortButtonRenderer) {
    SortButtonRenderer sbr = (SortButtonRenderer) tcr;
    sbr.setPressedColumn(column);
    sbr.setSelectedColumn(column);
    h.repaint();
    JTable table = h.getTable();
    if (table.isEditing()) {
      table.getCellEditor().stopCellEditing();
    }
    SortableTableModel model = (SortableTableModel) table.getModel();
    model.sortByColumn(column, SortButtonRenderer.DOWN == sbr.getState(column));
  }
}
 
Example 4
Source File: SortableTableModel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  JTableHeader h = (JTableHeader) e.getComponent();
  TableColumnModel columnModel = h.getColumnModel();
  int viewColumn = columnModel.getColumnIndexAtX(e.getX());
  if (viewColumn < 0) {
    return;
  }
  TableCellRenderer tcr = h.getDefaultRenderer();
  int column = columnModel.getColumn(viewColumn).getModelIndex();
  if (column != -1 && tcr instanceof SortButtonRenderer) {
    SortButtonRenderer sbr = (SortButtonRenderer) tcr;
    if (!sbr.isEnabledAt(column)) {
      return;
    }
    sbr.setPressedColumn(column);
    sbr.setSelectedColumn(column);
    h.repaint();
    JTable table = h.getTable();
    if (table.isEditing()) {
      table.getCellEditor().stopCellEditing();
    }
    SortableTableModel model = (SortableTableModel) table.getModel();
    model.sortByColumn(column, SortButtonRenderer.DOWN == sbr.getState(column));
  }
}
 
Example 5
Source File: HeaderCheckBoxHandler.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void tableChanged(TableModelEvent e) {
  int vci = table.convertColumnIndexToView(targetColumnIndex);
  TableColumn column = table.getColumnModel().getColumn(vci);
  Object status = column.getHeaderValue();
  TableModel m = table.getModel();
  boolean repaint = false;
  if (e.getType() == TableModelEvent.DELETE) {
    // System.out.println("DELETE");
    // System.out.println(status + ":   " + Status.INDETERMINATE.equals(status));
    repaint = fireDeleteEvent(m, column, status);
  } else if (e.getType() == TableModelEvent.INSERT && status != Status.INDETERMINATE) {
    // System.out.println("INSERT");
    repaint = fireInsertEvent(m, column, status, e);
  } else if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == targetColumnIndex) {
    // System.out.println("UPDATE");
    repaint = fireUpdateEvent(m, column, status);
  }
  if (repaint) {
    JTableHeader h = table.getTableHeader();
    h.repaint(h.getHeaderRect(vci));
  }
}
 
Example 6
Source File: SortableTableModel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  JTableHeader h = (JTableHeader) e.getComponent();
  TableColumnModel columnModel = h.getColumnModel();
  int viewColumn = columnModel.getColumnIndexAtX(e.getX());
  if (viewColumn < 0) {
    return;
  }
  TableCellRenderer tcr = h.getDefaultRenderer();
  int column = columnModel.getColumn(viewColumn).getModelIndex();
  if (column != -1 && tcr instanceof SortButtonRenderer) {
    SortButtonRenderer sbr = (SortButtonRenderer) tcr;
    sbr.setPressedColumn(column);
    sbr.setSelectedColumn(column);
    h.repaint();
    JTable table = h.getTable();
    if (table.isEditing()) {
      table.getCellEditor().stopCellEditing();
    }
    SortableTableModel model = (SortableTableModel) table.getModel();
    model.sortByColumn(column, SortButtonRenderer.DOWN == sbr.getState(column));
  }
}
 
Example 7
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static JTable makeJTable() {
  JTable table = new JTable(4, 3);
  table.setAutoCreateRowSorter(true);
  JPopupMenu popup = new JPopupMenu() {
    @Override public void show(Component c, int x, int y) {
      if (c instanceof JTableHeader) {
        JTableHeader header = (JTableHeader) c;
        header.setDraggedColumn(null);
        header.repaint();
        header.getTable().repaint();
        super.show(c, x, y);
      }
    }
  };
  popup.add("Item 1");
  popup.add("Item 2");
  popup.add("Item 3");
  table.getTableHeader().setComponentPopupMenu(popup);
  return table;
}
 
Example 8
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void show(Component c, int x, int y) {
  if (c instanceof JTableHeader) {
    JTableHeader header = (JTableHeader) c;
    JTable table = header.getTable();
    header.setDraggedColumn(null);
    header.repaint();
    table.repaint();
    int i = table.convertColumnIndexToModel(header.columnAtPoint(new Point(x, y)));
    if (i >= 0) {
      actions.forEach(a -> a.setIndex(i));
      super.show(c, x, y);
    }
  }
}
 
Example 9
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void show(Component c, int x, int y) {
  if (c instanceof JTableHeader) {
    JTableHeader header = (JTableHeader) c;
    header.setDraggedColumn(null);
    header.repaint();
    header.getTable().repaint();
    updateMenuItems(header.getColumnModel());
    super.show(c, x, y);
  }
}
 
Example 10
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void show(Component c, int x, int y) {
  if (c instanceof JTableHeader) {
    JTableHeader header = (JTableHeader) c;
    header.setDraggedColumn(null); // bookmark_1
    // if (header.getDraggedColumn() != null) remain dirty area >>>
    header.repaint();
    header.getTable().repaint();
    // <<<
    index = header.columnAtPoint(new Point(x, y));
    super.show(c, x, y);
  }
}
 
Example 11
Source File: HeaderCheckBoxHandler.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void tableChanged(TableModelEvent e) {
  if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == targetColumnIndex) {
    int vci = table.convertColumnIndexToView(targetColumnIndex);
    TableColumn column = table.getColumnModel().getColumn(vci);
    Object status = column.getHeaderValue();
    TableModel m = table.getModel();
    if (m instanceof DefaultTableModel && fireUpdateEvent((DefaultTableModel) m, column, status)) {
      JTableHeader h = table.getTableHeader();
      h.repaint(h.getHeaderRect(vci));
    }
  }
}
 
Example 12
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void show(Component c, int x, int y) {
  if (c instanceof JTableHeader) {
    JTableHeader header = (JTableHeader) c;
    header.setDraggedColumn(null);
    header.repaint();
    header.getTable().repaint();
    index = header.columnAtPoint(new Point(x, y));
    if (index >= 0) {
      super.show(c, x, y);
    }
  }
}
 
Example 13
Source File: ThreadsPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void repaintTimeline() {
    JTableHeader header = threadsTable.getTableHeader();
    TableColumn draggedColumn = header.getDraggedColumn();
    if (draggedColumn != null && draggedColumn.getModelIndex() == 2) {
        header.repaint();
    } else {
        int _column = threadsTable.convertColumnIndexToView(2);
        header.repaint(header.getHeaderRect(_column));
    }
}
 
Example 14
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void tableChanged(TableModelEvent e) {
  if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == targetColumnIndex) {
    int vci = table.convertColumnIndexToView(targetColumnIndex);
    TableColumn column = table.getColumnModel().getColumn(vci);
    Object status = column.getHeaderValue();
    TableModel m = table.getModel();
    if (m instanceof DefaultTableModel && fireUpdateEvent((DefaultTableModel) m, column, status)) {
      JTableHeader h = table.getTableHeader();
      h.repaint(h.getHeaderRect(vci));
    }
  }
}
 
Example 15
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void tableChanged(TableModelEvent e) {
  if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == targetColumnIndex) {
    int vci = table.convertColumnIndexToView(targetColumnIndex);
    TableColumn column = table.getColumnModel().getColumn(vci);
    Object status = column.getHeaderValue();
    TableModel m = table.getModel();
    if (m instanceof DefaultTableModel && fireUpdateEvent((DefaultTableModel) m, column, status)) {
      JTableHeader h = table.getTableHeader();
      h.repaint(h.getHeaderRect(vci));
    }
  }
}
 
Example 16
Source File: ThreadsPanel.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private void repaintTimeline() {
    JTableHeader header = threadsTable.getTableHeader();
    TableColumn draggedColumn = header.getDraggedColumn();
    if (draggedColumn != null && draggedColumn.getModelIndex() == 2) {
        header.repaint();
    } else {
        int _column = threadsTable.convertColumnIndexToView(2);
        header.repaint(header.getHeaderRect(_column));
    }
}
 
Example 17
Source File: Main_Blockade.java    From Hotel-Properties-Management-System with GNU General Public License v2.0 5 votes vote down vote up
public void populateTableHeaders() {

        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        final Calendar c = Calendar.getInstance();
        c.setTime(masterDate.getTime());

        JTableHeader tableHeader = table.getTableHeader();
        TableColumnModel tableColumnModel = tableHeader.getColumnModel();
        TableColumn tableColumn;

        tableColumn = tableColumnModel.getColumn(0);
        tableColumn.setHeaderValue("ROOM");
        tableColumn = tableColumnModel.getColumn(1);
        tableColumn.setHeaderValue("TYPE");
        tableColumn = tableColumnModel.getColumn(2);
        tableColumn.setHeaderValue("STATUS");

        //start the date from minus 1 to get today date.
        c.add(Calendar.DATE, -1);

        //start the loop from 3 because first 3 columns already 
        //populated up and the loop on 10 to get one week 
        for (int i = 3; i < 10; i++) {
            c.add(Calendar.DATE, 1);
            today = simpleDateFormat.format(c.getTime());
            tableColumn = tableColumnModel.getColumn(i);
            tableColumn.setHeaderValue(today);

            //store dates in special array to use it in bottom
            weekDates[i] = today;
        }

        tableHeader.revalidate();
        tableHeader.repaint();

    }