Example usage for javax.swing.table DefaultTableCellRenderer DefaultTableCellRenderer

List of usage examples for javax.swing.table DefaultTableCellRenderer DefaultTableCellRenderer

Introduction

In this page you can find the example usage for javax.swing.table DefaultTableCellRenderer DefaultTableCellRenderer.

Prototype

public DefaultTableCellRenderer() 

Source Link

Document

Creates a default table cell renderer.

Usage

From source file:hermes.browser.components.WatchTable.java

public WatchTable(QueueWatchTableModel model, final boolean showAge) {
    super(model);

    this.watchModel = model;

    DefaultTableCellRenderer dateRenderer = new DefaultTableCellRenderer() {
        /**//from w  w  w  .  ja v  a 2  s.  c o m
        * 
        */
        private static final long serialVersionUID = 2791021835238102467L;
        private FastDateFormat formatter = FastDateFormat.getInstance("EEE, d MMM yyyy HH:mm:ss");

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if (value == null) {
                setText("");
            } else if (value instanceof Date) {

                if (showAge) {
                    super.setText(TextUtils.getAge((Date) value));
                } else {
                    String strDate = formatter.format((Date) value);

                    super.setText(strDate);

                }

            }
            return this;
        }

        protected void setValueXXX(Object value) {
            if (value == null) {
                super.setText("");

            } else if (showAge) {
                super.setText(TextUtils.getAge((Date) value));
            } else {
                super.setText(value.toString());

            }
        }
    };

    setDefaultRenderer(Date.class, dateRenderer);

}

From source file:MainClass.java

