Java Code Examples for javax.swing.JTable#getTableHeader()

The following examples show how to use javax.swing.JTable#getTableHeader() . 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 jdk8u-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 2
Source File: TableSorter.java    From TencentKona-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 3
Source File: TableSorter.java    From jdk8u60 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-jdk8u 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: 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 6
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 7
Source File: TableColumn.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * As of Java 2 platform v1.3, this method is not called by the <code>TableColumn</code>
 * constructor.  Previously this method was used by the
 * <code>TableColumn</code> to create a default header renderer.
 * As of Java 2 platform v1.3, the default header renderer is <code>null</code>.
 * <code>JTableHeader</code> now provides its own shared default
 * renderer, just as the <code>JTable</code> does for its cell renderers.
 *
 * @return the default header renderer
 * @see javax.swing.table.JTableHeader#createDefaultRenderer()
 */
protected TableCellRenderer createDefaultHeaderRenderer() {
    DefaultTableCellRenderer label = new DefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value,
                     boolean isSelected, boolean hasFocus, int row, int column) {
            if (table != null) {
                JTableHeader header = table.getTableHeader();
                if (header != null) {
                    setForeground(header.getForeground());
                    setBackground(header.getBackground());
                    setFont(header.getFont());
                }
            }

            setText((value == null) ? "" : value.toString());
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            return this;
        }
    };
    label.setHorizontalAlignment(JLabel.CENTER);
    return label;
}
 
Example 8
Source File: TimelineHeaderRenderer.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    boolean timelineColumn = column == table.convertColumnIndexToModel(col);
    setVisible(timelineColumn);
    
    Component c = impl.getTableCellRendererComponent(table, timelineColumn ?
                  " " : value, isSelected, hasFocus, row, col); // NOI18N
    if (timelineColumn) {
        if (getParent() == null && c instanceof Container) ((Container)c).add(this);

        JTableHeader header = table.getTableHeader();
        if (painter == null) initStaticUI(c, header);
        
        TableColumn _column = header.getColumnModel().getColumn(col);
        
        setSize(_column.getWidth(), header.getSize().height);
    }
    
    return c;
}
 
Example 9
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) {
        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 10
Source File: QueryPortBelongs.java    From SimpleERP with Apache License 2.0 6 votes vote down vote up
public static void FitTableColumns(JTable myTable){
	  JTableHeader header = myTable.getTableHeader();
	     int rowCount = myTable.getRowCount();
	     Enumeration columns = myTable.getColumnModel().getColumns();
	     while(columns.hasMoreElements()){
	         TableColumn column = (TableColumn)columns.nextElement();
	         int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
	         int width = (int)myTable.getTableHeader().getDefaultRenderer()
	                 .getTableCellRendererComponent(myTable, column.getIdentifier()
	                         , false, false, -1, col).getPreferredSize().getWidth();
	         for(int row = 0; row<rowCount; row++){
	             int preferedWidth = (int)myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable,
	               myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
	             width = Math.max(width, preferedWidth);
	         }
	         header.setResizingColumn(column); // ���к���Ҫ
	         column.setWidth(width+myTable.getIntercellSpacing().width);
	     }
}
 
Example 11
Source File: ClassLevelTableModel.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void initializeTable(JTable classLevelTable)
{
	JTableHeader tableHeader = classLevelTable.getTableHeader();
	tableHeader.setResizingAllowed(false);
	tableHeader.setReorderingAllowed(false);
	TableColumnModel columnModel = new DefaultTableColumnModel();
	TableCellRenderer headerRenderer = tableHeader.getDefaultRenderer();
	columnModel.addColumn(Utilities.createTableColumn(0, "Level", headerRenderer, false));
	columnModel.addColumn(Utilities.createTableColumn(1, "HP", headerRenderer, false));
	columnModel.addColumn(Utilities.createTableColumn(2, "Class (All Levels In Class)", headerRenderer, true));
	classLevelTable.setColumnModel(columnModel);
	classLevelTable.setAutoCreateColumnsFromModel(false);
	classLevelTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
	classLevelTable.setFocusable(false);
	classLevelTable.setCellSelectionEnabled(false);
	classLevelTable.setRowHeight(20);
}
 
Example 12
Source File: HeaderClickCheckAllListenerTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
/**
 * Test of mouseClicked method, of class HeaderClickCheckAllListener.
 */
@Test
public void testMouseClicked_work()
{
   System.out.println("mouseClicked");
   JTable table=new JTable();
   table.addColumn(new TableColumn());
   MouseEvent evt = new MouseEvent(table.getTableHeader(), 1, 1, 1, 10, 10, 1, true);
   HeaderClickCheckAllListener instance = new HeaderClickCheckAllListener();
   instance.mouseClicked(evt);
}
 
Example 13
Source File: ColumnGroup.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
public ColumnGroup(TableCellRenderer renderer, String text) {
  if (renderer == null) {
    this.renderer = new DefaultTableCellRenderer() {

      /**
       * 
       */
      private static final long serialVersionUID = 1L;

      public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
        JTableHeader header = table.getTableHeader();
        if (header != null) {
          setForeground(header.getForeground());
          setBackground(header.getBackground());
          setFont(header.getFont());
        }
        setHorizontalAlignment(JLabel.CENTER);
        setText((value == null) ? "" : value.toString());
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        return this;
      }
    };
  } else {
    this.renderer = renderer;
  }
  this.text = text;
  v = new Vector<Object>();
}
 
