Search in sources :

Example 1 with JTableHeader

use of javax.swing.table.JTableHeader in project groovy by apache.

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() {

        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) {
                if (lastSortedColumn == column)
                    ascending = !ascending;
                sorter.sortByColumn(column, ascending);
                lastSortedColumn = column;
            }
        }
    };
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
Also used : MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) TableColumnModel(javax.swing.table.TableColumnModel) JTableHeader(javax.swing.table.JTableHeader)

Example 2 with JTableHeader

use of javax.swing.table.JTableHeader in project pcgen by PCGen.

the class CharacterHPDialog method initComponents.

private void initComponents() {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    JTable table = new JTable(tableModel) {

        @Override
        public TableCellEditor getCellEditor(int row, int column) {
            if (column == 5) {
                //TODO: the max roll should be calculated in a different manner
                String hd = levels.getClassTaken(levels.getElementAt(row)).getHD();
                int max = NumberUtils.toInt(hd);
                return new IntegerEditor(1, max);
            } else {
                return super.getCellEditor(row, column);
            }
        }
    };
    table.setDefaultRenderer(JButton.class, new Renderer());
    table.setDefaultEditor(JButton.class, new Editor());
    table.setCellSelectionEnabled(false);
    table.setRowHeight(new IntegerEditor(1, 10).getPreferredSize().height);
    JTableHeader header = table.getTableHeader();
    header.setReorderingAllowed(false);
    JScrollPane scrollPane = new JScrollPane(table);
    pane.add(scrollPane, BorderLayout.CENTER);
    Box box = Box.createHorizontalBox();
    box.add(new JLabel("Total Hp:"));
    box.add(Box.createHorizontalStrut(3));
    final ReferenceListener<Integer> hpListener = new ReferenceListener<Integer>() {

        @Override
        public void referenceChanged(ReferenceEvent<Integer> e) {
            totalHp.setText(e.getNewReference().toString());
        }
    };
    ReferenceFacade<Integer> hpRef = character.getTotalHPRef();
    totalHp.setText(hpRef.get().toString());
    hpRef.addReferenceListener(hpListener);
    box.add(totalHp);
    box.add(Box.createHorizontalStrut(5));
    JButton button = new JButton("Reroll All");
    button.setActionCommand("Reroll");
    button.addActionListener(this);
    box.add(button);
    box.add(Box.createHorizontalGlue());
    button = new JButton("Close");
    button.setActionCommand("Close");
    button.addActionListener(this);
    box.add(button);
    pane.add(box, BorderLayout.SOUTH);
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosed(WindowEvent e) {
            //Make sure to remove the listeners so that the garbage collector can
            //dispose of this dialog and prevent a memory leak
            levels.removeHitPointListener(tableModel);
            character.getTotalHPRef().removeReferenceListener(hpListener);
        }
    });
    Utility.installEscapeCloseOperation(this);
}
Also used : JScrollPane(javax.swing.JScrollPane) IntegerEditor(pcgen.gui2.util.table.IntegerEditor) JButton(javax.swing.JButton) JTableHeader(javax.swing.table.JTableHeader) JLabel(javax.swing.JLabel) WindowAdapter(java.awt.event.WindowAdapter) Box(javax.swing.Box) ReferenceListener(pcgen.facade.util.event.ReferenceListener) Container(java.awt.Container) BorderLayout(java.awt.BorderLayout) ReferenceEvent(pcgen.facade.util.event.ReferenceEvent) JTable(javax.swing.JTable) WindowEvent(java.awt.event.WindowEvent) TableCellRenderer(javax.swing.table.TableCellRenderer) TableCellEditor(javax.swing.table.TableCellEditor) AbstractCellEditor(javax.swing.AbstractCellEditor) IntegerEditor(pcgen.gui2.util.table.IntegerEditor)

Example 3 with JTableHeader

use of javax.swing.table.JTableHeader in project pcgen by PCGen.

the class PostLevelUpDialog method initComponents.

