Search in sources :

Example 26 with JTableHeader

use of javax.swing.table.JTableHeader in project cytoscape-impl by cytoscape.

the class BrowserTable method init.

// ==[ PRIVATE METHODS ]============================================================================================
protected void init() {
    final JTableHeader header = getTableHeader();
    header.setOpaque(false);
    header.setDefaultRenderer(new BrowserTableHeaderRenderer(serviceRegistrar.getService(IconManager.class)));
    header.getColumnModel().setColumnSelectionAllowed(true);
    header.addMouseMotionListener(this);
    header.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(final MouseEvent e) {
            maybeShowHeaderPopup(e);
        }

        @Override
        public void mouseReleased(final MouseEvent e) {
            maybeShowHeaderPopup(e);
        }
    });
    setSelectionModel(new BrowserTableListSelectionModel());
    setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    addMouseListener(this);
    getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(final ListSelectionEvent e) {
            if (!e.getValueIsAdjusting() && !ignoreRowSelectionEvents)
                selectFromTable();
        }
    });
}
Also used : MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) ListSelectionEvent(javax.swing.event.ListSelectionEvent) JTableHeader(javax.swing.table.JTableHeader) ListSelectionListener(javax.swing.event.ListSelectionListener)

Example 27 with JTableHeader

use of javax.swing.table.JTableHeader in project cytoscape-impl by cytoscape.

the class PreviewTablePanel method positionEditDialog.

private void positionEditDialog() {
    if (editDialog != null) {
        final JTableHeader hd = getPreviewTable().getTableHeader();
        // Get the column header location
        // (see: https://bugs.openjdk.java.net/browse/JDK-4408424)
        final AccessibleComponent ac = hd.getAccessibleContext().getAccessibleChild(editDialog.index).getAccessibleContext().getAccessibleComponent();
        final Point screenPt = ac.getLocationOnScreen();
        final Point compPt = ac.getLocation();
        int xOffset = screenPt.x - compPt.x;
        int yOffset = screenPt.y - compPt.y + hd.getBounds().height;
        final Point pt = ac.getBounds().getLocation();
        pt.translate(xOffset, yOffset);
        // This prevent the dialog from being positioned completely outside the parent panel
        pt.x = Math.max(pt.x, getTableScrollPane().getLocationOnScreen().x - editDialog.getBounds().width);
        pt.x = Math.min(pt.x, getTableScrollPane().getLocationOnScreen().x + getTableScrollPane().getBounds().width);
        // Show the dialog right below the column header
        editDialog.setLocation(pt);
    }
}
Also used : AccessibleComponent(javax.accessibility.AccessibleComponent) JTableHeader(javax.swing.table.JTableHeader) Point(java.awt.Point) Point(java.awt.Point)

Example 28 with JTableHeader

use of javax.swing.table.JTableHeader in project tetrad by cmu-phil.

the class IndependenceFactsAction method actionPerformed.

// ========================PUBLIC METHODS==========================//
/**
 * Performs the action of opening a session from a file.
 */
