Example usage for javax.swing JTable getBackground

List of usage examples for javax.swing JTable getBackground

Introduction

In this page you can find the example usage for javax.swing JTable getBackground.

Prototype

@Transient
public Color getBackground() 

Source Link

Document

Gets the background color of this component.

Usage

From source file:de.codesourcery.eve.skills.ui.components.impl.planning.ResourceStatusComponent.java

@Override
protected JPanel createPanel() {
    final JPanel result = new JPanel();

    table.setFillsViewportHeight(true);/*www  .  j  av a 2s  .  c om*/

    table.setRowSorter(model.getRowSorter());

    FixedBooleanTableCellRenderer.attach(table);

    table.setDefaultRenderer(Integer.class, new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if (!(value instanceof Integer)) {
                return this;
            }

            if (column == 0) {
                setHorizontalAlignment(SwingConstants.LEFT);
            } else {
                setHorizontalAlignment(SwingConstants.RIGHT);
            }

            Integer amount = (Integer) value;
            if (amount.intValue() < 0) {
                if (!isSelected) {
                    setBackground(Color.RED);
                } else {
                    setBackground(table.getSelectionBackground());
                }
            } else {
                setBackground(table.getBackground());
            }
            return this;
        }
    });

    addToShoppingListButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final List<ItemWithQuantity> items = model.getSelectedItems();

            if (items.isEmpty()) {
                return;
            }

            final ShoppingListEditorComponent comp = new ShoppingListEditorComponent(title, "", items);

            comp.setModal(true);
            ComponentWrapper.wrapComponent(comp).setVisible(true);
            if (!comp.wasCancelled() && !comp.getShoppingList().isEmpty()) {
                shoppingListManager.addShoppingList(comp.getShoppingList());
                getComponentCallback().dispose(ResourceStatusComponent.this);
            }

        }
    });

    new GridLayoutBuilder()
            .add(new GridLayoutBuilder.VerticalGroup(new GridLayoutBuilder.Cell(new JScrollPane(table)),
                    new GridLayoutBuilder.FixedCell(addToShoppingListButton)))
            .addTo(result);
    return result;
}

From source file:com.diversityarrays.kdxplore.trials.PlotCellRenderer.java

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    boolean cellSelected = table.isCellSelected(row, column);

    super.getTableCellRendererComponent(table, value, cellSelected, hasFocus, row, column);

    setToolTipText(null);//from w w  w  .j  a va  2  s .  c om

    commentMarker = null;
    sampleIconType = SampleIconType.NORMAL;
    if (value instanceof Plot) {
        Plot plot = (Plot) value;
        handlePlot(table, isSelected, hasFocus, row, column, cellSelected, plot);
        if (!plot.isActivated()) {
            sampleIconType = isSelected ? SampleIconType.INACTIVE_PLOT_SELECTED : SampleIconType.INACTIVE_PLOT;
        }
    } else {
        if (cellSelected) {
            this.setBackground(table.getSelectionBackground());
            this.setForeground(table.getSelectionForeground());
        } else {
            this.setBackground(table.getBackground());
            this.setForeground(table.getForeground());
        }
    }

    return this;
}

From source file:com.diversityarrays.kdxplore.trials.PlotCellRenderer.java

