Example usage for javax.swing.table JTableHeader setBackground

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

Introduction

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

Prototype

@BeanProperty(preferred = true, visualUpdate = true, description = "The background color of the component.")
public void setBackground(Color bg) 

Source Link

Document

Sets the background color of this component.

Usage

From source file:Main.java

public static void main(String[] args) {
    Integer[][] data = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    String[] cols = { "A", "B", "C" };

    JTable table = new JTable(data, cols);

    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.black);
    header.setForeground(Color.yellow);

    JOptionPane.showMessageDialog(null, new JScrollPane(table));
}

From source file:com.compomics.cell_coord.gui.controller.summary.SummaryDataController.java

/**
 * Initialize main view.//w  w w  . j a v  a 2  s.c  om
 */
private void initSummaryDataPanel() {
    // create new object
    summaryDataPanel = new SummaryDataPanel();
    // format the tables
    JTableHeader samplesHeader = summaryDataPanel.getSamplesTable().getTableHeader();
    samplesHeader.setBackground(GuiUtils.getHeaderColor());
    samplesHeader.setFont(GuiUtils.getHeaderFont());
    samplesHeader.setReorderingAllowed(false);

    JTableHeader tracksHeader = summaryDataPanel.getTracksTable().getTableHeader();
    tracksHeader.setBackground(GuiUtils.getHeaderColor());
    tracksHeader.setFont(GuiUtils.getHeaderFont());
    tracksHeader.setReorderingAllowed(false);

    JTableHeader trackSpotsHeader = summaryDataPanel.getTrackSpotsTable().getTableHeader();
    trackSpotsHeader.setBackground(GuiUtils.getHeaderColor());
    trackSpotsHeader.setFont(GuiUtils.getHeaderFont());
    trackSpotsHeader.setReorderingAllowed(false);

    summaryDataPanel.getSamplesTable().setRowSelectionAllowed(true);
    summaryDataPanel.getSamplesTable().setColumnSelectionAllowed(false);
    summaryDataPanel.getSamplesTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    summaryDataPanel.getTracksTable().setRowSelectionAllowed(true);
    summaryDataPanel.getTracksTable().setColumnSelectionAllowed(false);
    summaryDataPanel.getTracksTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // if you click on a sample, the relative tracks are shown in another table
    summaryDataPanel.getSamplesTable().getSelectionModel()
            .addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        int selectedRow = summaryDataPanel.getSamplesTable().getSelectedRow();
                        if (selectedRow != -1) {
                            Sample selectedSample = loadTracksController.getSamples().get(selectedRow);
                            showTracksInTable(selectedSample);
                        }
                    }
                }
            });

    // if you click on a track, the relative spots are shown in another table
    summaryDataPanel.getTracksTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                Sample selectedSample = loadTracksController.getSamples()
                        .get(summaryDataPanel.getSamplesTable().getSelectedRow());
                int selectedRow = summaryDataPanel.getTracksTable().getSelectedRow();
                if (selectedRow != -1) {
                    Track selectedTrack = selectedSample.getTracks().get(selectedRow);
                    showSpotsInTable(selectedTrack);
                }
            }
        }
    });

    // add view to parent controller
    loadTracksController.getMainFrame().getSummaryDataParentPanel().add(summaryDataPanel, gridBagConstraints);
}

From source file:com.compomics.cell_coord.gui.controller.computation.ComputationMainController.java

/**
 * Initialize some GUI components./*from   ww  w.  ja  v a2 s  .  com*/
 */
