Results 1 to 6 of 6
  1. #1
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Question Retrieve value from JTable

    In the picture You see that when I select the table cell (any) and click button, I'm returning the objects of the wrong Class type (wrong for what am I trying to do). You see that cell 4 returns string, and cell 5 time stamp. These are the data types of the properties of the underlying object (Dete). Can I, and how, return the value of he object, from which properties these table cells are formed?
    Thank You
    Attached Thumbnails Attached Thumbnails Retrieve value from JTable-1.jpg  

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

  3. #3
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Arrow Explanation ...

    Quote Originally Posted by KevinWorkman View Post
    Thank You for your answer and recommended reading.

    I'll try to make things clearer.

    On the fig. no. 2 You see the same example, but using JList object, this time, to display data.
    You see that when I click the button, the object returned from the list is the correct class type (Dete).
    The list on this example uses Vector full of objects with Dete class type.

    I hope this clarifies things a little bit more.

    Thank You.
    Attached Thumbnails Attached Thumbnails Retrieve value from JTable-2.jpg  
    Last edited by mcdhappy80; 11-30-2010 at 05:00 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mcdhappy80 View Post
    ....
    I hope this clarifies things a little bit more.

    Thank You.
    Not really -- so we're supposed to guess what's wrong with your code without seeing any code? Seriously?

    Please read the 2nd link in my signature links below.

  5. #5
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Arrow Classes

    Quote Originally Posted by Fubarable View Post
    Not really -- so we're supposed to guess what's wrong with your code without seeing any code? Seriously?
    Of course not.

    On the fig. no. 3 I present classes and packages my application has.

    The main class looks like this:

    Java Code:
    package Main;
    
    import Interfejs.*;
    import Logika.*;
    
    public class Main {
    
            public static void main(String args[]){
    
                //ListaCekanja lc = new ListaCekanja();
    
                GenericDataAccess da = new GenericDataAccess();
                new Forma_PregledListeCekanjaTabela(da.getModelTabeleListaCekanja()).setVisible(true); // JTable example - the one that doesn't work the way I want it to
                //new Forma_PregledListeCekanja(lc).setVisible(true); // if you wanna display data in List use this line of code instead two of the above and uncomment ListaCekanja line of code above
         
            }
    }
    GenericDataAccess class:

    Java Code:
    package Logika;
    
    import java.sql.*;
    import javax.swing.table.DefaultTableModel;
    
    public class GenericDataAccess {
    
        static String imeKlase = "sun.jdbc.odbc.JdbcOdbcDriver";
        static Connection con = null;
        static ResultSet rs = null;
        static Statement stmt = null;
        static String url = "jdbc:odbc:Driver={Microsoft Access Driver "
                + "(*.mdb, *.accdb)};DBQ=C:\\Program Files\\MCDesign\\DULane\\";
        //static String appPut = "Pedagog\\ObradaDece\\ObradaDeceData.accdb";
        //private DefaultTableModel tm = new DefaultTableModel();
    
      
        public GenericDataAccess() {
        }
    
        public static ResultSet getResultSet(String sql, String app) {
    
            try {
                Class.forName(imeKlase);
                con = DriverManager.getConnection(url + app);
    
                stmt = con.createStatement();
                rs = stmt.executeQuery(sql);
    
                System.out.println("Connected!");
                //rs.close();
                //stmt.close();
                //con.close();
    
            } catch (SQLException e) {
                System.out.println("SQL Exception: " + e.toString());
            } catch (ClassNotFoundException e) {
                e.getStackTrace();
            }
            return rs;
        }
    
        public static void zatvoriKonekciju() {
            try {
                rs.close();
                stmt.close();
                con.close();
            } catch (Exception e) {
            }
    
        }
    
        public DefaultTableModel getModelTabeleListaCekanja(){
    
            DefaultTableModel modelTabele = new DefaultTableModel();
    
            String SQL = "SELECT txtMBDeteta AS JMBG, txtPrezime AS Prezime, txtSrednjeIme As Srednje_Ime, " +
                    "txtIme AS Ime, dteDatumRodjenja AS Datum_Rodjenja " +
                    "FROM tblDete0 " +
                    "ORDER BY txtPrezime, txtSrednjeIme, txtIme ASC";
    
            rs = GenericDataAccess.getResultSet(SQL,"DULaneDB\\DULaneDB.accdb");
    
            modelTabele = (DefaultTableModel)DbUtils.resultSetToTableModel(rs);
    
            GenericDataAccess.zatvoriKonekciju();
    
            return modelTabele;
        }
    
    }
    DBUtils class:
    Java Code:
    package Logika;
    
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    
    import java.util.Vector;
    
    import javax.swing.table.DefaultTableModel;
    
    
    public class DbUtils {
    
    
        public DbUtils(){
        }
    
        public static DefaultTableModel resultSetToTableModel(ResultSet rs) {
            try {
                ResultSetMetaData metaData = rs.getMetaData();
                int numberOfColumns = metaData.getColumnCount();
                Vector columnNames = new Vector();
    
                // Get the column names
                for (int column = 0; column < numberOfColumns; column++) {
                    columnNames.addElement(metaData.getColumnLabel(column + 1));
                }
    
                // Get all rows.
                Vector rows = new Vector();
    
                while (rs.next()) {
                    Vector newRow = new Vector();
    
                    for (int i = 1; i <= numberOfColumns; i++) {
                        newRow.addElement(rs.getObject(i));
                    }
    
                    rows.addElement(newRow);
                }
    
                return new DefaultTableModel(rows, columnNames);
            } catch (Exception e) {
                e.printStackTrace();
    
                return null;
            }
        }
    
        
    }
    Forma_PregledListaCekanjaTabela class (fig. 1.):
    Java Code:
    package Interfejs;
    
    import Logika.*;
    import java.util.Vector;
    import javax.swing.table.DefaultTableModel;
    
    public class Forma_PregledListeCekanjaTabela extends javax.swing.JFrame {
    
        private ListaCekanja lcListaCekanja;
        //private Vector<Dete> deca;
        private DefaultTableModel modelTabele;
        private Vector naslovTabele;
    
        public Forma_PregledListeCekanjaTabela(DefaultTableModel pModelTabele) {
    
            initComponents();
    
            this.modelTabele = pModelTabele;
    
            this.tblDeca.setModel(this.modelTabele);
    
        }
    
        public Vector getNaslovTabele() {
            return naslovTabele;
        }
    
        public void setNasloviKolonaTabele(DefaultTableModel pModelTabele) {
    
            pModelTabele.addColumn((Object)"JMBG");
            pModelTabele.addColumn((Object)"Prezime");
            pModelTabele.addColumn((Object)"Srednje Ime");
            pModelTabele.addColumn((Object)"Ime");
            
        }
        
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            tblDeca = new javax.swing.JTable();
            btnIzmeni = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Obrada Dece - Pregled Liste Čekanja - Tabela");
    
            tblDeca.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
    
                },
                new String [] {
    
                }
            ));
            tblDeca.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            jScrollPane1.setViewportView(tblDeca);
    
            btnIzmeni.setText("Izmeni");
            btnIzmeni.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnIzmeniActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 599, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
                    .addComponent(btnIzmeni, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnIzmeni)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(153, Short.MAX_VALUE))
            );
    
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);
        }// </editor-fold>
    
        private void btnIzmeniActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
           try{
                int col = this.tblDeca.getSelectedColumn();
                int row = this.tblDeca.getSelectedRow();
                [B]Object d = (Object)this.tblDeca.getModel().getValueAt(row, col);[/B]
                System.out.println(d.getClass());
                //new FormaPromeni(t, this).setVisible(true);
            } catch(NullPointerException e){
                //new Poruka("Niste nista izabrali").setVisible(true);
            }
    
    
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton btnIzmeni;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable tblDeca;
        // End of variables declaration
    
    }
    The Forma_PregledListeCekanja class with the list that displays data (fig. 2):
    Java Code:
    package Interfejs;
    
    import Logika.*;
    import java.util.*;
    import javax.swing.DefaultListModel;
    
    public class Forma_PregledListeCekanja extends javax.swing.JFrame {
    
        private ListaCekanja lcListaCekanja;
        DefaultListModel modelListe;
    
        public Forma_PregledListeCekanja(ListaCekanja pListaCekanja) {
            
            this.lcListaCekanja = pListaCekanja;
    
            initComponents();
    
            modelListe = new DefaultListModel();
            this.liListaCekanja.setModel(modelListe);
    
            napuniModelListe();
        }
    
        private void napuniModelListe() {
            this.modelListe.removeAllElements();
            Vector<Dete> dete = this.lcListaCekanja.dajListuCekanja();
            for (Dete d : dete) {
                this.modelListe.addElement(d);
            }
        }
    
        public void osveziModelListe() {
            this.napuniModelListe();
        }
    
         private void btnIzmeniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIzmeniActionPerformed
            // TODO add your handling code here:
            try{
                [B]Dete d = (Dete)this.liListaCekanja.getSelectedValue(); // The lists getSelectedValue() method returns right type of object hat I want (object instance of Dete class)[/B]
                System.out.println(d.getClass());
                //new FormaPromeni(t, this).setVisible(true);
            } catch(NullPointerException e){
                //new Poruka("Niste nista izabrali").setVisible(true);
            }
        }//
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            liListaCekanja = new javax.swing.JList();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Obrada Dece - Pregled Liste Čekanja - Lista");
    
            liListaCekanja.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            jScrollPane1.setViewportView(liListaCekanja);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 572, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(218, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-808)/2, (screenSize.height-634)/2, 808, 634);
        }// </editor-fold>                        
    
    
        // Variables declaration - do not modify                     
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JList liListaCekanja;
        // End of variables declaration                   
    
    }
    ListaCekanja class:
    Java Code:
    package Logika;
    
    import java.util.Vector;
    
    public class ListaCekanja {
    
        private Vector<Dete> vekListaCekanja;
        private String sClanoviListe = "";
    
        public ListaCekanja(){
            this.vekListaCekanja = new Vector<Dete>();
        }
    
        public void dodajDete(Dete pDete){
            this.vekListaCekanja.add(pDete);
        }
    
        public Vector<Dete> dajListuCekanja(){
            return vekListaCekanja;
        }
    
        public String dajClanoveListe() {
    
            if (vekListaCekanja.size() > 0) {
                for (int i = 0; i < vekListaCekanja.size(); i++) {
                    sClanoviListe += "\n" + vekListaCekanja.elementAt(i).toString();
                }
            } else {
                System.out.println("Ova Lista Cekanja nema ni jednog clana");
                System.out.println("----------");
            }
    
            return sClanoviListe;
        }
    }
    Dete class:

    Java Code:
    package Logika;
    
    public class Dete extends Osoba{
    
        private Porodica porodica;
    
        public Dete(){
        }
    
        @Override
        public String toString(){
            return this.getTxtJMBG() + " - " + this.getTxtPrezime() + ", " + this.getTxtSIme() + " " + 
                    this.getTxtIme() + " - " + this.getDteDatumRodjenja();
        }
    
        public Porodica getPorodica() {
            return porodica;
        }
    
        public void setPorodica(Porodica porodica) {
            this.porodica = porodica;
        }
    }
    Osoba class:

    Java Code:
    package Logika;
    
    import java.sql.*;
    
    public class Osoba {
    
        private String txtJMBG;
        private String txtPrezime;
        private String txtSIme;
        private String txtIme;
        private Date dteDatumRodjenja;
        private String txtAdresa;
        private String txtPTTBr;
    
        public String getTxtJMBG() {
            return txtJMBG;
        }
    
        public void setTxtJMBG(String txtJMBG) {
            this.txtJMBG = txtJMBG;
        }
    
        public String getTxtPrezime() {
            return txtPrezime;
        }
    
        public void setTxtPrezime(String txtPrezime) {
            this.txtPrezime = txtPrezime;
        }
    
        public String getTxtSIme() {
            return txtSIme;
        }
    
        public void setTxtSIme(String txtSIme) {
            this.txtSIme = txtSIme;
        }
    
        public String getTxtIme() {
            return txtIme;
        }
    
        public void setTxtIme(String txtIme) {
            this.txtIme = txtIme;
        }
    
        public Date getDteDatumRodjenja() {
            return dteDatumRodjenja;
        }
    
        public void setDteDatumRodjenja(String dteDatumRodjenja) {
            this.dteDatumRodjenja = Date.valueOf(dteDatumRodjenja);
        }
    
        public String getTxtAdresa() {
            return txtAdresa;
        }
    
        public void setTxtAdresa(String txtAdresa) {
            this.txtAdresa = txtAdresa;
        }
    
        public String getTxtPTTBr() {
            return txtPTTBr;
        }
    
        public void setTxtPTTBr(String txtPTTBr) {
            this.txtPTTBr = txtPTTBr;
        }
        
    }
    Thank You.
    Attached Thumbnails Attached Thumbnails Retrieve value from JTable-3.jpg  
    Last edited by mcdhappy80; 11-30-2010 at 11:20 PM. Reason: Update Info

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    TL;DR
    Recommended reading:
    SSCCE : Java Glossary
    How To Ask Questions The Smart Way

    And if you really want to see the volunteers here jumping over themselves to offer help, don't post NetBeans/Matisse autogenerated code.

    db

Similar Threads

  1. retrieve OS architecture
    By jitheshmenon in forum Advanced Java
    Replies: 4
    Last Post: 08-21-2010, 07:52 PM
  2. how to retrieve data from other jtable..
    By javanewbie2010 in forum AWT / Swing
    Replies: 1
    Last Post: 02-12-2010, 09:32 AM
  3. How do I retrieve an array from a different class?
    By Psyclone in forum AWT / Swing
    Replies: 11
    Last Post: 02-08-2010, 10:52 AM
  4. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  5. how to retrieve the width of the column
    By katie in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 11:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •