Example usage for javax.swing.table JTableHeader getColumnModel

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

Introduction

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

Prototype

public TableColumnModel getColumnModel() 

Source Link

Document

Returns the TableColumnModel that contains all column information of this table header.

Usage

From source file:finalproject.BloodGlucoseGUI.java

/**
 * Constructor//from  w w  w  . ja va2  s. c o m
 * Creates new form BloodGlucoseGUI
 */
// I would put most of it in initComponents if I could 
// edit that generated code.
public BloodGlucoseGUI() {
    initComponents();
    //jfc = new JFileChooser("Resources/Data");

    // For "commercial" use
    //jfc = new JFileChooser("C:\\Program Files\\BGDataAnalysis\\Data");
    jfc = new JFileChooser("C:\\BGDataAnalysis\\Data");

    tabMod = new MyTableModel();
    jTable2.setModel(tabMod);

    listMod = new DefaultListModel();
    jList1.setModel(listMod);

    JTableHeader th = jTable2.getTableHeader();
    TableColumnModel tcm = th.getColumnModel();
    TableColumn tc = tcm.getColumn(0);
    tc.setHeaderValue("Time");
    tc = tcm.getColumn(1);
    tc.setHeaderValue("Blood Glucose");
    th.repaint();

    notesTextArea.setLineWrap(true);
    notesTextArea.setWrapStyleWord(true);

    notesTextArea.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                e.consume();

                ErrorGUIs.Popup pu = new ErrorGUIs.Popup();
                pu.add("Please don't press the ENTER button!");
                pu.display();
            }
        }
    });
}

From source file:org.ut.biolab.medsavant.client.view.genetics.variantinfo.VariantFrequencyAggregatePane.java

