@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;
  }
  @Override
  protected void customizeCellRenderer(
      JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {
    if (!(value instanceof String)) {
      return;
    }

    String s = (String) value;
    if (shouldClip(s)) {
      s = clip(s);
    }

    SimpleTextAttributes attributes = SimpleTextAttributes.REGULAR_ATTRIBUTES;
    String problem = ((StringResourceTableModel) table.getModel()).getCellProblem(row, column);
    if (problem != null) {
      if (ConstantColumn.KEY.ordinal() == column) {
        attributes = SimpleTextAttributes.ERROR_ATTRIBUTES;
      } else {
        attributes = CELL_ERROR_ATTRIBUTES;
      }
    }

    Font currentFont = table.getFont();
    Font f = FontUtil.getFontAbleToDisplay(s, currentFont);
    if (currentFont != f) {
      setFont(f);
    }

    setToolTipText(problem);
    append(s, attributes);
  }
Exemple #3
0
 private static int getTableBaseline(JTable table, int height) {
   if (TABLE_LABEL == null) {
     TABLE_LABEL = new JLabel("");
     TABLE_LABEL.setBorder(new EmptyBorder(1, 1, 1, 1));
   }
   JLabel label = TABLE_LABEL;
   label.setFont(table.getFont());
   int rowMargin = table.getRowMargin();
   int baseline = getLabelBaseline(label, table.getRowHeight() - rowMargin);
   return baseline += rowMargin / 2;
 }
 @Override
 public Component getTableCellRendererComponent(
     JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
   if (isSelected) {
     setForeground(table.getSelectionForeground());
     setBackground(table.getSelectionBackground());
   } else {
     setForeground(table.getForeground());
     setBackground(table.getBackground());
   }
   setFont(table.getFont());
   setText(Objects.toString(value, ""));
   return this;
 }
 @Override
 public Component getTableCellEditorComponent(
     JTable table, Object value, boolean isSelected, int row, int column) {
   System.out.println("getTableCellEditorComponent");
   setFont(table.getFont());
   setText(Objects.toString(value, ""));
   EventQueue.invokeLater(
       new Runnable() {
         @Override
         public void run() {
           setCaretPosition(getText().length());
           requestFocusInWindow();
           System.out.println("invokeLater: getTableCellEditorComponent");
         }
       });
   return scroll;
 }
  private static void setColumnWidths(JTable table, MetricTableSpecification tableSpecification) {
    final TableModel model = table.getModel();
    final TableColumnModel columnModel = table.getColumnModel();

    final List<Integer> columnWidths = tableSpecification.getColumnWidths();
    final List<String> columnOrder = tableSpecification.getColumnOrder();
    if (columnWidths != null && !columnWidths.isEmpty()) {

      final int columnCount = model.getColumnCount();
      for (int i = 0; i < columnCount; i++) {
        final String columnName = model.getColumnName(i);
        final int index = columnOrder.indexOf(columnName);
        if (index != -1) {
          final Integer width = columnWidths.get(index);
          final TableColumn column = columnModel.getColumn(i);
          column.setPreferredWidth(width.intValue());
        }
      }
    } else {
      final Graphics graphics = table.getGraphics();
      final Font font = table.getFont();
      final FontMetrics fontMetrics = table.getFontMetrics(font);

      final int rowCount = model.getRowCount();
      int maxFirstColumnWidth = 100;
      for (int i = 0; i < rowCount; i++) {
        final String name = (String) model.getValueAt(i, 0);
        if (name != null) {
          final Rectangle2D stringBounds = fontMetrics.getStringBounds(name, graphics);
          final double stringWidth = stringBounds.getWidth();
          if (stringWidth > maxFirstColumnWidth) {
            maxFirstColumnWidth = (int) stringWidth;
          }
        }
      }

      final int allocatedFirstColumnWidth = Math.min(300, maxFirstColumnWidth + 5);
      final TableColumn column = columnModel.getColumn(0);
      column.setPreferredWidth(allocatedFirstColumnWidth);
    }
  }
  @Override
  public Component getTableCellRendererComponent(
      JTable table, Object value, boolean selected, boolean focused, int row, int column) {
    RendererComponent panel = getEditorPanel(table);
    EditorEx editor = panel.getEditor();
    editor.getColorsScheme().setEditorFontSize(table.getFont().getSize());

    editor
        .getColorsScheme()
        .setColor(EditorColors.SELECTION_BACKGROUND_COLOR, table.getSelectionBackground());
    editor
        .getColorsScheme()
        .setColor(EditorColors.SELECTION_FOREGROUND_COLOR, table.getSelectionForeground());
    editor.setBackgroundColor(selected ? table.getSelectionBackground() : table.getBackground());
    panel.setSelected(!Comparing.equal(editor.getBackgroundColor(), table.getBackground()));

    panel.setBorder(
        null); // prevents double border painting when ExtendedItemRendererComponentWrapper is used

    customizeEditor(editor, table, value, selected, row, column);
    return panel;
  }
  public Component getTableCellRendererComponent(
      JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Color foreground = null;
    Color background = null;
    Font font = null;
    TableModel model = table.getModel();
    if (model instanceof AttributiveCellTableModel) {
      CellAttribute cellAtt = ((AttributiveCellTableModel) model).getCellAttribute();
      if (cellAtt instanceof ColoredCell) {
        foreground = ((ColoredCell) cellAtt).getForeground(row, column);
        background = ((ColoredCell) cellAtt).getBackground(row, column);
      }
      if (cellAtt instanceof CellFont) {
        font = ((CellFont) cellAtt).getFont(row, column);
      }
    }
    if (isSelected) {
      setForeground((foreground != null) ? foreground : table.getSelectionForeground());
      setBackground(table.getSelectionBackground());
    } else {
      setForeground((foreground != null) ? foreground : table.getForeground());
      setBackground((background != null) ? background : table.getBackground());
    }
    setFont((font != null) ? font : table.getFont());

    if (hasFocus) {
      setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
      if (table.isCellEditable(row, column)) {
        setForeground(
            (foreground != null) ? foreground : UIManager.getColor("Table.focusCellForeground"));
        setBackground(UIManager.getColor("Table.focusCellBackground"));
      }
    } else {
      setBorder(noFocusBorder);
    }
    setValue(value);
    return this;
  }