protected void installDefaults() {
   Color defaultGridColor = UIManager.getColor("Table.gridColor");
   Color defaultForegroundColor = UIManager.getColor("Table.foreground");
   Color defaultBackgroundColor = UIManager.getColor("Table.background");
   Border defaultBorder = UIManager.getBorder("Table.scrollPaneBorder");
   Color defaultSelectionForeground = UIManager.getColor("Table.selectionForeground");
   Color defaultSelectionBackground = UIManager.getColor("Table.selectionBackground");
   Color defaultFocusCellForeground = UIManager.getColor("Table.focusCellForeground");
   Color defaultFocusCellBackground = new Color(153, 153, 204);
   Font defaultFont = UIManager.getFont("Table.font");
   Border defaultGridBorder = UIManager.getBorder("Table.border");
   InputMap inputMap = (InputMap) UIManager.get("Table.ancestorInputMap");
   if (!installed) {
     UIManager.getDefaults().put("Grid.gridColor", defaultGridColor);
     UIManager.getDefaults().put("Grid.foreground", defaultForegroundColor);
     UIManager.getDefaults().put("Grid.background", defaultBackgroundColor);
     UIManager.getDefaults().put("Grid.selectionForegroundColor", defaultSelectionForeground);
     UIManager.getDefaults().put("Grid.selectionBackgroundColor", defaultSelectionBackground);
     UIManager.getDefaults().put("Grid.focusForegroundColor", defaultFocusCellForeground);
     UIManager.getDefaults().put("Grid.focusBackgroundColor", defaultFocusCellBackground);
     UIManager.getDefaults().put("Grid.border", defaultGridBorder);
     UIManager.getDefaults().put("Grid.font", defaultFont);
     UIManager.getDefaults().put("Grid.scrollPaneBorder", defaultBorder);
     UIManager.getDefaults().put("Grid.ancestorInputMap", inputMap);
     installed = true;
   }
   Color foregroundColor = grid.getForeground();
   Color backgroundColor = grid.getBackground();
   Font font = grid.getFont();
   Border border = grid.getBorder();
   Color gridColor = grid.getGridColor();
   Color selectionForeground = grid.getSelectionForegroundColor();
   Color selectionBackground = grid.getSelectionBackgroundColor();
   Color focusForeground = grid.getFocusForegroundColor();
   Color focusBackground = grid.getFocusBackgroundColor();
   if (foregroundColor == null || foregroundColor instanceof UIResource)
     grid.setForeground(defaultForegroundColor);
   if (backgroundColor == null || backgroundColor instanceof UIResource)
     grid.setBackground(defaultBackgroundColor);
   if (font == null || font instanceof UIResource) grid.setFont(defaultFont);
   if (gridColor == null || gridColor instanceof UIResource) grid.setGridColor(defaultGridColor);
   if (border == null || border instanceof UIResource) grid.setBorder(defaultGridBorder);
   if (selectionForeground == null || selectionForeground instanceof UIResource)
     grid.setSelectionForegroundColor(defaultSelectionForeground);
   if (selectionBackground == null || selectionBackground instanceof UIResource)
     grid.setSelectionBackgroundColor(defaultSelectionBackground);
   if (focusForeground == null || focusForeground instanceof UIResource)
     grid.setFocusForegroundColor(defaultFocusCellForeground);
   if (focusBackground == null || focusBackground instanceof UIResource)
     grid.setFocusBackgroundColor(defaultFocusCellBackground);
 }