private void initMainView() {
    // format the tables
    JTableHeader samplesHeader = getMainFrame().getSamplesTable().getTableHeader();
    samplesHeader.setBackground(GuiUtils.getHeaderColor());
    samplesHeader.setFont(GuiUtils.getHeaderFont());
    samplesHeader.setReorderingAllowed(false);

    JTableHeader tracksHeader = getMainFrame().getTracksTable().getTableHeader();
    tracksHeader.setBackground(GuiUtils.getHeaderColor());
    tracksHeader.setFont(GuiUtils.getHeaderFont());
    tracksHeader.setReorderingAllowed(false);

    getMainFrame().getSamplesTable().setRowSelectionAllowed(true);
    getMainFrame().getSamplesTable().setColumnSelectionAllowed(false);
    getMainFrame().getSamplesTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    getMainFrame().getTracksTable().setRowSelectionAllowed(true);
    getMainFrame().getTracksTable().setColumnSelectionAllowed(false);
    getMainFrame().getTracksTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // if you click on a sample, the relative tracks are shown in another table
    getMainFrame().getSamplesTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                int selectedRow = getMainFrame().getSamplesTable().getSelectedRow();
                if (selectedRow != -1) {
                    Sample selectedSample = getSamples().get(selectedRow);
                    showTracksInTable(selectedSample);
                    computationDataController.computeSample(selectedSample);
                    // call child controller to show sample data in table
                    computationDataController.showSampleData(selectedSample);
                    // call child controller to plot whatever we need to plot
                }
            }
        }
    });

    // if you click on a track, the relative spots are shown in another table
    getMainFrame().getTracksTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                Sample selectedSample = getSamples().get(getMainFrame().getSamplesTable().getSelectedRow());
                int selectedRow = getMainFrame().getTracksTable().getSelectedRow();
                if (selectedRow != -1) {
                    Track selectedTrack = selectedSample.getTracks().get(selectedRow);
                    // call child controller to show track data in table
                    computationDataController.showStepData(selectedTrack);
                    computationDataController.showTrackData(selectedTrack);
                    // call child controller to plot whatever we need to plot
                    computationDataController.plotStepData(selectedTrack);
                    computationDataController.plotTrackData(selectedTrack);
                }
            }
        }
    });
}

From source file:com.compomics.cell_coord.gui.controller.computation.ComputationDataController.java

/**
 * Initialize main view./*from   w ww .j a v a 2s  .c  om*/
 */
private void initComputationDataPanel() {
    // make a new object
    computationDataPanel = new ComputationDataPanel();

    // format the tables
    JTableHeader header = computationDataPanel.getStepDataTable().getTableHeader();
    header.setBackground(GuiUtils.getHeaderColor());
    header.setFont(GuiUtils.getHeaderFont());
    header.setReorderingAllowed(false);

    header = computationDataPanel.getSampleDataTable().getTableHeader();
    header.setBackground(GuiUtils.getHeaderColor());
    header.setFont(GuiUtils.getHeaderFont());
    header.setReorderingAllowed(false);

    header = computationDataPanel.getSampleDataTable().getTableHeader();
    header.setBackground(GuiUtils.getHeaderColor());
    header.setFont(GuiUtils.getHeaderFont());
    header.setReorderingAllowed(false);

    header = computationDataPanel.getTrackDataTable().getTableHeader();
    header.setBackground(GuiUtils.getHeaderColor());
    header.setFont(GuiUtils.getHeaderFont());
    header.setReorderingAllowed(false);

    computationDataPanel.getStepDataTable().setRowSelectionAllowed(true);
    computationDataPanel.getStepDataTable().setColumnSelectionAllowed(false);
    computationDataPanel.getStepDataTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    computationDataPanel.getStepDataTable().setRowSelectionAllowed(true);
    computationDataPanel.getStepDataTable().setColumnSelectionAllowed(false);
    computationDataPanel.getStepDataTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // add component to main container
    computationMainController.getMainFrame().getComputationDataParentPanel().add(computationDataPanel,
            gridBagConstraints);
}

From source file:com.compomics.cell_coord.gui.controller.load.LoadTracksController.java

/**
 * Initialize the view components.//from  w w  w.j a va2 s  . c o  m
 */
