Вот: Кстати вместо Icon можно использовать Border, заглушки методов для этого в коде есть. | Код | import java.awt.*; import javax.swing.*; import javax.swing.border.Border; import javax.swing.table.*;
public class Main extends DefaultTableCellRenderer implements Icon, Border { String green; String red; /** Creates a new instance of Main */ public Main () { setIcon (this); } /** * @param args the command line arguments */ public static void main (String[] args) { String[][] data = { {"A1-B1", "A1-B2"},{"A2-B1","A2-B2"}}; String[] col = {"C1","C2"}; JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Main main = new Main(); JTable table = new JTable(data, col); table.setDefaultRenderer (Object.class, main); frame.setContentPane (new JScrollPane(table)); frame.pack (); frame.setVisible (true); }
public void paintIcon (Component c, Graphics g, int x, int y) { g.setColor (getBackground ()); g.fillRect (x,y, getIconWidth (), getIconHeight ()); Font font = getFont (); g.setFont (font); FontMetrics fm = g.getFontMetrics (font); int ascent = fm.getMaxAscent (); g.setColor (Color.GREEN); g.drawString (green, x, y + ascent); g.setColor (Color.RED); g.drawString (red, x + getIconWidth () - (int)fm.getStringBounds (red,g).getWidth (), y + ascent); }
public int getIconWidth () { return getWidth (); }
public int getIconHeight () { return getHeight (); }
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column); String text = getText(); green = text.substring (0, 2); red = text.substring (3); setText(null); //setBorder (this); return this; }
public void paintBorder (Component c, Graphics g, int x, int y, int width, int height) { }
public Insets getBorderInsets (Component c) { return new Insets(0,0,0,0); }
public boolean isBorderOpaque () { return true; } }
|
|