Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2004

Tip: Looking for answers? Try searching our database.

Unable to access data in TableModel??

Thread view: 
Piet - 31 Oct 2004 14:19 GMT
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;
    }
}
Andrei Kouznetsov - 31 Oct 2004 15:43 GMT
> 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
[quoted text clipped - 13 lines]
> data" but my TableModel extension defines an instance field "data". So
> why can?t this variable be found?

just cast model to XmlTableModel...

Signature

Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Piet - 01 Nov 2004 00:13 GMT
> just cast model to XmlTableModel...
Thanks a lot! That was the hint I needed.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.