Unable to access data in TableModel??

From: Piet (pit.grinja_at_gmx.de)
Date: 10/31/04

  • Next message: Chris Rohr: "Re: GridBagConstraints anchor dilemma"
    Date: 31 Oct 2004 05:19:54 -0800
    
    

    Hello,
    I have made an app that takes an xml node and display its content in a
    JTable. THe node is handed to a converter class which reads the
    information and generates the ArrayList which contains the column
    labels and the ArrayList of ArrayLists that contains the data. These
    two objects are then handed to a TableModel which is used to construct
    the JTable. In principle, that works fine. I now want to add a table
    cell renderer that "grays out" those cells for which the table model
    doesnīt contain any data. For that purpose, I wanted to access the
    data of the model in the TableCellRenderer via
    public Component getTableCellRendererComponent(JTable table,Object
    value,
       boolean isSelected,boolean hasFocus,int rowNum, int colNum) {
            ArrayList tableRow = table.getModel().data.get(rowNum);
    }
    However, the compiler complains about "cannot resolve symbol variable
    data" but my TableModel extension defines an instance field "data". So
    why canīt this variable be found? Below is an outline of the program
    which shows how the different classes work together. Thanks in advance
    for any help. Piet
    public class XmlTableViewer extends JFrame{
            JTable IJTable;
            JScrollPane TablePane;
            XmlTableViewer(String title){
                super(title);
            //...creation of empty table...
            //...etc...
            }
            public void OnOpen(){//called by menu selection
                    //selecting XML file, parsing, getting the root node
                    Node root = IDocument.getDocumentElement();
                    DomListListConverter iDC = new DomListListConverter(root);
                    this.IJTable = new JTable();
                    this.IJTable.setModel(new XmlTableModel(iDC.headers, iDC.data));
                    this.IJTable.setAutoResizeMode(5);
                    this.IJTable.setDefaultRenderer(Object.class,new
    XmlTableCellRenderer());
                    JViewport vp = XmlTableViewer.this.TablePane.getViewport();
                    vp.remove(vp.getView());
                    vp.add(IJTable);
            this.getContentPane().add(TablePane);
                   this.repaint();
            }
            public Document openXmlFile(){
                    try {
                            JFileChooser dlg = new JFileChooser();
                            if (dlg.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                                    return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(dlg.getSelectedFile().getPath());
                            }
                    }
                    catch (Exception e){
                            return null;
                    };
                    return null;
            }
            public static void main(String[] args){
                    XmlTableViewer IXmlTableViewer = new XmlTableViewer("Xml table
    demo");
                    IXmlTableViewer.pack();
                    IXmlTableViewer.show();
            }
    }
    class DomListListConverter{
            ArrayList headers = new ArrayList();
            ArrayList data = new ArrayList();
            DomListListConverter(Node knoten){
                    this.headers.add(knoten.getNodeName());
                    this.headers.add("=");
                    //...more code...
    }
    class XmlTableModel extends AbstractTableModel{
            public ArrayList headers;
            public ArrayList data;
            public XmlTableModel(ArrayList headers, ArrayList data){
                    this.headers = headers;
                    this.data = data;
            //...more code...
            }
    }
    class XmlTableCellRenderer implements TableCellRenderer{
            public Component getTableCellRendererComponent(JTable table,Object
    value,boolean isSelected,boolean hasFocus,int rowNum, int colNum) {
            ArrayList tableRow = table.getModel().data.get(rowNum);
            JLabel label = new JLabel((String)value);
            label.setOpaque(true);
            return label;
            }
    }


  • Next message: Chris Rohr: "Re: GridBagConstraints anchor dilemma"