7 Replies - 1965 Views - Last Post: 28 November 2013 - 03:52 AM Rate Topic: -----

#1 NewToJava2011   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Populate jTable during runtime from an ActionPerformed Event

Posted 27 November 2013 - 11:13 AM

How do I populate a jTable during runtime after an action performed event?

     private void importXMLButtonActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            xmlUtil.xmlToObject();
        } catch (JAXBException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        for (JpegMetadata jpegMetadata : xmlUtil.getList()) { 
            /*
             * Populate jTable from here
             */      
        }
        
        

    }/code]



[code]

package components;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;

public class TableDemo extends JPanel {
    private boolean DEBUG = false;

    public TableDemo() {
        super(new GridLayout(1,0));

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"First Name",
                                        "Last Name",
                                        "Sport",
                                        "# of Years",
                                        "Vegetarian"};

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }

            data[row][col] = value;
            fireTableCellUpdated(row, col);

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Populate jTable during runtime from an ActionPerformed Event

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,238
  • Joined: 20-September 08

Re: Populate jTable during runtime from an ActionPerformed Event

Posted 27 November 2013 - 11:36 AM

Quote

xmlUtil.xmlToObject();
What is that doing? Is it really a void method?
Was This Post Helpful? 0
  • +
  • -

#3 NewToJava2011   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Re: Populate jTable during runtime from an ActionPerformed Event

Posted 27 November 2013 - 11:46 AM

View Postg00se, on 27 November 2013 - 11:36 AM, said:

Quote

xmlUtil.xmlToObject();
What is that doing? Is it really a void method?


I'm hoping it returns list...

    public ArrayList<JpegMetadata> xmlToObject() throws JAXBException, FileNotFoundException {
        // get variables from our xml file, created before
        JAXBContext context = JAXBContext.newInstance(JpegAlbum.class);
        System.out.println();
        System.out.println("Output from our XML File: ");
        Unmarshaller um = context.createUnmarshaller();
        jpegAlbum = (JpegAlbum) um.unmarshal(new FileReader(DOC_XML));
        
        list = jpegAlbum.getJpegList();
        return list; 
    }

    public ArrayList<JpegMetadata> getList() {
        return list;
    }


Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,238
  • Joined: 20-September 08

Re: Populate jTable during runtime from an ActionPerformed Event

Posted 27 November 2013 - 12:07 PM

It might do, but you're not using it :P You're treating it as a void method

The next question is: what is JpegMetadata?
Was This Post Helpful? 0
  • +
  • -

#5 NewToJava2011   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Re: Populate jTable during runtime from an ActionPerformed Event

Posted 27 November 2013 - 02:57 PM

View Postg00se, on 27 November 2013 - 12:07 PM, said:

It might do, but you're not using it :P/>/> You're treating it as a void method

The next question is: what is JpegMetadata?


OK. So I've amended my code.

Exif.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Exif {

    private int imgId;
    private Date imgTimesatmp;
    private String imgFilename;
    private Double imgLatitude;
    private Double imgLongitude;

    public  Exif() {
    }

    public String getImgFilename() {
        return imgFilename;
    }

    public void setImgFilename(String imgFilename) {
        this.imgFilename = imgFilename;
    }

    public int getImgId() {
        return imgId;
    }

    @XmlAttribute
    public void setImgId(int imgId) {
        this.imgId = imgId;
    }

    public Double getImgLatitude() {
        return imgLatitude;
    }

    @XmlElement
    public void setImgLatitude(Double imgLatitude) {
        this.imgLatitude = imgLatitude;
    }

    public Double getImgLongitude() {
        return imgLongitude;
    }

    @XmlElement
    public void setImgLongitude(Double imgLongitude) {
        this.imgLongitude = imgLongitude;
    }

    public Date getImgTimesatmp() {
        return imgTimesatmp;
    }

    @XmlElement
    public void setImgTimesatmp(Date imgTimesatmp) {
        this.imgTimesatmp = imgTimesatmp;
    }

    @Override
    public String toString() {
        return "Exif{" + "imgId=" + imgId + ", imgTimesatmp=" + imgTimesatmp + ", imgFilename=" + imgFilename + ", imgLatitude=" + imgLatitude + ", imgLongitude=" + imgLongitude + '}';
    }
  
}



TableDemo.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

/*
 * TableDemo.java requires no other files.
 */
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/** 
 * TableDemo is just like SimpleTableDemo, except that it
 * uses a custom TableModel.
 */
public class TableDemo extends JPanel {
    private JTable table;

    public TableDemo() {
        super(new GridLayout(1, 0));

        table = new JTable(new ExifTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);

        addDataToTable();
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private void addDataToTable() {
        try {

            File file = new File("/path/myDoc.xmll");
            JAXBContext jaxbContext = JAXBContext.newInstance(Exif.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Exif exif = (Exif) jaxbUnmarshaller.unmarshal(file);
            System.out.println(exif);
            

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}


ExifTableModel

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import javax.swing.table.AbstractTableModel;

public class ExifTableModel extends AbstractTableModel {

    private String[] columnNames = {"Image Id",
        "Timestamp",
        "Filename",
        "Latitude",
        "Longitude"};
    private Object[][] data = {
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null}
    };

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
//        @Override
//        public Class getColumnClass(int c) {
//            return getValueAt(0, c).getClass();
//        }
    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    @Override
    public void setValueAt(Object value, int row, int col) {
        System.out.println("Setting value at " + row + "," + col
                + " to " + value
                + " (an instance of "
                + value.getClass() + ")");

        data[row][col] = value;
        fireTableCellUpdated(row, col);

    }
}



The idea is that I'd like to click a jButton, read the XML file and populate a jTable.

This post has been edited by NewToJava2011: 27 November 2013 - 02:58 PM

Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,238
  • Joined: 20-September 08

Re: Populate jTable during runtime from an ActionPerformed Event

Posted 27 November 2013 - 04:30 PM

Well - you didn't answer my question ...
Was This Post Helpful? 0
  • +
  • -

#7 NewToJava2011   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Re: Populate jTable during runtime from an ActionPerformed Event

Posted 28 November 2013 - 12:36 AM

View Postg00se, on 27 November 2013 - 04:30 PM, said:

Well - you didn't answer my question ...


JpegMetadata has been removed. It is replaced with Exif.java. Most of the original code is redundant.
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,238
  • Joined: 20-September 08

Re: Populate jTable during runtime from an ActionPerformed Event

Posted 28 November 2013 - 03:52 AM

Quote

Exif exif = (Exif) jaxbUnmarshaller.unmarshal(file);
That's just returning one entity - why is that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1