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

The following examples show how to use javax.swing.table.JTableHeader#getColumnModel() . 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: ExtendedJTable.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
public void setSortingStatus(final int status, final boolean cancelSorting) {
	if (getModel() instanceof ExtendedJTableSorterModel) {
		ExtendedJTableSorterModel sorterModel = (ExtendedJTableSorterModel) getModel();

		JTableHeader h = getTableHeader();
		TableColumnModel columnModel = h.getColumnModel();
		int viewColumn = getSelectedColumn();
		if (viewColumn != -1) {
			int column = columnModel.getColumn(viewColumn).getModelIndex();
			if (column != -1) {
				if (sorterModel.isSorting()) {
					if (cancelSorting) {
						sorterModel.cancelSorting();
					}
				}
				sorterModel.setSortingStatus(column, status);
			}
		}
	}
}
 
Example 2
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 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void paint(Graphics g, JComponent c) {
  super.paint(g, c);
  if (c instanceof JLayer) {
    JScrollPane scroll = (JScrollPane) ((JLayer<?>) c).getView();
    JTable table = (JTable) scroll.getViewport().getView();
    JTableHeader header = table.getTableHeader();

    int width = header.getWidth();
    TableColumnModel cm = header.getColumnModel();
    for (int i = 0; i < cm.getColumnCount(); i++) {
      width -= cm.getColumn(i).getWidth();
    }

    Point pt = SwingUtilities.convertPoint(header, 0, 0, c);
    filler.setLocation(pt.x + header.getWidth() - width, pt.y);
    filler.setSize(width, header.getHeight());
    fillerColumn.setWidth(width);

    SwingUtilities.paintComponent(g, filler, tempTable, filler.getBounds());
  }
}
 