private void handlePlot(JTable table, boolean isSelected, boolean hasFocus, int row, int column,
        boolean cellSelected, Plot plot) {
    StringBuilder sb = new StringBuilder("<HTML>");

    if (plotVisitList != null) {
        PlotVisitGroup pvg = plotVisitList.getPlotVisitGroup(plot);
        if (pvg != null) {
            WalkSegment ws = pvg.walkSegment;
            sb.append(ws.segmentIndex + 1).append(":").append(ws.orientationUDLR.unicodeArrow).append("<BR>");
        }/*from   ww w .ja  v a 2  s.  co  m*/
    }

    if (showUserPlotId) {
        sb.append("<b>").append(plot.getUserPlotId()).append("</b>");
    }

    //      Collection<String> tagLabels = plot.getTagLabels();
    // Instead of the above, collect for all SampleGroups
    Set<String> tagLabels = plot.getTagsBySampleGroup().entrySet().stream().flatMap(e -> e.getValue().stream())
            .map(tag -> tag.getLabel()).collect(Collectors.toSet());

    addAnnotations(plot, tagLabels, sb);

    String plotType = plot.getPlotType();
    if (plotType != null && !plotType.isEmpty()) {
        sb.append(": <i>").append(StringUtil.htmlEscape(plotType)).append("</i>");
    }

    Pair<Integer, Integer> cell = new Pair<Integer, Integer>(row, column);

    Map<TraitInstance, Comparable<?>> tempMap = new HashMap<TraitInstance, Comparable<?>>();
    if (temporaryValues.containsKey(cell)) {
        tempMap = temporaryValues.get(cell);
    }

    boolean anyAttributesAdded = false;

    if (attributeNames != null && !attributeNames.isEmpty()) {
        if (plot.plotAttributeValues != null) {
            for (PlotAttributeValue pav : plot.plotAttributeValues) {
                if (attributeNames.contains(pav.plotAttributeName)) {
                    anyAttributesAdded = true;
                    String attrValue = pav.getAttributeValue();
                    sb.append("<br/>");
                    PlotAttribute pa = plotAttributeProvider.transform(pav);
                    sb.append("<i>").append(StringUtil.htmlEscape(pa.getPlotAttributeName())).append("</i>");
                    sb.append(": ");
                    if (attrValue == null) {
                        sb.append("-no-value-");
                    } else {
                        sb.append(StringUtil.htmlEscape(attrValue));
                    }
                }
            }
        }
    }

    String tttResult = null;
    if (!traitInstancesByTraitId.isEmpty()) {
        // Appends html to sb. Also sets commentMarker
        tttResult = collectCellHtml(isSelected, plot, sb, tempMap);
    }
    if (!Check.isEmpty(tagLabels)) {
        StringBuilder tmp = new StringBuilder();
        if (tttResult != null) {
            if (!tttResult.startsWith("<HTML>")) {
                tmp.append("<HTML>");
            }
            tmp.append(tttResult).append("<br>");
        } else {
            tmp.append("<HTML>");
        }
        String tagsep = "";
        for (String tag : tagLabels) {
            tmp.append(tagsep).append(tag);
            tagsep = ", ";
        }
        tttResult = tmp.toString();
    }
    setToolTipText(tttResult);

    if (activeInstance != null) {
        Color c = colorProvider == null ? table.getSelectionBackground()
                : colorProvider.getTraitInstanceColor(activeInstance).getBackground();

        if (!cellSelected) {
            cellSelected = fieldViewSelectionModel.isSelectedPlot(plot);
        }

        if (cellSelected) {
            this.setBackground(c);
            this.setForeground(table.getForeground());
        } else {
            this.setBackground(table.getBackground());
            this.setForeground(table.getForeground());
        }
    } else {
        if (cellSelected) {
            this.setBackground(table.getSelectionBackground());
            this.setForeground(table.getSelectionForeground());
        } else {
            this.setBackground(table.getBackground());
            this.setForeground(table.getForeground());
        }
    }

    setText(sb.toString());
    if (isSelected && hasFocus) {
        setBorder(focusSelectedCellHighlightBorder);
    } else {
        setBorder(border);
    }
}

From source file:org.dc.file.search.ui.DashboardForm.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
    return this;
}

From source file:org.dc.file.search.ui.DashboardForm.java

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
    if (jTable != null) {
        String fileName;//from   w w w .j ava 2 s.  c o m
        String comment = "";
        if (type == DashboardForm.StarRatingsType.COMMENT) {
            comment = (String) jTable.getModel().getValueAt(row, DashboardForm.COMMENT_COL_INDEX);
            fileName = selectedFile;
        } else {
            fileName = (String) jTable.getModel().getValueAt(row, DashboardForm.FILE_COL_INDEX);
        }
        updateValue((StarRater) value, fileName, comment, type);
    }
    return this;
}

From source file:org.formic.wizard.step.gui.FeatureListStep.java

/**
 * {@inheritDoc}/*from   w  w w .  j  a  v a  2s.com*/
 * @see org.formic.wizard.step.GuiStep#init()
 */
public Component init() {
    featureInfo = new JEditorPane("text/html", StringUtils.EMPTY);
    featureInfo.setEditable(false);
    featureInfo.addHyperlinkListener(new HyperlinkListener());

    Feature[] features = provider.getFeatures();
    JTable table = new JTable(features.length, 1) {
        private static final long serialVersionUID = 1L;

        public Class getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }

        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    table.setBackground(new javax.swing.JList().getBackground());

    GuiForm form = createForm();

    for (int ii = 0; ii < features.length; ii++) {
        final Feature feature = (Feature) features[ii];
        final JCheckBox box = new JCheckBox();

        String name = getName() + '.' + feature.getKey();
        form.bind(name, box);

        box.putClientProperty("feature", feature);
        featureMap.put(feature.getKey(), box);

        if (feature.getInfo() == null) {
            feature.setInfo(Installer.getString(getName() + "." + feature.getKey() + ".html"));
        }
        feature.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                if (Feature.ENABLED_PROPERTY.equals(event.getPropertyName())) {
                    if (box.isSelected() != feature.isEnabled()) {
                        box.setSelected(feature.isEnabled());
                    }
                }
            }
        });

        box.setText(Installer.getString(name));
        box.setBackground(table.getBackground());
        if (!feature.isAvailable()) {
            box.setSelected(false);
            box.setEnabled(false);
        }
        table.setValueAt(box, ii, 0);
    }

    FeatureListMouseListener mouseListener = new FeatureListMouseListener();
    for (int ii = 0; ii < features.length; ii++) {
        Feature feature = (Feature) features[ii];
        if (feature.isEnabled() && feature.isAvailable()) {
            JCheckBox box = (JCheckBox) featureMap.get(feature.getKey());
            box.setSelected(true);
            mouseListener.processDependencies(feature);
            mouseListener.processExclusives(feature);
        }
    }

    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.setDefaultRenderer(JCheckBox.class, new ComponentTableCellRenderer());

    table.addKeyListener(new FeatureListKeyListener());
    table.addMouseListener(mouseListener);
    table.getSelectionModel().addListSelectionListener(new FeatureListSelectionListener(table));

    table.setRowSelectionInterval(0, 0);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    JPanel container = new JPanel(new BorderLayout());
    container.add(table, BorderLayout.CENTER);
    panel.add(new JScrollPane(container), BorderLayout.CENTER);
    JScrollPane infoScroll = new JScrollPane(featureInfo);
    infoScroll.setMinimumSize(new Dimension(0, 50));
    infoScroll.setMaximumSize(new Dimension(0, 50));
    infoScroll.setPreferredSize(new Dimension(0, 50));
    panel.add(infoScroll, BorderLayout.SOUTH);

    return panel;
}

