JXTable自定义PainterHighlighter - 未调用paint

时间:2015-04-20 08:29:04

标签: java swing swingx

我尝试实现自定义画家荧光笔,但画家的绘画方法从未被调用过。在附加的示例中,不使用特殊的CellRenderer。 LAF是Nimbus。有什么问题?

public final class HighlighterExample {

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
      createUi();
    }
  });
}

private static void createUi() {
  JFrame frame = new JFrame();
  frame.setTitle("Example");
  frame.setSize(500, 500);
  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  frame.setLocationRelativeTo(null);
  try {
    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
      // Metal
      if ("Nimbus".equals(info.getName())) {
        UIManager.setLookAndFeel(info.getClassName());
        break;
      }
    }

  } catch (Exception e) {
    e.printStackTrace();
  }
  createContent(frame.getContentPane());
  frame.setVisible(true);
}

private static void createContent(final Container parent) {
  JXTable table = new JXTable();
  table.setModel(new ExampleModel());
  table.addHighlighter(new PainterHighlighter() {
    @Override
    protected boolean canHighlight(final Component component, final ComponentAdapter adapter) {
      Boolean value = Boolean.parseBoolean(adapter.getValue(0).toString());
      final boolean canHighlight = value && super.canHighlight(component, adapter);
      System.out.println("can highlight: " + canHighlight);
      return canHighlight;
    }

    @Override
    public Painter getPainter() {
      return new Painter() {
        @Override
        public void paint(final Graphics2D g, final Object object, final int width, final int height) {
          System.out.println("paint");
          g.setColor(Color.YELLOW);
          g.fillRect(0, 0, width, height);
        }
      };
    }
  });
  parent.add(new JScrollPane(table));
}

private static class ExampleModel extends AbstractTableModel {

  final static String[][] data = {
      {"true", "Donald", "Duck"},
      {"false", "Dagober", "Duck"}
  };


  @Override
  public int getRowCount() {
    return data.length;
  }

  @Override
  public int getColumnCount() {
    return data[0].length;
  }

  @Override
  public Object getValueAt(final int rowIndex, final int columnIndex) {
    return data[rowIndex][columnIndex];
  }
}
}

0 个答案:

没有答案