Turn on thread page Beta
    • Thread Starter
    Offline

    0
    ReputationRep:
    Im currently updating a program I wrote a while back and want to use a JTable instead of a list box. Basically.. I need it to display search results (searching through a database).

    In short, my question is... how do you add and delete rows from a JTable that has already been created. if i knew how to do this I could sort my main problem out quite easily.

    Would be happy to supply any additional information needed.

    Thanks =]
    Offline

    14
    ReputationRep:
    I am guessing you are using Joomla when you say JTable. Yes?
    • Thread Starter
    Offline

    0
    ReputationRep:
    No, Im using Netbeans.
    I dont even know what Joomla is.

    Its Java not Javascript. I probably should have said what language I was using :P
    Offline

    14
    ReputationRep:
    He He. Joomla is a PHP CMS. The prefix all their class names with a J. Sorry
    • Thread Starter
    Offline

    0
    ReputationRep:
    Dont worry =]
    Doesnt look like anyone can help me anyway

    time to go sifting through all those tutorials and API and try and mak sense again
    Offline

    1
    ReputationRep:
    Don't know if you ever got this sorted.

    Lots of Swing components such as JLists or JTables are backed by Model objects for storing their data.

    JTable has a getModel() method which returns an instance of the TableModel interface. Provided you haven't used the setModel() method to give the JTable a custom TableModel, you can cast this to a DefaultTableModel instance.

    DefaultTableModel has a addRow method which takes an array of Objects.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Baron)
    Don't know if you ever got this sorted.

    Lots of Swing components such as JLists or JTables are backed by Model objects for storing their data.

    JTable has a getModel() method which returns an instance of the TableModel interface. Provided you haven't used the setModel() method to give the JTable a custom TableModel, you can cast this to a DefaultTableModel instance.

    DefaultTableModel has a addRow method which takes an array of Objects.
    Ive tried this.
    I havnt used the setModel() method i don't think
    (unless mainTable = new JTable(data, columnNames); ) uses the setModel() method.

    But when trying to cast the JTable.getModel() to DefaultTableModel I get an error
    (Exception in thread "main" java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableMo del)

    Any Idea what the problem is..

    P.S. Thanks for the help =]
    • Thread Starter
    Offline

    0
    ReputationRep:
    I solved this in the end
    the easiest way I have found to do it
    is when creating a table do as so..

    Code:
    tabelParts.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
    
        },
        new String [] {
            "Name", "Machine", "Part Number", "Min. Stock", "Act. Stock"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false, false, false
        };
    
        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }
    
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });


    I found this using Netbeans built in GUI editor,
    this code is usable to set the model to the default model so you can
    then use the addRow() method.

    yay
    Offline

    14
    ReputationRep:
    I'd just like to say, thanks for (a) posting the solution and (b) changing the thread title to include [Solved]. That's perfect - it stops people wasting time coming in here to answer the question, and also allows other people to use the search facility to find the solution if they have the same problem. Other posters take note.
 
 
 
Reply
Submit reply
Turn on thread page Beta
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: November 25, 2008
Poll
How are you feeling in the run-up to Results Day 2018?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.