From source file:org.nuclos.client.ui.collect.component.AbstractCollectableComponent.java

public static void setBackgroundColor(Component cellRendererComponent, JTable tbl, Object oValue,
        boolean bSelected, boolean bHasFocus, int iRow, int iColumn) {
    cellRendererComponent.setBackground(bSelected ? tbl.getSelectionBackground()
            : iRow % 2 == 0 ? tbl.getBackground() : NuclosThemeSettings.BACKGROUND_PANEL);
    cellRendererComponent.setForeground(bSelected ? tbl.getSelectionForeground() : tbl.getForeground());

    final TableModel tm;
    final int adjustColIndex;
    if (tbl instanceof FixedColumnRowHeader.HeaderTable
            && ((FixedColumnRowHeader.HeaderTable) tbl).getExternalTable() != null) {
        tm = ((FixedColumnRowHeader.HeaderTable) tbl).getExternalTable().getModel();
        adjustColIndex = FixedRowIndicatorTableModel.ROWMARKERCOLUMN_COUNT;
    } else {/*from w  w w. j a v a  2 s .c  o  m*/
        tm = tbl.getModel();
        adjustColIndex = 0;
    }

    // check whether the data of the component is readable for current user, by asking the security agent of the actual field
    if (tm instanceof SortableCollectableTableModel<?>) {
        final SortableCollectableTableModel<Collectable> tblModel = (SortableCollectableTableModel<Collectable>) tm;
        if (tblModel.getRowCount() > iRow) {
            final Collectable clct = tblModel.getCollectable(iRow);
            final Integer iTColumn = tbl.getColumnModel().getColumn(iColumn).getModelIndex() - adjustColIndex;
            final CollectableEntityField clctef = tblModel.getCollectableEntityField(iTColumn);
            if (clctef == null) {
                throw new NullPointerException("getTableCellRendererComponent failed to find field: " + clct
                        + " tm index " + iTColumn);
            }

            boolean isEnabled = true;
            if (!clctef.isNullable() && isNull(oValue)) {
                cellRendererComponent.setBackground(getMandatoryColor());
                cellRendererComponent.setForeground(tbl.getForeground());
            } else {
                //               if (clct.getId() == null) {
                //                  cellRendererComponent.setBackground(tbl.getBackground());
                //                  cellRendererComponent.setForeground(tbl.getForeground());
                //               } else {
                if (tbl instanceof SubForm.SubFormTable) {
                    SubFormTable subformtable = (SubForm.SubFormTable) tbl;
                    Column subformcolumn = subformtable.getSubForm().getColumn(clctef.getName());
                    if (subformcolumn != null && !subformcolumn.isEnabled()) {
                        isEnabled = false;
                        if (bSelected) {
                            cellRendererComponent
                                    .setBackground(NuclosThemeSettings.BACKGROUND_INACTIVESELECTEDCOLUMN);
                        } else {
                            cellRendererComponent.setBackground(NuclosThemeSettings.BACKGROUND_INACTIVECOLUMN);
                        }
                    }
                }
                //               }

                try {
                    EntityMetaDataVO meta = MetaDataClientProvider.getInstance()
                            .getEntity(clctef.getEntityName());
                    if (meta.getRowColorScript() != null && !bSelected) {
                        AbstractCollectableComponent.setBackground(cellRendererComponent,
                                meta.getRowColorScript(), clct, meta, isEnabled);
                    }
                } catch (CommonFatalException ex) {
                    LOG.warn(ex);
                }
            }
        }
    }

    if (tbl instanceof TableRowMouseOverSupport) {
        TableRowMouseOverSupport trmos = (TableRowMouseOverSupport) tbl;
        if (trmos.isMouseOverRow(iRow)) {
            final Color bgColor = LangUtils.defaultIfNull(cellRendererComponent.getBackground(), Color.WHITE);
            cellRendererComponent
                    .setBackground(new Color(Math.max(0, bgColor.getRed() - (bgColor.getRed() * 8 / 100)),
                            Math.max(0, bgColor.getGreen() - (bgColor.getGreen() * 8 / 100)),
                            Math.max(0, bgColor.getBlue() - (bgColor.getBlue() * 8 / 100))));
            //            cellRendererComponent.setBackground(UIManager.getColor("Table.selectionBackground"));
        }
    }
}