public MainClass() {
    super("Table With DefaultCellEditor Example");
    setSize(500, 300);/*from  w  w  w . j  a  v a 2 s  .c  o m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTable table = new JTable(new AbstractTableModel() {
        ColorName data[] = { colors[0], colors[1], colors[2], colors[3], colors[4], colors[0], colors[1],
                colors[2], colors[3], colors[4] };

        public int getColumnCount() {
            return 3;
        }

        public int getRowCount() {
            return 10;
        }

        public Object getValueAt(int r, int c) {
            switch (c) {
            case 0:
                return (r + 1) + ".";
            case 1:
                return "Some pithy quote #" + r;
            case 2:
                return data[r];
            }
            return "Bad Column";
        }

        public Class getColumnClass(int c) {
            if (c == 2)
                return ColorName.class;
            return String.class;
        }

        public boolean isCellEditable(int r, int c) {
            return c == 2;
        }

        public void setValueAt(Object value, int r, int c) {
            data[r] = (ColorName) value;
        }
    });

    table.setDefaultEditor(ColorName.class, new DefaultCellEditor(new JComboBox(colors)));
    table.setDefaultRenderer(ColorName.class, new DefaultTableCellRenderer());
    table.setRowHeight(20);
    getContentPane().add(new JScrollPane(table));
}

From source file:com.puzzle.gui.MainFrame.java

/**
 * table?/*from   w w  w.  j  a  v  a2s .  co m*/
 *
 * @param table
 */
public static void makeJtableFace(javax.swing.JTable table) {

    try {
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
            public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                if (row % 2 == 0) {
                    setBackground(java.awt.Color.white); //
                } else if (row % 2 == 1) {
                    setBackground(new java.awt.Color(206, 231, 255)); //?
                }
                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            }
        };
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:PropertyTable.java

/**
 * This constructor method specifies what data the table will display (the
 * table model) and uses the TableColumnModel to customize the way that the
 * table displays it. The hard work is done by the TableModel implementation
 * below./*from ww  w. j  a  va  2  s. co  m*/
 */
public PropertyTable(Class beanClass) {
    // Set the data model for this table
    try {
        setModel(new JavaBeanPropertyTableModel(beanClass));
    } catch (IntrospectionException e) {
        System.err.println("WARNING: can't introspect: " + beanClass);
    }

    // Tweak the appearance of the table by manipulating its column model
    TableColumnModel colmodel = getColumnModel();

    // Set column widths
    colmodel.getColumn(0).setPreferredWidth(125);
    colmodel.getColumn(1).setPreferredWidth(200);
    colmodel.getColumn(2).setPreferredWidth(75);
    colmodel.getColumn(3).setPreferredWidth(50);

    // Right justify the text in the first column
    TableColumn namecol = colmodel.getColumn(0);
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(SwingConstants.RIGHT);
    namecol.setCellRenderer(renderer);
}

From source file:de.codesourcery.eve.skills.ui.components.impl.planning.ResourceStatusComponent.java

@Override
protected JPanel createPanel() {
    final JPanel result = new JPanel();

    table.setFillsViewportHeight(true);//from  w w  w .  j  a  v a2  s.c  om

    table.setRowSorter(model.getRowSorter());

    FixedBooleanTableCellRenderer.attach(table);

    table.setDefaultRenderer(Integer.class, new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if (!(value instanceof Integer)) {
                return this;
            }

            if (column == 0) {
                setHorizontalAlignment(SwingConstants.LEFT);
            } else {
                setHorizontalAlignment(SwingConstants.RIGHT);
            }

            Integer amount = (Integer) value;
            if (amount.intValue() < 0) {
                if (!isSelected) {
                    setBackground(Color.RED);
                } else {
                    setBackground(table.getSelectionBackground());
                }
            } else {
                setBackground(table.getBackground());
            }
            return this;
        }
    });

    addToShoppingListButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final List<ItemWithQuantity> items = model.getSelectedItems();

            if (items.isEmpty()) {
                return;
            }

            final ShoppingListEditorComponent comp = new ShoppingListEditorComponent(title, "", items);

            comp.setModal(true);
            ComponentWrapper.wrapComponent(comp).setVisible(true);
            if (!comp.wasCancelled() && !comp.getShoppingList().isEmpty()) {
                shoppingListManager.addShoppingList(comp.getShoppingList());
                getComponentCallback().dispose(ResourceStatusComponent.this);
            }

        }
    });

    new GridLayoutBuilder()
            .add(new GridLayoutBuilder.VerticalGroup(new GridLayoutBuilder.Cell(new JScrollPane(table)),
                    new GridLayoutBuilder.FixedCell(addToShoppingListButton)))
            .addTo(result);
    return result;
}

From source file:OAT.ui.util.UiUtil.java

public static void setupColumns(JTable table) {
    for (int i = 0; i < table.getColumnCount(); i++) {
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        TableColumn column = table.getColumnModel().getColumn(i);
        Class columnClass = table.getColumnClass(i);
        String columnName = table.getColumnName(i);
        renderer.setToolTipText(columnName);

        if (columnClass.getSuperclass().equals(Number.class)) {
            renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
        }/*from ww  w .  java 2s  .co m*/

        if (table.isEnabled()) {
            if (columnName.equals("Currency")) {
                //addComboCell(column, Product.CURRENCIES);
            } else if (columnName.equals("Holidays")) {
                //column.setCellEditor(new FileChooserCellEditor());
            }
        }

        column.setCellRenderer(renderer);
    }
}

From source file:se.backede.jeconomix.forms.report.TransactionReport.java

public void setTableData(List<TransactionReportDto> calculatedReport) {
    DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
    rightRenderer.setHorizontalAlignment(JLabel.RIGHT);

    TransactionReportModel model = new TransactionReportModel(calculatedReport);
    reportTable.setModel(model);/*ww w . j  a v a  2  s  .  c o  m*/

    for (int i = 1; i < 14; i++) {
        reportTable.getColumnModel().getColumn(i).setCellRenderer(rightRenderer);
    }

}

From source file:org.agmip.ui.afsirs.frames.SWFrame.java