public void actionPerformed(ActionEvent e) {
    this.independenceTest = getIndTestProducer().getIndependenceTest();
    final List<String> varNames = new ArrayList<>();
    varNames.add("VAR");
    varNames.addAll(getDataVars());
    varNames.add("?");
    varNames.add("+");
    final JComboBox variableBox = new JComboBox();
    DefaultComboBoxModel aModel1 = new DefaultComboBoxModel(varNames.toArray(new String[varNames.size()]));
    aModel1.setSelectedItem("VAR");
    variableBox.setModel(aModel1);
    // variableBox.addMouseListener(new MouseAdapter() {
    // public void mouseClicked(MouseEvent e) {
    // System.out.println(e);
    // }
    // });
    variableBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JComboBox box = (JComboBox) e.getSource();
            String var = (String) box.getSelectedItem();
            LinkedList<String> vars = getVars();
            int size = vars.size();
            if ("VAR".equals(var)) {
                return;
            }
            for (int i = 2; i < getVars().size() - 1; i++) {
                if (wildcard(i)) {
                    if (!("?".equals(var) || "+".equals(var))) {
                        JOptionPane.showMessageDialog(centeringComp, "Please specify wildcards after other variables (e.g. X _||_ ? | Y, +)");
                        return;
                    }
                }
            }
            if ("?".equals(var)) {
                if (size >= 0 && !vars.contains("+")) {
                    vars.addLast(var);
                }
            } else if ("+".equals(var)) {
                if (size >= 2) {
                    vars.addLast(var);
                }
            } else if ((vars.indexOf("?") < 2) && !(vars.contains("+")) && !(vars.contains(var))) {
                vars.add(var);
            }
            if (wildcard(0) && vars.size() >= 2 && !wildcard(1)) {
                JOptionPane.showMessageDialog(centeringComp, "Please specify wildcards after other variables (e.g. X _||_ ? | Y, +)");
                return;
            }
            resetText();
            // This is a workaround to an introduced bug in the JDK whereby
            // repeated selections of the same item send out just one
            // action event.
            DefaultComboBoxModel aModel = new DefaultComboBoxModel(varNames.toArray(new String[varNames.size()]));
            aModel.setSelectedItem("VAR");
            variableBox.setModel(aModel);
        }
    });
    final JButton delete = new JButton("Delete");
    delete.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (!getVars().isEmpty()) {
                getVars().removeLast();
                resetText();
            }
        }
    });
    textField.addKeyListener(new KeyAdapter() {

        public void keyTyped(KeyEvent e) {
            if ('?' == e.getKeyChar()) {
                variableBox.setSelectedItem("?");
            } else if ('+' == e.getKeyChar()) {
                variableBox.setSelectedItem("+");
            } else if ('\b' == e.getKeyChar()) {
                vars.removeLast();
                resetText();
            }
            e.consume();
        }
    });
    delete.addKeyListener(new KeyAdapter() {

        public void keyTyped(KeyEvent e) {
            if ('?' == e.getKeyChar()) {
                variableBox.setSelectedItem("?");
            } else if ('+' == e.getKeyChar()) {
                variableBox.setSelectedItem("+");
            } else if ('\b' == e.getKeyChar()) {
                vars.removeLast();
                resetText();
            }
        }
    });
    variableBox.addKeyListener(new KeyAdapter() {

        public void keyTyped(KeyEvent e) {
            super.keyTyped(e);
            if ('\b' == e.getKeyChar()) {
                vars.removeLast();
                resetText();
            }
        }
    });
    JButton list = new JButton("LIST");
    list.setFont(new Font("Dialog", Font.BOLD, 14));
    list.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            generateResults();
        }
    });
    Box b1 = Box.createVerticalBox();
    Box b2 = Box.createHorizontalBox();
    b2.add(new JLabel("Test: "));
    b2.add(new JLabel(getIndependenceTest().toString()));
    b2.add(Box.createHorizontalGlue());
    b1.add(b2);
    b1.add(Box.createVerticalStrut(10));
    Box b3 = Box.createHorizontalBox();
    b3.add(getTextField());
    b3.add(variableBox);
    b3.add(delete);
    b1.add(b3);
    b1.add(Box.createVerticalStrut(10));
    tableModel = new AbstractTableModel() {

        public String getColumnName(int column) {
            if (column == 0) {
                return "Index";
            }
            if (column == 1) {
                if (independenceTest instanceof IndTestDSep) {
                    return "D-Separation Relation";
                } else {
                    return "Independence Relation";
                }
            } else if (column == 2) {
                return "Judgment";
            } else if (column == 3) {
                return "P Value";
            }
            return null;
        }

        public int getColumnCount() {
            if (usesDSeparation()) {
                return 3;
            } else {
                return 4;
            }
        }

        public int getRowCount() {
            return getResults().size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Result result = getResults().get(rowIndex);
            if (columnIndex == 0) {
                return result.getIndex() + 1;
            }
            if (columnIndex == 1) {
                return result.getFact();
            } else if (columnIndex == 2) {
                if (independenceTest instanceof IndTestDSep) {
                    if (result.getType() == Result.Type.INDEPENDENT) {
                        return "D-Separated";
                    } else if (result.getType() == Result.Type.DEPENDENT) {
                        return "D-Connected";
                    } else if (result.getType() == Result.Type.UNDETERMINED) {
                        return "*";
                    }
                // return result.getType() ? "D-Separated" : "D-Connected";
                } else {
                    if (result.getType() == Result.Type.INDEPENDENT) {
                        return "Independent";
                    } else if (result.getType() == Result.Type.DEPENDENT) {
                        return "Dependent";
                    } else if (result.getType() == Result.Type.UNDETERMINED) {
                        return "*";
                    }
                // return result.getType() ? "Independent" : "Dependent";
                }
            } else if (columnIndex == 3) {
                return nf.format(result.getpValue());
            }
            return null;
        }

        public Class getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Number.class;
            }
            if (columnIndex == 1) {
                return String.class;
            } else if (columnIndex == 2) {
                return Number.class;
            } else if (columnIndex == 3) {
                return Number.class;
            }
            return null;
        }
    };
    JTable table = new JTable(tableModel);
    table.getColumnModel().getColumn(0).setMinWidth(40);
    table.getColumnModel().getColumn(0).setMaxWidth(40);
    table.getColumnModel().getColumn(1).setMinWidth(200);
    table.getColumnModel().getColumn(2).setMinWidth(100);
    table.getColumnModel().getColumn(2).setMaxWidth(100);
    if (!(usesDSeparation())) {
        table.getColumnModel().getColumn(3).setMinWidth(80);
        table.getColumnModel().getColumn(3).setMaxWidth(80);
    }
    JTableHeader header = table.getTableHeader();
    header.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            JTableHeader header = (JTableHeader) e.getSource();
            Point point = e.getPoint();
            int col = header.columnAtPoint(point);
            int sortCol = header.getTable().convertColumnIndexToModel(col);
            sortByColumn(sortCol, true);
        }
    });
    JScrollPane scroll = new JScrollPane(table);
    scroll.setPreferredSize(new Dimension(400, 400));
    b1.add(scroll);
    Box b4 = Box.createHorizontalBox();
    b4.add(new JLabel("Limit list to "));
    IntTextField field = new IntTextField(getListLimit(), 7);
    field.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                setListLimit(value);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    b4.add(field);
    b4.add(new JLabel(" items."));
    b4.add(Box.createHorizontalGlue());
    b4.add(list);
    b1.add(b4);
    b1.add(Box.createVerticalStrut(10));
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(b1, BorderLayout.CENTER);
    panel.setBorder(new EmptyBorder(10, 10, 10, 10));
    EditorWindow editorWindow = new EditorWindow(panel, "Independence Facts", "Save", false, centeringComp);
    DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
    editorWindow.setVisible(true);
    // Set the ok button so that pressing enter activates it.
    // jdramsey 5/5/02
    JRootPane root = SwingUtilities.getRootPane(editorWindow);
    if (root != null) {
        root.setDefaultButton(list);
    }
}
Also used : JTableHeader(javax.swing.table.JTableHeader) IndTestDSep(edu.cmu.tetrad.search.IndTestDSep) IntTextField(edu.cmu.tetradapp.util.IntTextField) EmptyBorder(javax.swing.border.EmptyBorder) AbstractTableModel(javax.swing.table.AbstractTableModel)

