下面列出了javax.swing.table.JTableHeader#addMouseListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void addMouseListenerToHeaderInTable(JTable table) {
final TableSorter sorter = this;
final JTable tableView = table;
tableView.setColumnSelectionAllowed(false);
final MouseAdapter listMouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
final TableColumnModel columnModel = tableView.getColumnModel();
final int viewColumn = columnModel.getColumnIndexAtX(e.getX());
final int column = tableView.convertColumnIndexToModel(viewColumn);
if (e.getClickCount() == 1 && column != -1) {
//System.out.println("Sorting ...");
final int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK;
final boolean ascending = (shiftPressed == 0);
sorter.sortByColumn(column, ascending);
}
}
};
final JTableHeader th = tableView.getTableHeader();
th.addMouseListener(listMouseListener);
}
private MainPanel() {
super(new BorderLayout());
JTable table = new JTable(makeModel());
table.setFillsViewportHeight(true);
table.setRowSelectionAllowed(true);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setComponentPopupMenu(new TablePopupMenu(table));
JTableHeader header = table.getTableHeader();
header.setDefaultRenderer(new SortButtonRenderer());
header.addMouseListener(new HeaderMouseListener());
// header.setReorderingAllowed(false);
TableColumn col = table.getColumnModel().getColumn(0);
col.setMinWidth(80);
col.setMaxWidth(80);
add(new JScrollPane(table));
add(makeToolBar(table), BorderLayout.NORTH);
setPreferredSize(new Dimension(320, 240));
}
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);
}
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);
}
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);
}
/**
* Standard constructor - builds a table for the specified model.
*
* @param model the data.
*/
public SortableTable(final SortableTableModel model) {
super(model);
final SortButtonRenderer renderer = new SortButtonRenderer();
final TableColumnModel cm = getColumnModel();
for (int i = 0; i < cm.getColumnCount(); i++) {
cm.getColumn(i).setHeaderRenderer(renderer);
}
final JTableHeader header = getTableHeader();
this.headerListener = new SortableTableHeaderListener(model, renderer);
header.addMouseListener(this.headerListener);
header.addMouseMotionListener(this.headerListener);
model.sortByColumn(0, true);
}
/**
* Adds a mouselistener to the header: left-click on the header sorts in
* ascending manner, using shift-left-click in descending manner.
*
* @param table the table to add the listener to
*/
public void addMouseListenerToHeader(JTable table) {
final SortableAndSearchableWrapperTableModel fModel;
final JTable fTable;
JTableHeader header;
fModel = this;
fTable = table;
fTable.setColumnSelectionAllowed(false);
header = fTable.getTableHeader();
if (header != null) {
if (m_MouseListener == null) {
m_MouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
TableColumnModel columnModel = fTable.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = fTable.convertColumnIndexToModel(viewColumn);
if (MouseUtils.isLeftClick(e) && !e.isAltDown() && !e.isControlDown() && (column != -1))
fModel.sort(column, !e.isShiftDown());
}
};
}
header.addMouseListener(m_MouseListener);
}
}
public static void install(TableSorter sorter, JTable table) {
TableHeaderSorter tableHeaderSorter = new TableHeaderSorter();
tableHeaderSorter.sorter = sorter;
tableHeaderSorter.table = table;
JTableHeader tableHeader = table.getTableHeader();
tableHeader.addMouseListener(tableHeaderSorter);
}
public void addMouseListenerToHeaderInTable(JTable table) {
tableView = table;
columnModel = tableView.getColumnModel();
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(this);
}
private MainPanel() {
super(new BorderLayout());
DefaultListModel<String> listModel = new DefaultListModel<>();
RowDataModel model = new RowDataModel(listModel);
model.addRowData(new RowData("Name 1", "comment"));
model.addRowData(new RowData("Name 2", "test"));
model.addRowData(new RowData("Name d", "ee"));
model.addRowData(new RowData("Name c", "test cc"));
model.addRowData(new RowData("Name b", "test bb"));
model.addRowData(new RowData("Name a", "ff"));
model.addRowData(new RowData("Name 0", "test aa"));
model.addRowData(new RowData("Name 0", "gg"));
JTable table = new JTable(model);
table.setCellSelectionEnabled(true);
JTableHeader header = table.getTableHeader();
header.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
if (table.isEditing()) {
table.getCellEditor().stopCellEditing();
}
int col = header.columnAtPoint(e.getPoint());
table.changeSelection(0, col, false, false);
table.changeSelection(table.getRowCount() - 1, col, false, true);
}
});
RowHeaderList<String> rowHeader = new RowHeaderList<>(listModel, table);
rowHeader.setFixedCellWidth(50);
JScrollPane scroll = new JScrollPane(table);
scroll.setRowHeaderView(rowHeader);
scroll.getRowHeader().addChangeListener(e ->
scroll.getVerticalScrollBar().setValue(((JViewport) e.getSource()).getViewPosition().y));
scroll.setComponentPopupMenu(new TablePopupMenu());
table.setInheritsPopupMenu(true);
rowHeader.setBackground(Color.BLUE);
scroll.setBackground(Color.RED);
scroll.getViewport().setBackground(Color.GREEN);
add(scroll);
setPreferredSize(new Dimension(320, 240));
}
public void addMouseListenerToHeaderInTable(JTable table) {
tableView = table;
columnModel = tableView.getColumnModel();
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(this);
}
private void initSortHeader() {
JTableHeader header = getTableHeader();
header.setDefaultRenderer(new SortHeaderRenderer());
header.addMouseListener(this);
}
public void addMouseListenerToHeaderInTable(JTable table) {
tableView = table;
columnModel = tableView.getColumnModel();
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(this);
}
/**
* Initialize the table displaying the features which can be extracted.
*/
private void setUpFeatureTable() {
controller.fstm_.fillTable(controller.dm_.featureDefinitions,
controller.dm_.defaults, controller.dm_.is_primary);
decorator = new SortingTableModelDecorator(controller.fstm_);
features_table = new JTable(decorator);
multipleToggleAction = new MultipleToggleAction(features_table);
String key = "MultipleToggleAction";
features_table.getInputMap().put(KeyStroke.getKeyStroke(' '), key);
features_table.getActionMap().put(key, multipleToggleAction);
int[] width = new int[3];
width[0] = decorator.getRealPrefferedWidth(features_table, 0);
width[1] = decorator.getRealPrefferedWidth(features_table, 1);
width[1] -= 100;
width[2] = decorator.getRealPrefferedWidth(features_table, 2);
for (int i = 0; i < width.length; ++i) {
features_table.getColumnModel().getColumn(i).setPreferredWidth(
width[i]);
}
// add handler for sorting panel
JTableHeader header = (JTableHeader) features_table.getTableHeader();
header.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
TableColumnModel tcm = features_table.getColumnModel();
int column = features_table.convertColumnIndexToModel(tcm
.getColumnIndexAtX(e.getX()));
decorator.sort(column);
} else {
decorator.resetIndeci();
}
}
});
// Set up and display the table
features_scroll_pane = new JScrollPane(features_table);
features_panel = new JPanel(new GridLayout(1, 1));
features_panel.add(features_scroll_pane);
add(features_panel, BorderLayout.CENTER);
controller.fstm_.fireTableDataChanged();
TableColumn tableColumn = features_table.getColumn(features_table
.getColumnName(1));
tableColumn.setCellRenderer(new FeatureDisplay());
features_table.removeColumn(features_table.getColumn(features_table
.getColumnName(3)));
repaint();
outer_frame.repaint();
}
public void addMouseListenerToHeaderInTable(JTable table) {
tableView = table;
columnModel = tableView.getColumnModel();
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(this);
}
public void addMouseListenerToHeaderInTable(JTable table) {
tableView = table;
columnModel = tableView.getColumnModel();
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(this);
}
public void addMouseListenerToHeaderInTable(JTable table) {
tableView = table;
columnModel = tableView.getColumnModel();
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(this);
}
public void addMouseListenerToHeaderInTable(JTable table) {
tableView = table;
columnModel = tableView.getColumnModel();
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(this);
}
protected HeaderRenderer(JTableHeader header, JPopupMenu pop) {
super();
this.pop = pop;
header.addMouseListener(handler);
header.addMouseMotionListener(handler);
}
public void addMouseListenerToHeaderInTable(JTable table) {
tableView = table;
columnModel = tableView.getColumnModel();
JTableHeader th = tableView.getTableHeader();
th.addMouseListener(this);
}