Example 4
Source File: SortingDecorator.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void mouseClicked(final MouseEvent e) {
    final JTableHeader h = (JTableHeader) e.getSource();
    final TableColumnModel columnModel = h.getColumnModel();
    final int viewColumnIndex = columnModel.getColumnIndexAtX(e.getX());
    final int columnIndex = columnModel.getColumn(viewColumnIndex).getModelIndex();
    if (columnIndex != -1) {
        int direction = getSortingDirection(columnIndex);
        if (!e.isControlDown()) {
            clearSortingDirections();
            _tableHeader.repaint();
        }
        // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
        // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
        direction += (e.isShiftDown() ? -1 : 1);
        direction = (direction + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
        setDirectionForColumn(columnIndex, direction);
        doSortBy = false;
        initViewToModel();
        fireTableDataChanged();
    }
}
 
Example 5
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 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: AOISortingDecorator.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
        public void mouseClicked(final MouseEvent e) {
            final JTableHeader h = (JTableHeader) e.getSource();
            final TableColumnModel columnModel = h.getColumnModel();
            final int viewColumnIndex = columnModel.getColumnIndexAtX(e.getX());
            final int columnIndex = columnModel.getColumn(viewColumnIndex).getModelIndex();
            if (columnIndex != -1) {
                int direction = getSortingDirection(columnIndex);
                if (!e.isControlDown()) {
                    clearSortingDirections();
//                    viewToModel = null;
//                    fireTableDataChanged();
                    _tableHeader.repaint();
                }
                // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
                // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
                direction += (e.isShiftDown() ? -1 : 1);
                direction = (direction + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
                setDirectionForColumn(columnIndex, direction);
//                viewToModel = null;
                initViewToModel();
                fireTableDataChanged();
            }
        }
 
Example 8
Source File: TableSorter.java    From jplag with GNU General Public License v3.0 6 votes vote down vote up
public void mouseClicked(MouseEvent e) {
    JTableHeader h = (JTableHeader) e.getSource();
    TableColumnModel columnModel = h.getColumnModel();
    int viewColumn = columnModel.getColumnIndexAtX(e.getX());
    int column = columnModel.getColumn(viewColumn).getModelIndex();
    if (column != -1) {
        int status = getSortingStatus(column);
        if (!e.isControlDown()) {
            cancelSorting();
        }
        // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or 
        // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed. 
        status = status + (e.isShiftDown() ? -1 : 1);
        status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
        setSortingStatus(column, status);
    }
}
 
Example 9
Source File: CTableSorter.java    From binnavi with Apache License 2.0 6 votes vote down vote up
@Override
public void mouseClicked(final MouseEvent e) {
  final JTableHeader h = (JTableHeader) e.getSource();
  final TableColumnModel columnModel = h.getColumnModel();
  final int viewColumn = columnModel.getColumnIndexAtX(e.getX());
  final int column = columnModel.getColumn(viewColumn).getModelIndex();
  if (column != -1) {
    int status = getSortingStatus(column);
    if (!e.isControlDown()) {
      cancelSorting();
    }
    // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
    // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
    status = status + (e.isShiftDown() ? -1 : 1);
    status = ((status + 4) % 3) - 1; // signed mod, returning {-1, 0, 1}
    setSortingStatus(column, status);
  }
}
 
Example 10
Source File: ExtendedJTableSorterModel.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
	JTableHeader h = (JTableHeader) e.getSource();
	TableColumnModel columnModel = h.getColumnModel();
	int viewColumn = getSortingColumnIndex(h, e.getPoint());
	if (viewColumn != -1) {
		int column = columnModel.getColumn(viewColumn).getModelIndex();
		if (column != -1) {
			int status = getSortingStatus(column);
			if (!SwingTools.isControlOrMetaDown(e)) {
				cancelSorting();
			}
			// Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
			// {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is
			// pressed.
			status = status + (e.isShiftDown() ? -1 : 1);
			status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
			setSortingStatus(column, status);
		}
		e.consume();
	}
}
 
Example 11
Source File: FileTable.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
	if (!e.isPopupTrigger()) {
		JTableHeader h = (JTableHeader) e.getSource();
		TableColumnModel columnModel = h.getColumnModel();
		int viewColumn = columnModel.getColumnIndexAtX(e.getX());
		int column = columnModel.getColumn(viewColumn).getModelIndex();
		if (column != -1) {
			if (columnModel.getColumn(viewColumn).getHeaderValue().equals(FILE_NAME)) {
				FileTable.this.fileList.orderBy(FileList.ORDER_BY_FILE_NAME, false);
				FileTable.this.fileList.updateTableData();
			} else if (columnModel.getColumn(viewColumn).getHeaderValue().equals(TYPE)) {
				FileTable.this.fileList.orderBy(FileList.ORDER_BY_FILE_TYPE, false);
				FileTable.this.fileList.updateTableData();
			} else if (columnModel.getColumn(viewColumn).getHeaderValue().equals(LAST_MODIFIED)) {
				FileTable.this.fileList.orderBy(FileList.ORDER_BY_FILE_MODIFIED, false);
				FileTable.this.fileList.updateTableData();
			} else if (columnModel.getColumn(viewColumn).getHeaderValue().equals(SIZE)) {
				FileTable.this.fileList.orderBy(FileList.ORDER_BY_FILE_SIZE, false);
				FileTable.this.fileList.updateTableData();
			}
		}
	}
}
 
Example 12
Source File: TableSorter.java    From Logisim with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
	JTableHeader h = (JTableHeader) e.getSource();
	TableColumnModel columnModel = h.getColumnModel();
	int viewColumn = columnModel.getColumnIndexAtX(e.getX());
	int column = columnModel.getColumn(viewColumn).getModelIndex();
	if (column != -1) {
		int status = getSortingStatus(column);
		if (!e.isControlDown()) {
			cancelSorting();
		}
		// Cycle the sorting states through {NOT_SORTED, ASCENDING,
		// DESCENDING} or
		// {NOT_SORTED, DESCENDING, ASCENDING} depending on whether
		// shift is pressed.
		status = status + (e.isShiftDown() ? -1 : 1);
		status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0,
										// 1}
		setSortingStatus(column, status);
	}
}
 
Example 13
Source File: TableSorter.java    From evosql with Apache License 2.0 6 votes vote down vote up
public void mouseClicked(MouseEvent e) {

            JTableHeader     h           = (JTableHeader) e.getSource();
            TableColumnModel columnModel = h.getColumnModel();
            int              viewColumn  = h.columnAtPoint(e.getPoint());
            int column = columnModel.getColumn(viewColumn).getModelIndex();

            if (column != -1) {
                int status = getSortingStatus(column);

                if (!e.isControlDown()) {
                    cancelSorting();
                }

                // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
                // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
                status = status + (e.isShiftDown() ? -1
                                                   : 1);
                status = (status + 4) % 3 - 1;    // signed mod, returning {-1, 0, 1}

                setSortingStatus(column, status);
            }
        }
 