private void initComponents() {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    JTable table = new JTable(tableModel) {

        @Override
        public TableCellEditor getCellEditor(int row, int column) {
            if (column == LevelTableModel.COL_ROLLED_HP && row < numLevels) {
                //TODO: the max roll should be calculated in a different manner
                String hd = levels.getClassTaken(levels.getElementAt(row + oldLevel)).getHD();
                int max = NumberUtils.toInt(hd);
                return new SpinnerEditor(new SpinnerNumberModel(1, 1, max, 1));
            }
            return super.getCellEditor(row, column);
        }

        @Override
        public TableCellRenderer getCellRenderer(int row, int column) {
            if (column == LevelTableModel.COL_ROLLED_HP && row < numLevels) {
                return new SpinnerRenderer();
            }
            return super.getCellRenderer(row, column);
        }
    };
    table.setCellSelectionEnabled(false);
    table.setRowHeight(new JSpinner().getPreferredSize().height);
    JTableHeader header = table.getTableHeader();
    header.setReorderingAllowed(false);
    JScrollPane scrollPane = new JScrollPane(table);
    pane.add(scrollPane, BorderLayout.CENTER);
    Box box = Box.createHorizontalBox();
    box.add(Box.createHorizontalGlue());
    //$NON-NLS-1$
    JButton button = new JButton(LanguageBundle.getString("in_close"));
    //$NON-NLS-1$
    button.setMnemonic(LanguageBundle.getMnemonic("in_mn_close"));
    //$NON-NLS-1$
    button.setActionCommand("Close");
    button.addActionListener(this);
    box.add(button);
    pane.add(box, BorderLayout.SOUTH);
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosed(WindowEvent e) {
            //Make sure to remove the listeners so that the garbage collector can
            //dispose of this dialog and prevent a memory leak
            levels.removeHitPointListener(tableModel);
        }
    });
    Utility.installEscapeCloseOperation(this);
}
Also used : JScrollPane(javax.swing.JScrollPane) SpinnerEditor(pcgen.gui2.util.table.TableCellUtilities.SpinnerEditor) JButton(javax.swing.JButton) JTableHeader(javax.swing.table.JTableHeader) WindowAdapter(java.awt.event.WindowAdapter) Box(javax.swing.Box) SpinnerRenderer(pcgen.gui2.util.table.TableCellUtilities.SpinnerRenderer) SpinnerNumberModel(javax.swing.SpinnerNumberModel) Container(java.awt.Container) BorderLayout(java.awt.BorderLayout) JTable(javax.swing.JTable) WindowEvent(java.awt.event.WindowEvent) JSpinner(javax.swing.JSpinner)

Example 4 with JTableHeader

use of javax.swing.table.JTableHeader in project EnrichmentMapApp by BaderLab.

the class ColumnHeaderRankOptionRenderer method menuButtonClicked.

private void menuButtonClicked(JTable table, JButton button) {
    JTableHeader header = table.getTableHeader();
    List<RankingOption> rankOptions = mainPanel.getAllRankingOptions();
    JPopupMenu menu = new JPopupMenu();
    for (RankingOption rankOption : rankOptions) {
        JMenuItem item = new JCheckBoxMenuItem(rankOption.getName());
        item.setSelected(rankOption == mainPanel.getSelectedRankOption());
        SwingUtil.makeSmall(item);
        menu.add(item);
        item.addActionListener(e -> mainPanel.updateSetting_RankOption(rankOption));
    }
    int y = button.getHeight();
    int x = 0;
    for (int i = 0; i < colIndex; i++) {
        TableColumn tableColumn = table.getColumnModel().getColumn(i);
        x += tableColumn.getWidth();
    }
    menu.show(header, x, y);
}
Also used : JTableHeader(javax.swing.table.JTableHeader) RankingOption(org.baderlab.csplugins.enrichmentmap.view.heatmap.RankingOption) JMenuItem(javax.swing.JMenuItem) TableColumn(javax.swing.table.TableColumn) JPopupMenu(javax.swing.JPopupMenu) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem)

