Example usage for javax.swing.table JTableHeader addMouseListener

List of usage examples for javax.swing.table JTableHeader addMouseListener

Introduction

In this page you can find the example usage for javax.swing.table JTableHeader addMouseListener.

Prototype

public synchronized void addMouseListener(MouseListener l) 

Source Link

Document

Adds the specified mouse listener to receive mouse events from this component.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable(5, 5);
    JTableHeader header = table.getTableHeader();
    header.addMouseListener(new ColumnHeaderListener());
}

From source file:ListProperties.java

public static void install(TableSorter sorter, JTable table) {
    TableHeaderSorter tableHeaderSorter = new TableHeaderSorter();
    tableHeaderSorter.sorter = sorter;//from   w  ww . j a  va  2  s .c o  m
    tableHeaderSorter.table = table;
    JTableHeader tableHeader = table.getTableHeader();
    tableHeader.addMouseListener(tableHeaderSorter);
}

From source file:TableSortDemo.java

public TableSortDemo() {
    super();/*from   w  w  w. j  a v  a2 s . c o  m*/
    setSize(600, 300);

    tableModel = new MyTableModel();

    getContentPane().add(titleLabel, BorderLayout.NORTH);
    table.setModel(tableModel);

    JTableHeader header = table.getTableHeader();
    header.setUpdateTableInRealTime(true);
    header.addMouseListener(tableModel.new ColumnListener(table));
    header.setReorderingAllowed(true);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:StocksTable4.java

public StocksTable4() {
    super("Stocks Table");
    setSize(600, 300);//  w  w w.ja va  2  s .  co m

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(true);
    header.addMouseListener(m_data.new ColumnListener(m_table));
    header.setReorderingAllowed(true);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:StocksTable5.java

public StocksTable5() {
    super("Stocks Table");
    setSize(600, 300);//from ww  w  . ja  v  a 2 s  . c o m

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(true);
    header.addMouseListener(m_data.new ColumnListener(m_table));
    header.setReorderingAllowed(true);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    JMenuBar menuBar = createMenuBar();
    setJMenuBar(menuBar);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:com.mirth.connect.client.ui.components.MirthTreeTable.java

public MirthTreeTable(String prefix, Set<String> defaultVisibleColumns) {
    customHiddenColumnMap = new HashMap<String, Set<String>>();

    this.prefix = prefix;
    this.defaultVisibleColumns = defaultVisibleColumns;

    columnOrderMap = new HashMap<String, Integer>();
    sortOrderColumn = -1;/* w  ww.  j  av a  2 s .c o m*/
    sortOrder = null;

    if (StringUtils.isNotEmpty(prefix)) {
        try {
            userPreferences = Preferences.userNodeForPackage(Mirth.class);
            String columns = userPreferences.get(prefix + "ColumnOrderMap", "");

            if (StringUtils.isNotEmpty(columns)) {
                columnOrderMap = (Map<String, Integer>) ObjectXMLSerializer.getInstance().deserialize(columns,
                        Map.class);
            }
        } catch (Exception e) {
        }

        try {
            String order = userPreferences.get(prefix + "SortOrder", "");

            if (StringUtils.isNotEmpty(order)) {
                sortOrder = ObjectXMLSerializer.getInstance().deserialize(order, SortOrder.class);
                sortOrderColumn = userPreferences.getInt(prefix + "SortOrderColumn", -1);
            }
        } catch (Exception e) {
        }
    }

    addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
            boolean isAccelerated = (((e.getModifiers()
                    & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) > 0)
                    || ((e.getModifiers() & InputEvent.CTRL_MASK) > 0));
            if ((e.getKeyCode() == KeyEvent.VK_S) && isAccelerated) {
                PlatformUI.MIRTH_FRAME.doContextSensitiveSave();
            }
        }

        public void keyReleased(KeyEvent e) {
        }

        public void keyTyped(KeyEvent e) {
        }
    });

    /*
     * Swingx 1.0 has this set to true by default, which doesn't allow dragging and dropping
     * into tables. Swingx 0.8 had this set to false. Tables that want it set to true can
     * override it.
     */
    putClientProperty("terminateEditOnFocusLost", Boolean.FALSE);

    JTableHeader header = getTableHeader();
    header.addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent e) {
            saveColumnOrder();
        }
    });
    header.setDefaultRenderer(new SortableHeaderCellRenderer(header.getDefaultRenderer()));

    final JButton columnControlButton = new JButton(new ColumnControlButton(this).getIcon());

    columnControlButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JPopupMenu columnMenu = getColumnMenu();
            Dimension buttonSize = columnControlButton.getSize();
            int xPos = columnControlButton.getComponentOrientation().isLeftToRight()
                    ? buttonSize.width - columnMenu.getPreferredSize().width
                    : 0;
            columnMenu.show(columnControlButton, xPos, columnControlButton.getHeight());
        }

    });

    setColumnControl(columnControlButton);
}