Example 29 with JTableHeader

use of javax.swing.table.JTableHeader in project beast2 by CompEvol.

the class TaxonSetInputEditor method getContent.

private Component getContent(List<Taxon> taxonset) {
    m_taxonset = taxonset;
    m_taxonMap = new HashMap<>();
    m_lineageset = new ArrayList<>();
    for (Taxon taxonset2 : m_taxonset) {
        if (taxonset2 instanceof TaxonSet) {
            for (Taxon taxon : ((TaxonSet) taxonset2).taxonsetInput.get()) {
                m_lineageset.add(taxon);
                m_taxonMap.put(taxon.getID(), taxonset2.getID());
            }
        }
    }
    // set up table.
    // special features: background shading of rows
    // custom editor allowing only Date column to be edited.
    m_model = new DefaultTableModel();
    m_model.addColumn("Taxon");
    m_model.addColumn("Species/Population");
    taxonSetToModel();
    m_table = new JTable(m_model) {

        private static final long serialVersionUID = 1L;

        // method that induces table row shading
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int Index_row, int Index_col) {
            Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
            // even index, selected or not selected
            if (isCellSelected(Index_row, Index_col)) {
                comp.setBackground(Color.gray);
            } else if (Index_row % 2 == 0) {
                comp.setBackground(new Color(237, 243, 255));
            } else {
                comp.setBackground(Color.white);
            }
            return comp;
        }
    };
    // set up editor that makes sure only doubles are accepted as entry
    // and only the Date column is editable.
    m_table.setDefaultEditor(Object.class, new TableCellEditor() {

        JTextField m_textField = new JTextField();

        int m_iRow, m_iCol;

        @Override
        public boolean stopCellEditing() {
            m_table.removeEditor();
            String text = m_textField.getText();
            // Log.warning.println(text);
            m_model.setValueAt(text, m_iRow, m_iCol);
            // try {
            // Double.parseDouble(text);
            // } catch (Exception e) {
            // return false;
            // }
            modelToTaxonset();
            return true;
        }

        @Override
        public boolean isCellEditable(EventObject anEvent) {
            return m_table.getSelectedColumn() == 1;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowNr, int colNr) {
            if (!isSelected) {
                return null;
            }
            m_iRow = rowNr;
            m_iCol = colNr;
            m_textField.setText((String) value);
            return m_textField;
        }

        @Override
        public boolean shouldSelectCell(EventObject anEvent) {
            return false;
        }

        @Override
        public void removeCellEditorListener(CellEditorListener l) {
        }

        @Override
        public Object getCellEditorValue() {
            return null;
        }

        @Override
        public void cancelCellEditing() {
        }

        @Override
        public void addCellEditorListener(CellEditorListener l) {
        }
    });
    m_table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    m_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    int size = m_table.getFont().getSize();
    m_table.setRowHeight(20 * size / 13);
    m_table.getColumnModel().getColumn(0).setPreferredWidth(250 * size / 13);
    m_table.getColumnModel().getColumn(1).setPreferredWidth(250 * size / 13);
    JTableHeader header = m_table.getTableHeader();
    header.addMouseListener(new ColumnHeaderListener());
    JScrollPane pane = new JScrollPane(m_table);
    Box tableBox = Box.createHorizontalBox();
    tableBox.add(Box.createHorizontalGlue());
    tableBox.add(pane);
    tableBox.add(Box.createHorizontalGlue());
    Box box = Box.createVerticalBox();
    box.add(createFilterBox());
    box.add(tableBox);
    box.add(createButtonBox());
    return box;
}
Also used : JScrollPane(javax.swing.JScrollPane) TableCellRenderer(javax.swing.table.TableCellRenderer) Taxon(beast.evolution.alignment.Taxon) DefaultTableModel(javax.swing.table.DefaultTableModel) Color(java.awt.Color) JTableHeader(javax.swing.table.JTableHeader) CellEditorListener(javax.swing.event.CellEditorListener) Box(javax.swing.Box) TaxonSet(beast.evolution.alignment.TaxonSet) JTextField(javax.swing.JTextField) EventObject(java.util.EventObject) JTable(javax.swing.JTable) EventObject(java.util.EventObject) TableCellEditor(javax.swing.table.TableCellEditor) Component(java.awt.Component)