Example 5 with JTableHeader

use of javax.swing.table.JTableHeader in project EnrichmentMapApp by BaderLab.

the class ColumnHeaderRankOptionRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(JTable table, final Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    // Convert RankingOption to display String
    String text = null;
    if (value instanceof RankingOption) {
        RankingOption rankingOption = (RankingOption) value;
        text = rankingOption.getTableHeaderText();
    } else if (value instanceof RankOptionErrorHeader) {
        RankOptionErrorHeader headerValue = (RankOptionErrorHeader) value;
        text = headerValue.getRankingOption().getTableHeaderText();
    }
    // Use the default renderer to paint the header nicely (with sort arrows)
    JTableHeader header = table.getTableHeader();
    TableCellRenderer delegate = table.getTableHeader().getDefaultRenderer();
    Component component = delegate.getTableCellRendererComponent(table, text, isSelected, hasFocus, row, col);
    // Create the panel
    JButton button = new JButton("Sort");
    SwingUtil.makeSmall(button);
    if (isAquaLAF())
        button.putClientProperty("JButton.buttonType", "gradient");
    button.addActionListener(e -> menuButtonClicked(table, button));
    JPanel buttonPanel = new JPanel(new BorderLayout());
    buttonPanel.add(button, BorderLayout.CENTER);
    buttonPanel.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    buttonPanel.setForeground(header.getForeground());
    buttonPanel.setBackground(header.getBackground());
    JPanel panel = new JPanel(new BorderLayout());
    panel.setForeground(header.getForeground());
    panel.setBackground(header.getBackground());
    panel.add(component, BorderLayout.CENTER);
    panel.add(buttonPanel, BorderLayout.NORTH);
    if (value instanceof RankOptionErrorHeader) {
        JLabel icon = new JLabel(IconManager.ICON_TIMES_CIRCLE);
        icon.setForeground(Color.RED.darker());
        icon.setFont(iconManager.getIconFont(13.0f));
        icon.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        icon.setOpaque(false);
        panel.add(icon, BorderLayout.WEST);
    }
    // Add mouse listener
    if (mouseListener != null) {
        header.removeMouseListener(mouseListener);
    }
    header.addMouseListener(mouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            int col = header.columnAtPoint(e.getPoint());
            if (col == colIndex)
                if (e.getY() <= button.getHeight())
                    button.doClick();
                else
                    sortColumn(table);
            else
                sortOrder = null;
        }
    });
    return panel;
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) JPanel(javax.swing.JPanel) MouseEvent(java.awt.event.MouseEvent) JButton(javax.swing.JButton) MouseAdapter(java.awt.event.MouseAdapter) RankingOption(org.baderlab.csplugins.enrichmentmap.view.heatmap.RankingOption) JTableHeader(javax.swing.table.JTableHeader) JLabel(javax.swing.JLabel) BorderLayout(java.awt.BorderLayout) Component(java.awt.Component)

Aggregations

JTableHeader (javax.swing.table.JTableHeader)123 JTable (javax.swing.JTable)31 TableColumn (javax.swing.table.TableColumn)27 TableCellRenderer (javax.swing.table.TableCellRenderer)25 TableColumnModel (javax.swing.table.TableColumnModel)24 Component (java.awt.Component)21 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)19 JScrollPane (javax.swing.JScrollPane)17 MouseEvent (java.awt.event.MouseEvent)15 JLabel (javax.swing.JLabel)15 Color (java.awt.Color)11 MouseAdapter (java.awt.event.MouseAdapter)11 JPanel (javax.swing.JPanel)11 DefaultTableModel (javax.swing.table.DefaultTableModel)10 BorderLayout (java.awt.BorderLayout)9 Dimension (java.awt.Dimension)9 Point (java.awt.Point)9 JButton (javax.swing.JButton)8 JComponent (javax.swing.JComponent)8 JPopupMenu (javax.swing.JPopupMenu)7