Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 package org.apache.cayenne.swing;
 import java.util.List;
 import java.util.Map;
A binding for a JTable.

Andrei Adamchik
 public class TableBinding extends BindingBase {

A variable exposed in the context of set/get cell value.
     public static final String ITEM_VAR = "item";
     protected JTable table;
     protected String[] headers;
     protected BindingExpression[] columns;
     protected boolean[] editableState;
     protected Class[] columnClass;
     protected List list;
     public TableBinding(JTable tableString listBindingString[] headers,
             BindingExpression[] columnsClass[] columnClassboolean[] editableState,
             Object[] sampleLongValues) {
         this. = table;
         this. = headers;
         this. = columns;
         this. = editableState;
         this. = columnClass;
         table.setModel(new BoundTableModel());
     protected void resizeColumns(Object[] sampleLongValues) {
         TableCellRenderer headerRenderer = .getTableHeader().getDefaultRenderer();
         TableColumnModel columnModel = .getColumnModel();
         TableModel tableModel = .getModel();
         for (int i = 0; i < columnModel.getColumnCount(); i++) {
             TableColumn column = columnModel.getColumn(i);
             Component header = headerRenderer.getTableCellRendererComponent(nullcolumn
                     .getHeaderValue(), falsefalse, 0, 0);
             int headerWidth = header.getPreferredSize().;
             if (sampleLongValues[i] != null) {
                 Component bigCell = 
                 int cellWidth = bigCell.getPreferredSize().;
            else {
    public void setContext(Object object) {
        this. = updateList();
    public Component getView() {
        return ;
    public void updateView() {
        this. = updateList();
    int getListSize() {
        return ( != null) ? .size() : 0;
    List updateList() {
        if (getContext() == null) {
            return null;
        Object list = getValue();
        if (list == null) {
            return null;
        if (list instanceof List) {
            return (Listlist;
        if (list instanceof Object[]) {
            Object[] objects = (Object[]) list;
            return Arrays.asList(objects);
        if (list instanceof Collection) {
            return new ArrayList((Collectionlist);
        throw new BindingException("List expected, got - " + list);
    final class BoundTableModel extends AbstractTableModel {
        // this map is used as "flyweight", providing on the spot context for Ognl
        // expression evaluation
        Map listContext = new SingletonMap(null);
        public int getColumnCount() {
            return .;
        public int getRowCount() {
            return getListSize();
        public boolean isCellEditable(int rowIndexint columnIndex) {
            return [columnIndex];
        public Object getValueAt(int rowIndexint columnIndex) {
            Object item = .get(rowIndex);
            return [columnIndex].getValue(getContext(), );
        public String getColumnName(int column) {
            // per CAY-513 - if an empty string is passed for header, table header will
            // have zero height on Windows... So we have to check for this condition
            return Util.isEmptyString([column]) ? " " : [column];
        public Class getColumnClass(int columnIndex) {
            return [columnIndex];
        public void setValueAt(Object valueint rowIndexint columnIndex) {
            Object item = .get(rowIndex);
            [columnIndex].setValue(getContext(), value);
New to GrepCode? Check out our FAQ X