Example 30 with JTableHeader

use of javax.swing.table.JTableHeader in project jmeter-plugins by undera.

the class HeaderClickCheckAllListener method mouseClicked.

@Override
public void mouseClicked(MouseEvent evt) {
    JTable table = ((JTableHeader) evt.getSource()).getTable();
    TableColumnModel colModel = table.getColumnModel();
    // The index of the column whose header was clicked
    int vColIndex = colModel.getColumnIndexAtX(evt.getX());
    // Return if not clicked on any column header
    if (vColIndex == -1) {
        return;
    }
    // Determine if mouse was clicked between column heads
    Rectangle headerRect = table.getTableHeader().getHeaderRect(vColIndex);
    if (vColIndex == 0) {
        // Hard-coded constant
        headerRect.width -= 3;
    } else {
        // Hard-coded constant
        headerRect.grow(-3, 0);
    }
    if (!headerRect.contains(evt.getX(), evt.getY())) {
        // Mouse was clicked between column heads
        // vColIndex is the column head closest to the click
        // vLeftColIndex is the column head to the left of the click
        int vLeftColIndex = vColIndex;
        if (evt.getX() < headerRect.x) {
            vLeftColIndex--;
        }
    } else {
        // click was on column
        if (vColIndex == CHECK_COL_INDEX) {
            checked = !checked;
            for (int n = 0; n < table.getModel().getRowCount(); n++) {
                table.getModel().setValueAt(checked, n, CHECK_COL_INDEX);
            }
        }
    }
}
Also used : JTable(javax.swing.JTable) Rectangle(java.awt.Rectangle) JTableHeader(javax.swing.table.JTableHeader) TableColumnModel(javax.swing.table.TableColumnModel)

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