Example 14
Source File: TableCheckBoxColumn.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
public static void installFor(JTable table, JPopupMenu popupMenu, int column) {
    removeOldHeaderMouseListener(table);
    TableModel model = table.getModel();
    model.addTableModelListener(new HeaderCheckBoxHandler(table));
    table.getTableHeader().addMouseListener(new HeaderMouseListener(popupMenu, column));
    TableCellRenderer r = new HeaderRenderer(table.getTableHeader(), popupMenu, column);
    table.getColumnModel().getColumn(column).setHeaderRenderer(r);
    TableCellRenderer leftAlign = new LeftAlignHeaderRenderer();
    for (int i = 0; i < table.getColumnCount(); i++) {
        if (i != column) {
            table.getColumnModel().getColumn(i).setHeaderRenderer(leftAlign);
        }
    }
}
 
Example 15
Source File: TableHeaderUI.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
		int row, int column) {
	if (table != null) {
		JTableHeader header = table.getTableHeader();
		if (header != null) {
			setFont(header.getFont());
		}
	}

	this.rollOver = column == TableHeaderUI.this.highlightedColumn;
	if (header != null) {
		curCol = column;
		this.isLeftmost = column == 0;
		this.isRightmost = column == header.getColumnModel().getColumnCount() - 1;
	}

	if (header != null && header.getDraggedColumn() != null) {
		this.isPressed = viewIndexForColumn(header.getDraggedColumn()) == column
				|| column == TableHeaderUI.this.pressedColumn;
	} else {
		this.isPressed = false;
	}
	setText(value == null ? "" : value.toString());
	setHorizontalAlignment(SwingConstants.LEFT);
	setHorizontalTextPosition(SwingConstants.LEADING);
	setBorder(HEADER_BORDER);
	return this;
}
 
Example 16
Source File: ColumnGroup.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
public ColumnGroup(TableCellRenderer renderer, String text) {
  if (renderer == null) {
    this.renderer = new DefaultTableCellRenderer() {

      /**
       * 
       */
      private static final long serialVersionUID = 1L;

      public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
        JTableHeader header = table.getTableHeader();
        if (header != null) {
          setForeground(header.getForeground());
          setBackground(header.getBackground());
          setFont(header.getFont());
        }
        setHorizontalAlignment(JLabel.CENTER);
        setText((value == null) ? "" : value.toString());
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        return this;
      }
    };
  } else {
    this.renderer = renderer;
  }
  this.text = text;
  v = new Vector<Object>();
}
 
Example 17
Source File: SortButtonRenderer.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the renderer component.
 *
 * @param table      the table.
 * @param value      the value.
 * @param isSelected selected?
 * @param hasFocus   focussed?
 * @param row        the row.
 * @param column     the column.
 * @return the renderer.
 */
public Component getTableCellRendererComponent(final JTable table,
                                               final Object value,
                                               final boolean isSelected,
                                               final boolean hasFocus,
                                               final int row, final int column) {

    if (table == null) {
        throw new NullPointerException("Table must not be null.");
    }

    final JComponent component;
    final SortableTableModel model = (SortableTableModel) table.getModel();
    final int cc = table.convertColumnIndexToModel(column);
    final boolean isSorting = (model.getSortingColumn() == cc);
    final boolean isAscending = model.isAscending();

    final JTableHeader header = table.getTableHeader();
    final boolean isPressed = (cc == this.pressedColumn);

    if (this.useLabels) {
        final JLabel label = getRendererLabel(isSorting, isAscending);
        label.setText((value == null) ? "" : value.toString());
        component = label;
    }
    else {
        final JButton button = getRendererButton(isSorting, isAscending);
        button.setText((value == null) ? "" : value.toString());
        button.getModel().setPressed(isPressed);
        button.getModel().setArmed(isPressed);
        component = button;
    }

    if (header != null) {
        component.setForeground(header.getForeground());
        component.setBackground(header.getBackground());
        component.setFont(header.getFont());
    }
    return component;
}
 