Example 14
Source File: TableSorter.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mouseClicked(MouseEvent e) {
  JTableHeader h = (JTableHeader) e.getComponent();
  TableColumnModel columnModel = h.getColumnModel();
  int viewColumn = columnModel.getColumnIndexAtX(e.getX());
  // ArrayIndexOutOfBoundsException: -1
  if (viewColumn < 0) {
    return;
  }
  int column = columnModel.getColumn(viewColumn).getModelIndex();
  if (column != -1) {
    int status = getSortingStatus(column) + (e.isShiftDown() ? -1 : 1);
    if (!e.isControlDown()) {
      cancelSorting();
    }
    // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
    // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
    // int d = e.isShiftDown() ? -1 : 1;
    // status = status + d;
    status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
    setSortingStatus(column, status);
  }
}
 
Example 15
Source File: TableSorter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void mouseClicked(MouseEvent e) {
    JTableHeader h = (JTableHeader) e.getSource();
    TableColumnModel columnModel = h.getColumnModel();
    int viewColumn = columnModel.getColumnIndexAtX(e.getX());
    int column = columnModel.getColumn(viewColumn).getModelIndex();
    if (column != -1) {
        int status = getSortingStatus(column);
        if (!e.isControlDown()) {
            cancelSorting();
        }
        // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or 
        // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed. 
        status = status + (e.isShiftDown() ? -1 : 1);
        status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
        setSortingStatus(column, status);
    }
}
 
Example 16
Source File: TableSorter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
    JTableHeader h = (JTableHeader) e.getSource();
    TableColumnModel columnModel = h.getColumnModel();
    int viewColumn = columnModel.getColumnIndexAtX(e.getX());
    int column = columnModel.getColumn(viewColumn).getModelIndex();
    if (column != -1) {
        int status = getSortingStatus(column);
        if (!e.isControlDown()) {
            cancelSorting();
        }
        // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or 
        // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed. 
        status += e.isShiftDown() ? -1 : 1;
        status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
        setSortingStatus(column, status);
        if(issueTable != null) {
            issueTable.sortOrderChanged();
        }
    }
}
 
Example 17
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  JTableHeader h = (JTableHeader) e.getComponent();
  int idx = h.columnAtPoint(e.getPoint());
  if (idx < 0) {
    return;
  }
  TableColumnModel m = h.getColumnModel();
  Object title = m.getColumn(idx).getHeaderValue();
  cardLayout.show(contentsPanel, Objects.toString(title));
  selectedColumn = title;
}
 
Example 18
Source File: JTableHeaderJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private List<IJavaElement> findmatch(ArrayList<IJavaElement> r, PropertyPredicate p) {
    JTableHeader header = (JTableHeader) component;
    TableColumnModel columnModel = header.getColumnModel();
    int col = columnModel.getColumnCount();
    for (int i = 0; i < col; i++) {
        JTableHeaderItemJavaElement e = new JTableHeaderItemJavaElement(this, i);
        if (p.isValid(e)) {
            r.add(e);
        }
    }
    return r;
}
 
Example 19
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();

    }
 
Example 20
Source File: TableSorter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
    JTableHeader h = (JTableHeader) e.getSource();
    JTable table = h.getTable();
    int selectedRow = table.getSelectedRow();
    TableModel model = table.getModel();
    //remember selection to keep after sorting
    Object selectedAction=null;
    int objectColumn=-1;
    if(selectedRow>-1) {
        for(int i=0; i<table.getColumnCount(); i++) {
            //first find colum with appropriate object
            if(model.getValueAt(selectedRow, i) instanceof ActionHolder) {
                //remember this object
                selectedAction=model.getValueAt(selectedRow, i);
                objectColumn=i;
                //stop edition as we click somewhere ouside of editor
                TableCellEditor editor=table.getCellEditor();
                if(editor!=null) {
                    editor.stopCellEditing();
                }
                break;
            }
        }
    }
    TableColumnModel columnModel = h.getColumnModel();
    int viewColumn = columnModel.getColumnIndexAtX(e.getX());
    int column = columnModel.getColumn(viewColumn).getModelIndex();
    if (column != -1) {
        int status = getSortingStatus(column);
        if (!e.isControlDown()) {
            cancelSorting();
        }
        // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or 
        // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed. 
        status = status + (e.isShiftDown() ? -1 : 1);
        status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
        setSortingStatus(column, status);
        //reselect the same object
        if(selectedAction!=null)setSelectedRow(table, selectedAction, objectColumn);
    }
}