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

The following examples show how to use javax.swing.table.JTableHeader#addMouseListener() . 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: TableSorter.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
}
 
Example 2
Source File: SortableTable.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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);

}
 
Example 3
Source File: TableSorter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: TableSorter.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
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));
}
 
Example 6
Source File: TableSorter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 7
Source File: DataManager.java    From iBioSim with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: SortableAndSearchableWrapperTableModel.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 9
Source File: TableSorter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    tableView = table;
    columnModel = tableView.getColumnModel();
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(this);
}
 
Example 10
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
protected HeaderRenderer(JTableHeader header, JPopupMenu pop) {
  super();
  this.pop = pop;
  header.addMouseListener(handler);
  header.addMouseMotionListener(handler);
}
 
Example 11
Source File: TableSorter.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    tableView = table;
    columnModel = tableView.getColumnModel();
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(this);
}
 
Example 12
Source File: TableSorter.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    tableView = table;
    columnModel = tableView.getColumnModel();
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(this);
}
 
Example 13
Source File: TableSorter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    tableView = table;
    columnModel = tableView.getColumnModel();
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(this);
}
 
Example 14
Source File: TableSorter.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    tableView = table;
    columnModel = tableView.getColumnModel();
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(this);
}
 
Example 15
Source File: FeatureSelectorPanel.java    From jAudioGIT with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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();
}
 
Example 16
Source File: TableSorter.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    tableView = table;
    columnModel = tableView.getColumnModel();
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(this);
}
 
Example 17
Source File: JSortTable.java    From tn5250j with GNU General Public License v2.0 4 votes vote down vote up
private void initSortHeader() {
  JTableHeader header = getTableHeader();
  header.setDefaultRenderer(new SortHeaderRenderer());
  header.addMouseListener(this);
}
 
Example 18
Source File: TableSorter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    tableView = table;
    columnModel = tableView.getColumnModel();
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(this);
}
 
Example 19
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
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));
}
 
Example 20
Source File: TableSorter.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    tableView = table;
    columnModel = tableView.getColumnModel();
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(this);
}