下面列出了javax.swing.table.JTableHeader#getColumnModel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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();
}
}
}
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);
}
}
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);
}
}
@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);
}
}
@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();
}
}
}
}
@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();
}
}
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);
}
}
}
}
@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);
}
}
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);
}
}
@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();
}
}
@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));
}
}
@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));
}
}
@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();
}
}
@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());
}
}
@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));
}
}
@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);
}
}
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;
}
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();
}
@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;
}
@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);
}
}