private void initView() {
    // create new view
    loadTracksPanel = new LoadTracksPanel();
    // disable the IMPORT FILES button
    loadTracksPanel.getImportFilesButton().setEnabled(false);
    // initialize the flag to keep track of importing
    isImported = false;
    // populate the combobox with available file formats
    // note: these are annoatated as spring beans
    Set<String> parsers = TrackFileParserFactory.getInstance().getParserBeanNames();
    for (String parser : parsers) {
        loadTracksPanel.getFileFormatComboBox().addItem(parser);
    }

    // format the table
    JTableHeader tracksTableHeader = loadTracksPanel.getTracksTable().getTableHeader();
    tracksTableHeader.setBackground(GuiUtils.getHeaderColor());
    tracksTableHeader.setFont(GuiUtils.getHeaderFont());
    tracksTableHeader.setReorderingAllowed(false);

    /**
     * Action Listeners.
     */
    // load directory
    loadTracksPanel.getLoadDirectoryButton().addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (directory == null) {
                chooseDirectoryAndLoadData();
            } else {
                // otherwise we ask the user if they want to reload the directory
                Object[] options = { "Load a different directory", "Cancel" };
                int showOptionDialog = JOptionPane.showOptionDialog(null,
                        "It seems a directory was already loaded.\nWhat do you want to do?", "",
                        JOptionPane.CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
                switch (showOptionDialog) {
                case 0: // load a different directory:
                    // reset the model of the directory tree
                    DefaultTreeModel model = (DefaultTreeModel) loadTracksPanel.getDirectoryTree().getModel();
                    DefaultMutableTreeNode rootNote = (DefaultMutableTreeNode) model.getRoot();
                    rootNote.removeAllChildren();
                    model.reload();
                    chooseDirectoryAndLoadData();
                    loadTracksPanel.getChosenDirectoryTextArea().setText(directory.getAbsolutePath());
                    break; // cancel: do nothing
                }
            }
        }
    });

    // import the selected files in the tree
    loadTracksPanel.getImportFilesButton().addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // check if an import already took place
            if (!isImported) {
                // get the selected file(s) from the JTree
                TreePath[] selectionPaths = loadTracksPanel.getDirectoryTree().getSelectionPaths();
                if (selectionPaths != null && selectionPaths.length != 0) {
                    // at least a file was selected --  proceed with the import
                    importFiles();
                    isImported = true;
                    cellCoordController.showMessage(selectionPaths.length + " file(s) successfully imported!",
                            "success loading", JOptionPane.INFORMATION_MESSAGE);
                    // do basic computations
                    preprocess();
                    // go to child controllers and show samples in the tables
                    summaryDataController.showSamplesInTable();
                    computationMainController.showSamplesInTable();
                    // proceed with next step in the plugin
                    cellCoordController.getCellCoordFrame().getNextButton().setEnabled(true);
                } else {
                    // inform the user that no file was selected!
                    cellCoordController.showMessage("You have to select at least one file!",
                            "no files selected", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                // an import already took place: ask for user input
                Object[] options = { "Load other file(s)", "Cancel" };
                int showOptionDialog = JOptionPane.showOptionDialog(loadTracksPanel,
                        "It seems some files were already loaded.\nWhat do you want to do?", "",
                        JOptionPane.CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
                switch (showOptionDialog) {
                case 0: // load other files
                    // clear the sample list
                    samples.clear();
                    // clear the track spot list
                    trackSpotsBindingList.clear();
                    // clear the selection in the JTree
                    loadTracksPanel.getDirectoryTree().clearSelection();
                    isImported = false;
                    // inform the user they need to select other files
                    JOptionPane.showMessageDialog(loadTracksPanel, "Please select other files", "",
                            JOptionPane.INFORMATION_MESSAGE);
                    break; // cancel: do nothing
                }
            }

        }
    });

    // add view to parent component
    cellCoordController.getCellCoordFrame().getLoadTracksParentPanel().add(loadTracksPanel, gridBagConstraints);
}

From source file:nl.phanos.liteliveresultsclient.gui.ResultsWindows.java

private void initCustumComponents() {
    logoLabel = new javax.swing.JLabel();
    icon = new ImageIcon(getCLub());
    logoLabel.setIcon(icon); // NOI18N
    logoLabel.setBounds(this.getWidth() - icon.getIconWidth(), this.getHeight() - icon.getIconHeight(),
            icon.getIconWidth(), icon.getIconHeight());
    LayerdPane.add(logoLabel, JLayeredPane.PALETTE_LAYER);
    clockLabel = new javax.swing.JLabel();
    clockLabel.setFont(new java.awt.Font("Lucida Grande", 0, fontSize)); // NOI18N
    clockLabel.setText("");
    clockLabel.setForeground(Color.YELLOW);
    LayerdPane.add(clockLabel, JLayeredPane.PALETTE_LAYER);
    jTable1.setRowSelectionAllowed(false);
    jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {},
            new String[] { "Plaats", "Atleet", "Tijd" }) {
        Class[] types = new Class[] { java.lang.Integer.class, java.lang.String.class, java.lang.String.class };
        boolean[] canEdit = new boolean[] { false, false, false };

        public Class getColumnClass(int columnIndex) {
            return types[columnIndex];
        }// ww  w  .ja  va  2  s  . co  m

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit[columnIndex];
        }
    });

    jScrollPane1.getViewport().setBackground(Color.black);
    this.setBackground(Color.black);
    JTableHeader header = jTable1.getTableHeader();
    header.setOpaque(false);
    jPanel1.setBackground(Color.black);
    header.setBackground(Color.black);
    header.setForeground(Color.YELLOW);
    DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer();
    headerRenderer.setBackground(Color.BLACK);

    for (int i = 0; i < jTable1.getModel().getColumnCount(); i++) {
        if (jTable1.getModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer);
        }
    }
    DefaultTableCellRenderer LEFTRenderer = new DefaultTableCellRenderer();
    LEFTRenderer.setHorizontalAlignment(JLabel.LEFT);
    jTable1.getColumnModel().getColumn(0).setCellRenderer(LEFTRenderer);
    ChangeFont(fontSize);
}