public SWFrame(JFrame prev) {
    initComponents();/*w ww  . j  a  v a  2s.  c  o  m*/
    this.prev = prev;
    setLocation(400, 50);
    utils = AFSIRSUtils.getInstance();
    ir = utils.getIrrigationSystem();
    DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    centerRenderer.setHorizontalAlignment(JLabel.CENTER);
    soilTable.setDefaultRenderer(Double.class, centerRenderer);
    soilTable.setDefaultRenderer(Integer.class, centerRenderer);
    infoButton.setForeground(Color.blue);
    infoButton.setBorder(LineBorder.createGrayLineBorder());

    JTableHeader tHeader = soilTable.getTableHeader();
    tHeader.setPreferredSize(new Dimension(300, 35));

    wcErrorLabel.setVisible(false);
    errorLabel.setVisible(false);

    init();

    if (AFSIRSUtils.defaultMode) {
        dwtText.setText(60.0 + "");
        soilListBox.setSelectedIndex(5);
        waterholdcapacityBox.setSelectedIndex(waterHoldSelectedItemIndex[0]);
    }

    getRootPane().setDefaultButton(nextButton);
}

From source file:com.iucosoft.eavertizare.gui.MainJFrame.java

private void initModes() {
    jListFirma.setModel(firmeListModel);
    jListFirma.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jListFirma.addMouseListener(mouseListenerList);

    jTableClients.setModel(clientiTableModel);
    jTableClients.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    //  jTableClients.addMouseListener(mouseListenerTable);
    jTableClients.getColumnModel().getColumn(0).setPreferredWidth(27);
    jTableClients.getColumnModel().getColumn(7).setPreferredWidth(1);

    jTableClients.getColumnModel().getColumn(7).setCellRenderer(new MyImageRenderer());
    jTableClients.setBackground(new Color(220, 220, 220));
    jListFirma.setBackground(new Color(220, 220, 220));

    DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    centerRenderer.setHorizontalAlignment(JLabel.CENTER);
    jTableClients.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
    jTableClients.getColumnModel().getColumn(5).setCellRenderer(centerRenderer);
    refreshFrame();/*ww w . j  av a2 s .  c  o m*/

}

From source file:net.sf.profiler4j.console.ClassListPanel.java

/**
 * This method initializes classesTable//from w  w  w  .  j a  v a  2  s  .  c o m
 * 
 * @return javax.swing.JTable
 */
private JTable getClassesTable() {
    if (classesTable == null) {
        classesTable = new JTable();
        classesTable.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        classesTable.setFont(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 12));
        classesTable.setModel(getClassListTableModel());
        classesTable.setRowHeight(20);
        classesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (classesTable.getSelectedRowCount() > 0) {
                    addAsRuleButton.setEnabled(true);
                } else {
                    addAsRuleButton.setEnabled(false);
                }

            }
        });
        TableColumn c = classesTable.getColumnModel().getColumn(0);
        c.setMinWidth(50);
        c.setMaxWidth(50);

        c = classesTable.getColumnModel().getColumn(1);
        c.setMinWidth(300);
        c.setCellRenderer(new DefaultTableCellRenderer() {
            Font f1 = new Font("Tahoma", java.awt.Font.PLAIN, 12);
            Font f2 = new Font("Tahoma", java.awt.Font.BOLD, 12);

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                if (isSelected) {
                    setFont(f2);
                } else {
                    setFont(f1);
                }
                if (classListTableModel.getRow(row).info.isInstrumented()) {
                    if (isSelected) {
                        setForeground(Color.YELLOW);
                        setBackground(Color.BLUE);
                    } else {
                        setBackground(Color.decode("#bbffbb"));
                        setForeground(Color.BLACK);
                    }
                } else {
                    if (isSelected) {
                        setForeground(Color.WHITE);
                        setBackground(Color.BLUE);
                    } else {
                        setBackground(Color.WHITE);
                        setForeground(Color.BLACK);
                    }
                }
                setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
                return this;
            }
        });

    }
    return classesTable;
}