public void setVariantRecords(Set<VariantRecord> records) {
    innerPanel.removeAll();/* w  w w .  j av  a2  s  .  c om*/
    //This function executes quickly, no need for waitPanel.
    //innerPanel.add(new WaitPanel("Loading DNA Ids..."));
    tableModel.setValues(records);
    aggregateTable = new AggregateTable(tableModel) {
        @Override
        //Necessary to enable buttons within the table.
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return colIndex == buttonIndex;
        }

        //"Disables" selection within the aggregateTable.
        @Override
        public TableCellRenderer getCellRenderer(final int rowIndex, final int columnIndex) {
            final TableCellRenderer renderer = super.getCellRenderer(rowIndex, columnIndex);
            return new TableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable jtable, final Object o, boolean bln,
                        boolean bln1, int i, int i1) {
                    return renderer.getTableCellRendererComponent(jtable, o, false, false, i, i1);
                }
            };
        }

        //This overridden method, together with the custom mouse listener on the 
        //AggregateTable header, disallows moving the first column, and columns
        //>= dnaIDIndex                    
        @Override
        protected AggregateTable.DraggingHandler createDraggingColumnPropertyChangeListener() {
            return new AggregateTable.DraggingHandler() {
                @Override
                public void columnMoved(TableColumnModelEvent e) {
                    if (fromColumnIndex == -1) {
                        fromColumnIndex = e.getFromIndex();
                    }
                    toColumnIndex = e.getToIndex();
                }

            };
        }
    };

    header = new AggregateTableHeader(aggregateTable);
    header.addMouseListener(new MouseAdapter() {
        //Disable moving the first column, or columns with index
        //>=dnaIDIndex
        @Override
        public void mouseReleased(MouseEvent e) {
            if (toColumnIndex != -1 && ((toColumnIndex == 0 || fromColumnIndex == 0)
                    || (toColumnIndex >= dnaIDIndex || fromColumnIndex >= dnaIDIndex))) {
                aggregateTable.moveColumn(toColumnIndex, fromColumnIndex);
                String msg = "This column cannot be moved.";
                DialogUtils.displayMessage(msg);
            } else {
                for (int columnHeaderIndex = 0; columnHeaderIndex < header.getColumnModel().getColumnCount()
                        - 2; columnHeaderIndex++) {
                    String columnTitle = (String) header.getColumnModel().getColumn(columnHeaderIndex)
                            .getHeaderValue();
                    aggregateColumns[columnHeaderIndex] = columnTitle;
                }
                reaggregate();
            }
            //aggregateTable.aggregate(this.aggregateColumns);
            fromColumnIndex = -1;
            toColumnIndex = -1;
            //expandAllButLast();
        }
    });

    header.setAutoFilterEnabled(false);
    header.setReorderingAllowed(true);
    header.setFont(TABLE_FONT_LARGE);
    aggregateTable.setTableHeader(header);
    aggregateTable.setAutoResizeMode(AggregateTable.AUTO_RESIZE_ALL_COLUMNS);
    aggregateTable.setFont(TABLE_FONT_LARGE);

    //Setup a custom "summary".  This is what calculates frequencies when cells are
    //collapsed
    PivotField f = aggregateTable.getAggregateTableModel().getField(BasicVariantColumns.DNA_ID.getAlias());
    f.setSummaryType(PivotConstants.SUMMARY_RESERVED_MAX + 1);
    aggregateTable.getAggregateTableModel().setSummaryCalculator(new SummaryCalculator() {
        private Set<String> collapsedDNAIDs = new HashSet<String>();
        private Values lastRowValues;
        private int valueCount = 0;
        private final int SUMMARY_FREQ = PivotConstants.SUMMARY_RESERVED_MAX + 1;

        @Override
        public void addValue(PivotValueProvider dataModel, PivotField field, Values rowValues,
                Values columnValues, Object object) {
            //this gets called multiple times for all the cells that disappear when 
            //something is collapsed.  
            // row[0] is the value of the corresponding first column: e.g. a Cohort or Family. 
            // columnValues can be ignored (blank)
            // Object is the value of the item in the cell that disappeared. (i.e. those cells which are being summarized)
            // field.getName() is the column name corresponding to Object

            // Useful Debugging code:                      
            //if(field.getName().equals(BasicVariantColumns.DNA_ID.getAlias())){

            /*
             System.out.println("==========");
             System.out.println("Field : " + field.getName());
             System.out.println("Row values: ");
                    
             for (int i = 0; i < rowValues.getCount(); ++i) {
             System.out.println("\trow[" + i + "] = " + rowValues.getValueAt(i).getValue());
                    
             }
                    
             System.out.println("Column values: ");
             for (int i = 0; i < columnValues.getCount(); ++i) {
             System.out.println("\tcol[" + i + "] = " + columnValues.getValueAt(i).getValue());
             }
                    
             System.out.println("Object: ");
             System.out.println("\t" + object);
             System.out.println("==========");
             */

            // }
            if (field.getName().equals(BasicVariantColumns.DNA_ID.getAlias())) {
                collapsedDNAIDs.add((String) object);
                lastRowValues = rowValues;
            } else {
                lastRowValues = null;
            }
            valueCount++;
        }

        //Should never be called
        @Override
        public void addValue(Object o) {
            LOG.error("Unexpected method invocation in OtherIndividualsSubInspector (1)");
        }

        //Should never be called
        @Override
        public void addValue(IPivotDataModel ipdm, PivotField pf, int i, int i1, Object o) {
            LOG.error("Unexpected method invocation in OtherIndividualsSubInspector (2)");
        }

        //Should never be called
        @Override
        public void addValue(PivotValueProvider pvp, PivotField pf, Object o) {
            LOG.error("Unexpected method invocation in OtherIndividualsSubInspector (3)");
        }

        @Override
        public void clear() {
            collapsedDNAIDs.clear();
            valueCount = 0;
            lastRowValues = null;
        }

        @Override
        public Object getSummaryResult(int type) {
            //if null, then we're not in the DNAId column.  Return null
            //to show a blank in this cell
            if (lastRowValues == null) {
                return null;
            }

            int numIndividuals = getNumberOfIndividualsInGroup(lastRowValues.getValueAt(0));

            return new Frequency(collapsedDNAIDs.size(), numIndividuals);
        }

        private int getNumberOfIndividualsInGroup(Value v) {
            if (aggregateColumns[0].equals("Cohort")) {
                //LOG.debug("Getting number of individuals in group " + v.getValue());
                Set<String> dnaIds = cohortDNAIDMap.get((Cohort) v.getValue());
                //for (String id : dnaIds) {
                //LOG.debug("\tGot id " + id);
                //}
                return cohortDNAIDMap.get((Cohort) v.getValue()).size();
            } else if (aggregateColumns[0].equals(BasicPatientColumns.FAMILY_ID.getAlias())) {
                return familyIDdnaIDMap.get((String) v.getValue()).size();
            } else {
                LOG.error("Invalid first column");
                return -1;
            }
        }

        @Override
        public long getCount() {
            return valueCount;
        }

        @Override
        public int getNumberOfSummaries() {
            return 1;
        }

        @Override
        public String getSummaryName(Locale locale, int i) {
            return "Frequency";
        }

        @Override
        public int[] getAllowedSummaries(Class<?> type) {
            return new int[] { SUMMARY_FREQ };
        }

        @Override
        public int[] getAllowedSummaries(Class<?> type, ConverterContext cc) {
            return new int[] { SUMMARY_FREQ };
        }
    });

    //Sets up the context menu for clicking column headers.  This will probably not be used
    //frequently.  Limit the available operations to collapsing, expanding, and grouping.
    TableHeaderPopupMenuInstaller installer = new TableHeaderPopupMenuInstaller(aggregateTable);
    installer.addTableHeaderPopupMenuCustomizer(new AggregateTablePopupMenuCustomizer() {
        @Override
        public void customizePopupMenu(JTableHeader header, JPopupMenu popup, int clickingColumn) {
            super.customizePopupMenu(header, popup, clickingColumn);
            for (int i = 0; i < popup.getComponentCount(); i++) {
                String menuItemName = popup.getComponent(i).getName();
                if (!(CONTEXT_MENU_COLLAPSE.equals(menuItemName)
                        || CONTEXT_MENU_COLLAPSE_ALL.equals(menuItemName)
                        || CONTEXT_MENU_EXPAND.equals(menuItemName)
                        || CONTEXT_MENU_EXPAND_ALL.equals(menuItemName)
                        || CONTEXT_MENU_GROUP.equals(menuItemName)
                        || CONTEXT_MENU_UNGROUP.equals(menuItemName))) {

                    popup.remove(popup.getComponent(i));

                }
            }
        }
    });

    aggregateTable.getAggregateTableModel().setSummaryMode(true);

    aggregateTable.aggregate(aggregateColumns);
    aggregateTable.setShowContextMenu(false);

    expandAllButLast();
    setupButtonColumn();

    JScrollPane jsp = new JScrollPane(aggregateTable);
    jsp.setPreferredSize(PREFERRED_SIZE);

    JPanel bp = new JPanel();

    bp.setLayout(new BoxLayout(bp, BoxLayout.X_AXIS));
    JButton closeButton = new JButton(IconFactory.getInstance().getIcon(IconFactory.StandardIcon.CLOSE));
    closeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            unsplitScreen();
        }
    });
    bp.add(Box.createHorizontalGlue());
    bp.add(title);
    bp.add(Box.createHorizontalGlue());
    bp.add(closeButton);
    innerPanel.add(bp);
    innerPanel.add(jsp);
    reaggregate();
    innerPanel.revalidate();
    innerPanel.repaint();
}

