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);
}
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;
}
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;
}
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);
}
}
});
}
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);
}
Aggregations