--- FM2SQL/Attic/TableComponent.java 2004/03/23 11:57:06 1.5 +++ FM2SQL/Attic/TableComponent.java 2004/05/26 11:37:32 1.6 @@ -41,6 +41,7 @@ public class TableComponent extends JPan int selIndex = 0; Icon trueIcon = null, falseIcon = null, empty = null; TableComponent tableComp = this; + BooleanRenderer booleanRenderer = new BooleanRenderer(); /** * Constructs an empty table with default header and dummy data */ @@ -165,7 +166,7 @@ public class TableComponent extends JPan { tableComp.row = row; boolean value = false; - if (getColumnClass(column) == ArrayList.class || getColumnClass(column) == JComboBox.class || getColumnClass(column) == SQLCommand.class || getColumnClass(column) == IDComboBox.class) + if (getColumnClass(column) == ArrayList.class || getColumnClass(column) == JComboBox.class || getColumnClass(column) == SQLCommand.class || getColumnClass(column) == IDComboBox.class|| getColumnClass(column) == Boolean.class) value = true; //System.out.println("tried to edit at row "+row+" column "+column+" "+value); @@ -209,41 +210,7 @@ public class TableComponent extends JPan });*/ table.setDefaultEditor(SQLCommand.class, new SQLCommandEditor()); - table.setDefaultRenderer(Boolean.class, new TableCellRenderer() - { - JCheckBox checkBox = new JCheckBox(); - JLabel label = new JLabel(); - public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) - { - if (value instanceof Integer) - { - label.setText(value.toString()); - label.setHorizontalAlignment(JLabel.RIGHT); - - return label; - } else if (value instanceof String) - { - label.setText(value.toString()); - label.setHorizontalAlignment(JLabel.CENTER); - return label; - } - checkBox.setHorizontalAlignment(JLabel.CENTER); - checkBox.setVerticalAlignment(JLabel.CENTER); - - checkBox.setSelectedIcon(trueIcon); - if (value == null) - checkBox.setIcon(empty); - else - checkBox.setIcon(falseIcon); - checkBox.setForeground(table.getForeground()); - checkBox.setBackground(table.getBackground()); - - checkBox.setSelected((value != null && ((Boolean) value).booleanValue())); - return checkBox; - - } - - }); + table.setDefaultRenderer(Boolean.class, booleanRenderer); table.setRowHeight(20); @@ -330,6 +297,10 @@ public class TableComponent extends JPan for (int i = 0; i < model.getColumnCount(); ++i) { label = new JLabel(model.getColumn(i).getHeaderValue().toString()); + //System.out.println("labelWith "+label.getPreferredSize().width); + if(label.getPreferredSize().width==0) + model.getColumn(i).setPreferredWidth(20); + else model.getColumn(i).setPreferredWidth(label.getPreferredSize().width + 50); } @@ -1098,6 +1069,94 @@ public class TableComponent extends JPan } return alterMe; } + public static class BooleanRenderer implements TableCellRenderer + { + Icon trueIcon,falseIcon,empty; + // renders false state with empty icon + boolean renderFalseEmpty=false; + public BooleanRenderer(){ + try + { + trueIcon = new ImageIcon(getClass().getResource("icons/ok.gif")); + + falseIcon = new Icon() + { + public int getIconHeight() + { + return 23; + } + public int getIconWidth() + { + return 27; + } + public void paintIcon(Component c, Graphics g, int x, int y) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.blue); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2.setStroke(new BasicStroke(2.0f)); + int xr = x + 20, yr = y + 15; + int xl = x + 5, yl = y + 5; + g2.drawLine(xl, yl, xr, yr); + g2.drawLine(xl, yr, xr, yl); + + } + }; + empty = new Icon() + { + public int getIconHeight() + { + return 23; + } + public int getIconWidth() + { + return 27; + } + public void paintIcon(Component c, Graphics g, int x, int y) + { + + } + }; + + } catch (Exception e) + { + System.out.println("Error while loading ok.gif"); + } + + + } + JCheckBox checkBox = new JCheckBox(); + JLabel label = new JLabel(); + + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) + { + if (value instanceof Integer) + { + label.setText(value.toString()); + label.setHorizontalAlignment(JLabel.RIGHT); + + return label; + } else if (value instanceof String) + { + label.setText(value.toString()); + label.setHorizontalAlignment(JLabel.CENTER); + return label; + } + checkBox.setHorizontalAlignment(JLabel.CENTER); + checkBox.setVerticalAlignment(JLabel.CENTER); + + checkBox.setSelectedIcon(trueIcon); + if(renderFalseEmpty|| value==null) + checkBox.setIcon(empty); + else checkBox.setIcon(falseIcon); + checkBox.setForeground(table.getForeground()); + checkBox.setBackground(table.getBackground()); + + checkBox.setSelected((value != null && ((Boolean) value).booleanValue())); + return checkBox; + + } +} public static class SQLCommand { String command = "";