@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { textLabel.setFont(table.getFont()); textLabel.setText(Objects.toString(value, "")); textLabel.setBorder(hasFocus ? focusCellHighlightBorder : noFocusBorder); FontMetrics fm = table.getFontMetrics(table.getFont()); Insets i = textLabel.getInsets(); int swidth = iconLabel.getPreferredSize().width + fm.stringWidth(textLabel.getText()) + i.left + i.right; int cwidth = table.getColumnModel().getColumn(column).getWidth(); dim.width = swidth > cwidth ? cwidth : swidth; if (isSelected) { textLabel.setOpaque(true); textLabel.setForeground(table.getSelectionForeground()); textLabel.setBackground(table.getSelectionBackground()); iconLabel.setIcon(sicon); } else { textLabel.setOpaque(false); textLabel.setForeground(table.getForeground()); textLabel.setBackground(table.getBackground()); iconLabel.setIcon(nicon); } return panel; }
public BorderedComponent(String text, JComponent comp, boolean collapsible) { super(null); this.comp = comp; // Only add border if text is not null if (text != null) { TitledBorder border; if (collapsible) { final JLabel textLabel = new JLabel(text); JPanel borderLabel = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0)) { public int getBaseline(int w, int h) { Dimension dim = textLabel.getPreferredSize(); return textLabel.getBaseline(dim.width, dim.height) + textLabel.getY(); } }; borderLabel.add(textLabel); border = new LabeledBorder(borderLabel); textLabel.setForeground(border.getTitleColor()); if (IS_WIN) { collapseIcon = new ImageIcon(getImage("collapse-winlf")); expandIcon = new ImageIcon(getImage("expand-winlf")); } else { collapseIcon = new ArrowIcon(SOUTH, textLabel); expandIcon = new ArrowIcon(EAST, textLabel); } moreOrLessButton = new JButton(collapseIcon); moreOrLessButton.setContentAreaFilled(false); moreOrLessButton.setBorderPainted(false); moreOrLessButton.setMargin(new Insets(0, 0, 0, 0)); moreOrLessButton.addActionListener(this); String toolTip = Messages.BORDERED_COMPONENT_MORE_OR_LESS_BUTTON_TOOLTIP; moreOrLessButton.setToolTipText(toolTip); borderLabel.add(moreOrLessButton); borderLabel.setSize(borderLabel.getPreferredSize()); add(borderLabel); } else { border = new TitledBorder(text); } setBorder(new CompoundBorder(new FocusBorder(this), border)); } else { setBorder(new FocusBorder(this)); } if (comp != null) { add(comp); } }