Example 18
Source File: TableUtils.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static void repaintTableHeader(JTable table) {
	// force an update on the headers so they display the new sorting order
	JTableHeader tableHeader = table.getTableHeader();
	if (tableHeader != null) {
		tableHeader.paintImmediately(tableHeader.getBounds());
	}
}
 
Example 19
Source File: Utils.java    From Method_Trace_Tool with Apache License 2.0 5 votes vote down vote up
public static void fitTableColumns(JTable myTable) {

        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(
                    column.getIdentifier());
            int width = (int) myTable
                    .getTableHeader()
                    .getDefaultRenderer()
                    .getTableCellRendererComponent(myTable,
                            column.getIdentifier(), false, false, -1, col)
                    .getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable
                        .getCellRenderer(row, col)
                        .getTableCellRendererComponent(myTable,
                                myTable.getValueAt(row, col), false, false,
                                row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // 此行很重要
            column.setWidth(width + myTable.getIntercellSpacing().width + 4);// 使表格看起来不是那么拥挤,起到间隔作用
        }
    }
 
Example 20
Source File: AccountsPanel.java    From btdex with GNU General Public License v3.0 4 votes vote down vote up
public AccountsPanel(Main main) {
	super(new BorderLayout());

	this.main = main;

	table = new JTable(model = new DefaultTableModel(COLUMN_NAMES, 0));
	for (int i = 0; i < COLUMN_NAMES.length; i++) {
		table.getColumnModel().getColumn(i).setHeaderValue(tr(COLUMN_NAMES[i]));
	}
	table.setRowHeight(table.getRowHeight()+7);
	table.setPreferredScrollableViewportSize(new Dimension(400, 200));
	table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	table.getSelectionModel().addListSelectionListener(this);

	left = new JPanel(new BorderLayout());
	right = new JPanel();
	right.setVisible(false);
	right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));

	left.setBorder(BorderFactory.createTitledBorder(tr("acc_your_accounts")));
	right.setBorder(BorderFactory.createTitledBorder(tr("acc_account_details")));

	JScrollPane scrollPane = new JScrollPane(table);
	table.setFillsViewportHeight(true);

	// Center header and all columns
	DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
	centerRenderer.setHorizontalAlignment( JLabel.CENTER );
	for (int i = 0; i < table.getColumnCount(); i++) {
		table.getColumnModel().getColumn(i).setCellRenderer( centerRenderer );			
	}
	JTableHeader jtableHeader = table.getTableHeader();
	DefaultTableCellRenderer rend = (DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer();
	rend.setHorizontalAlignment(JLabel.CENTER);
	jtableHeader.setDefaultRenderer(rend);

	table.setAutoCreateColumnsFromModel(false);
	table.getColumnModel().getColumn(COL_NAME).setPreferredWidth(200);
	table.getColumnModel().getColumn(COL_MARKET).setPreferredWidth(20);

	JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
	left.add(buttonPane, BorderLayout.PAGE_END);

	addButton = new JButton(tr("acc_add_button"));
	removeButton = new JButton(tr("acc_remove_button"));
	removeButton.setEnabled(false);

	addButton.addActionListener(this);
	removeButton.addActionListener(this);

	buttonPane.add(addButton);
	buttonPane.add(removeButton);

	marketComboBox = new JComboBox<Market>();
	for(Market m : Markets.getMarkets()) {
		if(m.getTokenID()!=null)
			continue;
		marketComboBox.addItem(m);
	}
	marketComboBox.addActionListener(this);

	JPanel topPanel = new JPanel(new SpringLayout());
	topPanel.add(new Desc(tr("main_market"), marketComboBox), BorderLayout.LINE_START);
	topPanel.add(new Desc(tr("acc_alias"), nameField = new JTextField()), BorderLayout.CENTER);
	SpringUtilities.makeCompactGrid(topPanel, 1, 2, 0, 0, PAD, PAD);
	right.add(topPanel);

	formPanel = new JPanel(new SpringLayout());
	//		JScrollPane formScroll = new JScrollPane(formPanel);
	//		right.add(formScroll);
	right.add(formPanel);

	cancelButton = new JButton(tr("dlg_cancel"));
	okButton = new JButton(tr("dlg_ok"));

	cancelButton.addActionListener(this);
	okButton.addActionListener(this);
	rightButtonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
	rightButtonPane.add(cancelButton);
	rightButtonPane.add(okButton);
	right.add(rightButtonPane);

	add(left, BorderLayout.LINE_START);
	JPanel rightContainer = new JPanel(new BorderLayout());
	rightContainer.add(right, BorderLayout.PAGE_START);
	add(rightContainer, BorderLayout.CENTER);

	left.add(scrollPane, BorderLayout.CENTER);	

	loadAccounts();
}