Esempio n. 2
0
  public FileNameRenderer(JTable table) {
    Border b = UIManager.getBorder("Table.noFocusBorder");
    if (Objects.isNull(b)) { // Nimbus???
      Insets i = focusCellHighlightBorder.getBorderInsets(textLabel);
      b = BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right);
    }
    noFocusBorder = b;

    p.setOpaque(false);
    panel.setOpaque(false);

    // http://www.icongalore.com/ XP Style Icons - Windows Application Icon, Software XP Icons
    nicon = new ImageIcon(getClass().getResource("wi0063-16.png"));
    sicon =
        new ImageIcon(
            p.createImage(
                new FilteredImageSource(nicon.getImage().getSource(), new SelectedImageFilter())));

    iconLabel = new JLabel(nicon);
    iconLabel.setBorder(BorderFactory.createEmptyBorder());

    p.add(iconLabel, BorderLayout.WEST);
    p.add(textLabel);
    panel.add(p, BorderLayout.WEST);

    Dimension d = iconLabel.getPreferredSize();
    dim.setSize(d);
    table.setRowHeight(d.height);
  }
 protected void installDefaults() {
   String string = UIManager.getLookAndFeel().getName();
   Color defaultGridColor = UIManager.getColor("Table.gridColor");
   Color defaultForegroundColor = UIManager.getColor("TableHeader.foreground");
   Color defaultBackgroundColor = UIManager.getColor("TableHeader.background");
   Font defaultGridFont = UIManager.getFont("Table.font");
   Border defaultGridBorder = UIManager.getBorder("TableHeader.border");
   Color defaultSelectionForegroundColor = defaultForegroundColor.brighter();
   Color defaultSelectionBackgroundColor = defaultBackgroundColor;
   Color defaultFocusForegroundColor = defaultForegroundColor.brighter();
   Color defaultFocusBackgroundColor = defaultBackgroundColor.brighter();
   if (!installedHeader) {
     UIManager.getDefaults().put("GridHeader.gridColor", defaultGridColor);
     UIManager.getDefaults().put("GridHeader.foreground", defaultForegroundColor);
     UIManager.getDefaults().put("GridHeader.background", defaultBackgroundColor);
     UIManager.getDefaults()
         .put("GridHeader.selectionForegroundColor", defaultSelectionForegroundColor);
     UIManager.getDefaults()
         .put("GridHeader.selectionBackgroundColor", defaultSelectionBackgroundColor);
     UIManager.getDefaults().put("GridHeader.focusForegroundColor", defaultFocusForegroundColor);
     UIManager.getDefaults().put("GridHeader.focusBackgroundColor", defaultFocusBackgroundColor);
     UIManager.getDefaults().put("GridHeader.border", defaultGridBorder);
     UIManager.getDefaults().put("GridHeader.font", defaultGridFont);
   }
   Color foregroundColor = gridHeader.getForeground();
   Color backgroundColor = gridHeader.getBackground();
   Font gridFont = gridHeader.getFont();
   Border gridBorder = gridHeader.getBorder();
   Color gridColor = gridHeader.getGridColor();
   Color selectionForegroundColor = gridHeader.getSelectionForegroundColor();
   Color selectionBackgroundColor = gridHeader.getSelectionBackgroundColor();
   Color focusForegroundColor = gridHeader.getFocusForegroundColor();
   Color focusBackgroundColor = gridHeader.getFocusBackgroundColor();
   if (foregroundColor == null || foregroundColor instanceof UIResource)
     gridHeader.setForeground(defaultForegroundColor);
   if (backgroundColor == null || backgroundColor instanceof UIResource)
     gridHeader.setBackground(defaultBackgroundColor);
   if (gridColor == null || gridColor instanceof UIResource)
     gridHeader.setGridColor(defaultGridColor);
   if (gridFont == null || gridFont instanceof UIResource) gridHeader.setFont(defaultGridFont);
   if (gridBorder == null || gridBorder instanceof UIResource)
     gridHeader.setBorder(defaultGridBorder);
   if (selectionForegroundColor == null || selectionForegroundColor instanceof UIResource)
     gridHeader.setSelectionForegroundColor(defaultSelectionForegroundColor);
   if (selectionBackgroundColor == null || selectionBackgroundColor instanceof UIResource)
     gridHeader.setSelectionBackgroundColor(defaultSelectionBackgroundColor);
   if (focusForegroundColor == null || focusForegroundColor instanceof UIResource)
     gridHeader.setFocusForegroundColor(defaultFocusForegroundColor);
   if (focusBackgroundColor == null || focusBackgroundColor instanceof UIResource)
     gridHeader.setFocusBackgroundColor(defaultFocusBackgroundColor);
 }
Esempio n. 4
0
class FileNameRenderer implements TableCellRenderer {
  private final Dimension dim = new Dimension();
  private final JPanel p =
      new JPanel(new BorderLayout()) {
        @Override
        public Dimension getPreferredSize() {
          return dim;
        }
      };
  private final JPanel panel = new JPanel(new BorderLayout());
  private final JLabel textLabel = new JLabel(" ");
  private final JLabel iconLabel;
  private final Border focusCellHighlightBorder =
      UIManager.getBorder("Table.focusCellHighlightBorder");
  private final Border noFocusBorder;
  private final ImageIcon nicon;
  private final ImageIcon sicon;

  public FileNameRenderer(JTable table) {
    Border b = UIManager.getBorder("Table.noFocusBorder");
    if (Objects.isNull(b)) { // Nimbus???
      Insets i = focusCellHighlightBorder.getBorderInsets(textLabel);
      b = BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right);
    }
    noFocusBorder = b;

    p.setOpaque(false);
    panel.setOpaque(false);

    // http://www.icongalore.com/ XP Style Icons - Windows Application Icon, Software XP Icons
    nicon = new ImageIcon(getClass().getResource("wi0063-16.png"));
    sicon =
        new ImageIcon(
            p.createImage(
                new FilteredImageSource(nicon.getImage().getSource(), new SelectedImageFilter())));

    iconLabel = new JLabel(nicon);
    iconLabel.setBorder(BorderFactory.createEmptyBorder());

    p.add(iconLabel, BorderLayout.WEST);
    p.add(textLabel);
    panel.add(p, BorderLayout.WEST);

    Dimension d = iconLabel.getPreferredSize();
    dim.setSize(d);
    table.setRowHeight(d.height);
  }

  @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;
  }
}