From source file:org.isatools.isacreator.spreadsheet.Spreadsheet.java

/**
 * Setup the JTable with its desired characteristics
 *///from  ww  w .  j  a  va2  s  . c  om
private void setupTable() {
    table = new CustomTable(spreadsheetModel);
    table.setShowGrid(true);
    table.setGridColor(Color.BLACK);
    table.setShowVerticalLines(true);
    table.setShowHorizontalLines(true);
    table.setGridColor(UIHelper.LIGHT_GREEN_COLOR);
    table.setRowSelectionAllowed(true);
    table.setColumnSelectionAllowed(true);
    table.setAutoCreateColumnsFromModel(false);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.getSelectionModel().addListSelectionListener(this);
    table.getColumnModel().getSelectionModel().addListSelectionListener(this);
    table.getTableHeader().setReorderingAllowed(true);
    table.getColumnModel().addColumnModelListener(this);
    try {
        table.setDefaultRenderer(Class.forName("java.lang.Object"), new SpreadsheetCellRenderer());
    } catch (ClassNotFoundException e) {
        // ignore this error
    }

    table.addMouseListener(this);
    table.getTableHeader().addMouseMotionListener(new MouseMotionListener() {
        public void mouseDragged(MouseEvent event) {
        }

        public void mouseMoved(MouseEvent event) {
            // display a tooltip when user hovers over a column. tooltip is derived
            // from the description of a field from the TableReferenceObject.
            JTable table = ((JTableHeader) event.getSource()).getTable();
            TableColumnModel colModel = table.getColumnModel();
            int colIndex = colModel.getColumnIndexAtX(event.getX());

            // greater than 1 to account for the row no. being the first col
            if (colIndex >= 1) {
                TableColumn tc = colModel.getColumn(colIndex);
                if (tc != null) {
                    try {
                        table.getTableHeader().setToolTipText(getFieldDescription(tc));
                    } catch (Exception e) {
                        // ignore this error
                    }
                }
            }
        }
    });

    //table.getColumnModel().addColumnModelListener(this);
    InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);

    //  Override the default tab behaviour
    //  Tab to the next editable cell. When no editable cells goto next cell.
    final Action previousTabAction = table.getActionMap().get(im.get(tab));
    Action newTabAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            // maintain previous tab action procedure
            previousTabAction.actionPerformed(e);

            JTable table = (JTable) e.getSource();
            int row = table.getSelectedRow();
            int originalRow = row;
            int column = table.getSelectedColumn();
            int originalColumn = column;

            while (!table.isCellEditable(row, column)) {
                previousTabAction.actionPerformed(e);
                row = table.getSelectedRow();
                column = table.getSelectedColumn();

                //  Back to where we started, get out.
                if ((row == originalRow) && (column == originalColumn)) {
                    break;
                }
            }

            if (table.editCellAt(row, column)) {
                table.getEditorComponent().requestFocusInWindow();
            }
        }
    };

    table.getActionMap().put(im.get(tab), newTabAction);
    TableColumnModel model = table.getColumnModel();

    String previousColumnName = null;
    for (int columnIndex = 0; columnIndex < tableReferenceObject.getHeaders().size(); columnIndex++) {
        if (!model.getColumn(columnIndex).getHeaderValue().toString()
                .equals(TableReferenceObject.ROW_NO_TEXT)) {
            model.getColumn(columnIndex).setHeaderRenderer(columnRenderer);
            model.getColumn(columnIndex).setPreferredWidth(spreadsheetFunctions
                    .calcColWidths(model.getColumn(columnIndex).getHeaderValue().toString()));
            // add appropriate cell editor for cell.
            spreadsheetFunctions.addCellEditor(model.getColumn(columnIndex), previousColumnName);
            previousColumnName = model.getColumn(columnIndex).getHeaderValue().toString();
        } else {
            model.getColumn(columnIndex).setHeaderRenderer(new RowNumberCellRenderer());
        }
    }

    JTableHeader header = table.getTableHeader();
    header.setBackground(UIHelper.BG_COLOR);
    header.addMouseListener(new HeaderListener(header, columnRenderer));

    table.addNotify();
}