From source file:SortableTable.java

/**
 * Standard constructor - builds a table for the specified model.
 *
 * @param model  the data.//from  w ww  .j  a  v  a 2s.co m
 */
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);

}

From source file:gda.gui.BatonPanel.java

private void initGUI() {
    try {//from  w ww.  j  a  v a  2  s .c om
        {
            BorderLayout thisLayout = new BorderLayout();
            this.setLayout(thisLayout);
            this.setPreferredSize(new java.awt.Dimension(902, 431));
        }
        {
            JPanel jPanel1 = new JPanel();
            this.add(jPanel1, BorderLayout.WEST);
            this.add(getPnlMessages(), BorderLayout.CENTER);
            jPanel1.setLayout(null);
            jPanel1.setPreferredSize(new java.awt.Dimension(480, 419));
            {
                sudoPanel = new JPanel();
                BorderLayout sudoPanelLayout = new BorderLayout();
                sudoPanel.setLayout(sudoPanelLayout);
                jPanel1.add(sudoPanel);
                sudoPanel.add(getPnlSudoButtons(), BorderLayout.CENTER);
                sudoPanel.add(getPnlSUMessage(), BorderLayout.NORTH);
                sudoPanel.setBorder(BorderFactory.createTitledBorder("Switch User"));
                sudoPanel.setBounds(12, 340, 456, 80);
            }
            {
                otherClientPanel = new JPanel();
                jPanel1.add(otherClientPanel);
                otherClientPanel.setBorder(BorderFactory.createTitledBorder("Clients on this beamline"));
                otherClientPanel.setBounds(12, 15, 456, 215);
                otherClientPanel.setLayout(null);
                {
                    lblUser = new JLabel();
                    otherClientPanel.add(lblUser);
                    lblUser.setText("You are logged in as: abc123");
                    lblUser.setBounds(15, 36, 422, 14);
                    lblUser.setHorizontalAlignment(SwingConstants.CENTER);
                    lblUser.setHorizontalTextPosition(SwingConstants.CENTER);
                }
                {
                    lblBaton = new JLabel();
                    otherClientPanel.add(lblBaton);
                    lblBaton.setText("You hold the baton and have control of the beamline");
                    lblBaton.setBounds(15, 66, 422, 14);
                    lblBaton.setHorizontalTextPosition(SwingConstants.CENTER);
                    lblBaton.setInheritsPopupMenu(false);
                    lblBaton.setHorizontalAlignment(SwingConstants.CENTER);
                }
                {
                    JScrollPane jScrollPane1 = new JScrollPane();
                    otherClientPanel.add(jScrollPane1);
                    jScrollPane1.setBounds(73, 96, 306, 97);
                    {
                        userClients = new JTable();
                        jScrollPane1.setViewportView(userClients);
                        userClients.setModel(this.new BatonTableModel());
                        userClients.setAutoCreateRowSorter(true);
                        userClients.setPreferredSize(new java.awt.Dimension(213, 97));
                        userClients.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
                        userClients.setColumnSelectionAllowed(false);
                        userClients.getSelectionModel().addListSelectionListener(this);

                        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                        renderer.setHorizontalAlignment(SwingConstants.CENTER);
                        userClients.getColumnModel().getColumn(0).setCellRenderer(renderer);
                        userClients.getColumnModel().getColumn(1).setCellRenderer(renderer);
                        userClients.getColumnModel().getColumn(2).setCellRenderer(renderer);

                        JTableHeader th = userClients.getTableHeader();
                        th.addMouseListener(this);

                    }
                }
            }
            {
                buttonsPanel = new JPanel();
                BorderLayout buttonsPanelLayout = new BorderLayout();
                buttonsPanel.setLayout(buttonsPanelLayout);
                jPanel1.add(buttonsPanel);
                buttonsPanel.setBorder(BorderFactory.createTitledBorder("Baton control"));
                buttonsPanel.setBounds(12, 242, 456, 92);
                buttonsPanel.add(getPnlControlButton(), BorderLayout.CENTER);
                buttonsPanel.add(getPnlTickBox(), BorderLayout.SOUTH);
            }
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:com.projity.pm.graphic.spreadsheet.SpreadSheet.java

public void setModel(SpreadSheetModel spreadSheetModel, SpreadSheetColumnModel spreadSheetColumnModel) {
    makeCustomTableHeader(spreadSheetColumnModel);
    TableModel oldModel = getModel();
    setModel(spreadSheetModel);/*  ww  w  . jav  a 2  s. c o  m*/

    if (spreadSheetColumnModel != null) {
        //System.out.println("creating new ColModel");
        setColumnModel(spreadSheetColumnModel);

        selection = new SpreadSheetSelectionModel(this);
        selection.setRowSelection(new SpreadSheetListSelectionModel(selection, true));
        selection.setColumnSelection(new SpreadSheetListSelectionModel(selection, false));
        setSelectionModel(selection.getRowSelection());
        createDefaultColumnsFromModel(spreadSheetModel.getFieldArray()); //Consume memory
        getColumnModel().setSelectionModel(selection.getColumnSelection());
    }

    registerEditors(); //Consume memory
    initRowHeader(spreadSheetModel);
    initModel();
    initListeners();

    GraphicConfiguration config = GraphicConfiguration.getInstance();
    //fix for substance
    setTableHeader(createDefaultTableHeader());
    JTableHeader header = getTableHeader();
    header.setPreferredSize(
            new Dimension((int) header.getPreferredSize().getWidth(), config.getColumnHeaderHeight()));
    header.addMouseListener(new HeaderMouseListener(this));

    addMouseListener(new MouseAdapter() {
        //         Cursor oldCursor = null;
        //         public void mouseEntered(MouseEvent e) {
        //            Point p = e.getPoint();
        //            int col = columnAtPoint(p);
        //            Field field = ((SpreadSheetModel) getModel()).getFieldInNonTranslatedColumn(col + 1);
        //            System.out.println("mouse entered field " + field);
        //            if (field != null && field.isHyperlink()) {
        //               oldCursor = getCursor();
        //               setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        //               System.out.println("setting new cursor to " + Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) + " old is " + oldCursor);
        //            } else 
        //               super.mouseEntered(e);
        //
        //         }
        //
        //         public void mouseExited(MouseEvent e) {
        //            Point p = e.getPoint();
        //            int col = columnAtPoint(p);
        //            Field field = ((SpreadSheetModel) getModel()).getFieldInNonTranslatedColumn(col + 1);
        //            System.out.println("mouse exited field " + field);
        //            if (field != null && field.isHyperlink()) {
        //               setCursor(oldCursor);
        //               System.out.println("setting old cursor to " + oldCursor);
        //               e.consume();
        //            } else 
        //               super.mouseEntered(e);
        //         }

        public void mousePressed(MouseEvent e) { // changed to mousePressed instead of mouseClicked() for snappier handling 17/5/04 hk
            Point p = e.getPoint();
            int row = rowAtPoint(p);
            int col = columnAtPoint(p);
            SpreadSheetPopupMenu popup = getPopup();
            if (SwingUtilities.isLeftMouseButton(e)) {
                SpreadSheetColumnModel columnModel = (SpreadSheetColumnModel) getColumnModel();
                Field field = ((SpreadSheetModel) getModel()).getFieldInNonTranslatedColumn(col + 1);
                SpreadSheetModel model = (SpreadSheetModel) getModel();
                if (field.isNameField()) {
                    // if (col == columnModel.getNameIndex()) {
                    GraphicNode node = model.getNode(row);
                    if (isOnIcon(e)) {
                        if (model.getCellProperties(node).isCompositeIcon()) {
                            finishCurrentOperations();
                            selection.getRowSelection().clearSelection();
                            boolean change = true;
                            if (!node.isFetched()) // for subprojects
                                change = node.fetch();
                            if (change)
                                model.changeCollapsedState(row);
                            e.consume(); // prevent dbl click treatment below

                            // because editor may have already been
                            // installed we
                            // have to update its collapsed state
                            // updateNameCellEditor(node);

                            // editCellAt(row,model.findGraphicNodeRow(node));
                        }
                    }
                } else if (field != null && field.isHyperlink()) {
                    Hyperlink link = (Hyperlink) model.getValueAt(row, col + 1);
                    if (link != null) {
                        BrowserControl.displayURL(link.getAddress());
                        e.consume(); // prevent dbl click treatment below
                    }

                }
                if (!e.isConsumed()) {
                    if (e.getClickCount() == 2) // if above code didn't treat and is dbl click
                        doDoubleClick(row, col);
                    else
                        doClick(row, col);
                }

            } else if (popup != null && SwingUtilities.isRightMouseButton(e)) { // e.isPopupTrigger() can be used too
                //               selection.getRowSelection().clearSelection();
                //               selection.getRowSelection().addSelectionInterval(row, row);
                popup.setRow(row);
                popup.setCol(col);
                popup.show(SpreadSheet.this, e.getX(), e.getY());
            }
        }
    });

    if (oldModel != spreadSheetModel && oldModel instanceof CommonSpreadSheetModel)
        ((CommonSpreadSheetModel) getModel()).getCache().removeNodeModelListener(this);
    spreadSheetModel.getCache().addNodeModelListener(this);

    //      getColumnModel().addColumnModelListener(new TableColumnModelListener(){
    //         public void columnAdded(TableColumnModelEvent e) {
    //            // TODO Auto-generated method stub
    //            
    //         }
    //         public void columnMarginChanged(ChangeEvent e) {
    //            // TODO Auto-generated method stub
    //            
    //         }
    //         public void columnMoved(TableColumnModelEvent e) {
    //            // TODO Auto-generated method stub
    //            
    //         }
    //         public void columnRemoved(TableColumnModelEvent e) {
    //            // TODO Auto-generated method stub
    //            
    //         }
    //         public void columnSelectionChanged(ListSelectionEvent e) {
    //            System.out.println(((e.getValueIsAdjusting())?"lse=":"LSE=")+e.getFirstIndex()+", "+e.getLastIndex());
    //            SpreadSheet.this.revalidate();
    //            //SpreadSheet.this.paintImmediately(0, 0, getWidth(), GraphicConfiguration.getInstance().getColumnHeaderHeight());
    //         }
    //      });

}

From source file:com.aw.swing.mvp.binding.component.BndSJTable.java

private void configureJTable() {
    LookAndFeelManager.setLookAndFeel(jTable);
    //by default allow single line selection
    if (isScrollable || fixedWidth) {
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }//w w  w .j  a  va2s .c  o m
    if (createAutoSortCols) {
        jTable.setAutoCreateRowSorter(true);
    }
    jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JTableMouseProcessor jTableMouseProcessor = new JTableMouseProcessor(presenter, this, gridIndex);
    jTable.addMouseListener(getJTableMouseListener(jTableMouseProcessor));
    JTableHeader header = jTable.getTableHeader();
    header.addMouseListener(getJTableHeaderMouseListener(jTableMouseProcessor));
    header.setReorderingAllowed(false);
    jTable.setName("grid" + gridIndex);
}