From source file:org.yccheok.jstock.gui.PortfolioManagementJPanel.java

/** This method is called from within the constructor to
 * initialize the form.//from   ww  w .  jav a 2  s. c  o  m
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jPanel4 = new javax.swing.JPanel();
    jPanel3 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jPanel5 = new javax.swing.JPanel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jSplitPane1 = new javax.swing.JSplitPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    buyTreeTable = new SortableTreeTable(new BuyPortfolioTreeTableModelEx());
    // We need to have a hack way, to have "Comment" in the model, but not visible to user.
    // So that our ToolTipHighlighter can work correctly.
    // setVisible should be called after JXTreeTable has been constructed. This is to avoid
    // initGUIOptions from calling JTable.removeColumn
    // ToolTipHighlighter will not work correctly if we tend to hide column view by removeColumn.
    // We need to hide the view by using TableColumnExt.setVisible.
    // Why? Don't ask me. Ask SwingX team.
    ((TableColumnExt) buyTreeTable.getColumn(GUIBundle.getString("PortfolioManagementJPanel_Comment")))
            .setVisible(false);
    jScrollPane2 = new javax.swing.JScrollPane();
    sellTreeTable = new SortableTreeTable(new SellPortfolioTreeTableModelEx());

    // We need to have a hack way, to have "Comment" in the model, but not visible to user.
    // So that our ToolTipHighlighter can work correctly.
    // setVisible should be called after JXTreeTable has been constructed. This is to avoid
    // initGUIOptions from calling JTable.removeColumn
    // ToolTipHighlighter will not work correctly if we tend to hide column view by removeColumn.
    // We need to hide the view by using TableColumnExt.setVisible.
    // Why? Don't ask me. Ask SwingX team.
    ((TableColumnExt) sellTreeTable.getColumn(GUIBundle.getString("PortfolioManagementJPanel_Comment")))
            .setVisible(false);
    jPanel2 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();

    addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            formMouseClicked(evt);
        }
    });
    setLayout(new java.awt.BorderLayout());

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Portfolio Management"));
    jPanel1.setLayout(new java.awt.BorderLayout(0, 5));

    jPanel4.setLayout(new java.awt.BorderLayout());

    jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

    jLabel1.setText(getShareLabel());
    jPanel3.add(jLabel1);

    jLabel2.setFont(jLabel2.getFont().deriveFont(jLabel2.getFont().getStyle() | java.awt.Font.BOLD));
    jPanel3.add(jLabel2);

    jLabel3.setText(getCashLabel());
    jPanel3.add(jLabel3);

    jLabel4.setFont(jLabel4.getFont().deriveFont(jLabel4.getFont().getStyle() | java.awt.Font.BOLD));
    jPanel3.add(jLabel4);

    jPanel4.add(jPanel3, java.awt.BorderLayout.WEST);

    jLabel5.setText(getPaperProfitLabel());
    jPanel5.add(jLabel5);

    jLabel6.setFont(jLabel6.getFont().deriveFont(jLabel6.getFont().getStyle() | java.awt.Font.BOLD));
    jPanel5.add(jLabel6);

    jLabel7.setText(getRealizedProfitLabel());
    jPanel5.add(jLabel7);

    jLabel8.setFont(jLabel8.getFont().deriveFont(jLabel8.getFont().getStyle() | java.awt.Font.BOLD));
    jPanel5.add(jLabel8);

    jPanel4.add(jPanel5, java.awt.BorderLayout.EAST);

    jPanel1.add(jPanel4, java.awt.BorderLayout.NORTH);

    jSplitPane1.setDividerLocation(250);
    jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui"); // NOI18N
    jScrollPane1.setBorder(
            javax.swing.BorderFactory.createTitledBorder(bundle.getString("PortfolioManagementJPanel_Buy"))); // NOI18N

    buyTreeTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
    buyTreeTable.setRootVisible(true);
    // this must be before any sort instructions or get funny results
    buyTreeTable.setAutoCreateColumnsFromModel(false);

    buyTreeTable.addMouseListener(new BuyTableRowPopupListener());
    buyTreeTable.addKeyListener(new TableKeyEventListener());

    org.jdesktop.swingx.decorator.Highlighter highlighter0 = org.jdesktop.swingx.decorator.HighlighterFactory
            .createSimpleStriping(new Color(245, 245, 220));
    buyTreeTable.addHighlighter(highlighter0);
    buyTreeTable.addHighlighter(new ToolTipHighlighter());

    initTreeTableDefaultRenderer(buyTreeTable);

    // Not sure why. Without this code, sorting won't work just after you resize 
    // table header.
    JTableHeader oldBuyTableHeader = buyTreeTable.getTableHeader();
    JXTableHeader newBuyTableHeader = new JXTableHeader(oldBuyTableHeader.getColumnModel());
    buyTreeTable.setTableHeader(newBuyTableHeader);

    // We need to have a hack way, to have "Comment" in the model, but not visible to user.
    // So that our ToolTipHighlighter can work correctly.
    buyTreeTable.getTableHeader().addMouseListener(new TableColumnSelectionPopupListener(1,
            new String[] { GUIBundle.getString("PortfolioManagementJPanel_Comment") }));
    buyTreeTable.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
        public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
            buyTreeTableValueChanged(evt);
        }
    });
    jScrollPane1.setViewportView(buyTreeTable);

    jSplitPane1.setLeftComponent(jScrollPane1);

    jScrollPane2.setBorder(
            javax.swing.BorderFactory.createTitledBorder(bundle.getString("PortfolioManagementJPanel_Sell"))); // NOI18N

    sellTreeTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
    sellTreeTable.setRootVisible(true);
    // this must be before any sort instructions or get funny results
    sellTreeTable.setAutoCreateColumnsFromModel(false);

    sellTreeTable.addMouseListener(new SellTableRowPopupListener());
    sellTreeTable.addKeyListener(new TableKeyEventListener());

    org.jdesktop.swingx.decorator.Highlighter highlighter1 = org.jdesktop.swingx.decorator.HighlighterFactory
            .createSimpleStriping(new Color(245, 245, 220));
    sellTreeTable.addHighlighter(highlighter1);
    sellTreeTable.addHighlighter(new ToolTipHighlighter());

    initTreeTableDefaultRenderer(sellTreeTable);

    // Not sure why. Without this code, sorting won't work just after you resize 
    // table header.
    JTableHeader oldSellTableHeader = sellTreeTable.getTableHeader();
    JXTableHeader newSellTableHeader = new JXTableHeader(oldSellTableHeader.getColumnModel());
    sellTreeTable.setTableHeader(newSellTableHeader);

    // We need to have a hack way, to have "Comment" in the model, but not visible to user.
    // So that our ToolTipHighlighter can work correctly.
    sellTreeTable.getTableHeader().addMouseListener(new TableColumnSelectionPopupListener(1,
            new String[] { GUIBundle.getString("PortfolioManagementJPanel_Comment") }));
    sellTreeTable.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
        public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
            sellTreeTableValueChanged(evt);
        }
    });
    jScrollPane2.setViewportView(sellTreeTable);

    jSplitPane1.setRightComponent(jScrollPane2);

    jPanel1.add(jSplitPane1, java.awt.BorderLayout.CENTER);

    add(jPanel1, java.awt.BorderLayout.CENTER);

    jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/16x16/inbox.png"))); // NOI18N
    jButton1.setText(bundle.getString("PortfolioManagementJPanel_Buy...")); // NOI18N
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });
    jPanel2.add(jButton1);

    jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/16x16/outbox.png"))); // NOI18N
    jButton3.setText(bundle.getString("PortfolioManagementJPanel_Sell...")); // NOI18N
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });
    jPanel2.add(jButton3);

    jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/16x16/money.png"))); // NOI18N
    jButton4.setText(bundle.getString("PortfolioManagementJPanel_Cash...")); // NOI18N
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });
    jPanel2.add(jButton4);

    jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/16x16/money2.png"))); // NOI18N
    jButton5.setText(bundle.getString("PortfolioManagementJPanel_Dividen...")); // NOI18N
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });
    jPanel2.add(jButton5);

    add(jPanel2, java.awt.BorderLayout.SOUTH);
}

From source file:richtercloud.reflection.form.builder.panels.AbstractListPanel.java

/**
 * Subclasses are strongly recommended to call {@link #reset() } to
 * initialize the values.//from  ww  w . jav a 2 s.c o  m
 *
 * @param reflectionFormBuilder
 * @param mainListCellEditor
 * @param mainListCellRenderer
 * @param mainListModel
 * @param initialValues
 * @param messageHandler
 * @param tableHeader a table header object which allows control over the
 * height of the table column header
 */
/*
internal implementation notes:
- reset isn't called here because it uses an overridable call to
TableModel.setValueAt which causes trouble in subclasses
*/
public AbstractListPanel(R reflectionFormBuilder, ListPanelTableCellEditor mainListCellEditor,
        ListPanelTableCellRenderer mainListCellRenderer, M mainListModel, List<T> initialValues,
        MessageHandler messageHandler, JTableHeader tableHeader) {
    //not possible to call this() because of dependency on cell renderer and
    //editor
    this.reflectionFormBuilder = reflectionFormBuilder;
    //don't add an item initially because that is most certainly
    //unintentional
    this.mainListCellEditor = mainListCellEditor;
    this.mainListCellRenderer = mainListCellRenderer;
    this.mainListModel = mainListModel;
    initComponents();
    this.mainList.setTableHeader(tableHeader);
    this.mainList.setColumnModel(tableHeader.getColumnModel());
    this.mainList.setDefaultRenderer(Object.class, mainListCellRenderer);
    this.mainList.setDefaultEditor(Object.class, mainListCellEditor);
    this.messageHandler = messageHandler;
    this.initialValues = initialValues;
}