Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: reference issue

  1. #1
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default reference issue

    I am adding panels to a jtabbed pane dynamically.. as described below..

    I have maintained a table in a database named.. "rms_sessions" that maintains a list of recent years..say, 2009, 2010, 2011.





    now the panels that are added to the jtabbedpane are named as "2009-2010","2010-2011" and so on.. (dynamically) as

    Java Code:
    package retail_manager;
    
    import java.sql.*;
    import javax.swing.*;
    
    public class mtabs extends javax.swing.JFrame {
    
        private JScrollPane jScrollPane;
        private JTable jTable;
    
        public mtabs() {
            initComponents();
    
            [COLOR="Red"]Connection connection = null;
            Statement statement = null;
            ResultSet rSet1 = null, rSet2 = null;
            try {
                connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Oracle9i", "rms", "stephen00.rms");
                statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
                rSet1 = statement.executeQuery("select count(*) from rms_sessions order by year");
                rSet1.next();
                int count = rSet1.getInt(1), i = 1;
                rSet2 = statement.executeQuery("select * from rms_sessions order by year");
                String str;
                while (i <= count - 1) {
                    rSet2.next();
                    str = rSet2.getObject(1).toString() + "-";
                    rSet2.next();
                    str = str + rSet2.getObject(1).toString();
                    rSet2.previous();
    
                    jScrollPane = new JScrollPane();
    
                    jTable = new JTable();
                    jTable.setModel(new javax.swing.table.DefaultTableModel(
                            new Object[][]{},
                            new String[]{"    NAME", "    ADDRESS"}));
                    jScrollPane.setViewportView(jTable);
    
                    jTabbedPane1.add(str, new JPanel().add(jScrollPane));
                    i++;[/COLOR]
                }
            } catch (SQLException sqlExcption) {
                sqlExcption.printStackTrace();
            } finally {
                try {
                    statement.close();
                    connection.close();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jTabbedPane1 = new javax.swing.JTabbedPane();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE));
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE));
    
            pack();
        }// </editor-fold>
    
        public static void main(String args[]) {
            mtabs m = new mtabs();
            m.setBounds(100, 50, 1030, 700);
            m.setVisible(true);
        }
        private javax.swing.JTabbedPane jTabbedPane1;
    }



    now the problem is that i am able to refer only to the panel that is added at the very last.i.e., "2010-2011". Suppose these tables contain some data and i need to change the content of the table "2009-2010". but i am not able to do so.. because i don't have any references to that table.

    please suggest.. i guess i need to save the refernces but don't know how to do so. If there is any other way possible. please suggest. thanks.
    Last edited by Stephen Douglas; 07-04-2010 at 11:58 AM.
    The Quieter you become the more you are able to hear !

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Implement a class MyPanel that contains the scrollPane, the table and some utility methods like getTableModel(). Now you add as many of those panels to your tabbedPane. Via tabbedPane.getComponentAt you get the reference to the coresponding MyPanel and you can call getTableModel on it.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    how can i obtain the index to the panel that was clicked??
    The Quieter you become the more you are able to hear !

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

  5. #5
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    i have already cut the code very short.. which part do you want me to eliminate? i mean i can't eliminate the part in red..
    The Quieter you become the more you are able to hear !

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    An SSCCE would have no database code.

    Also, to get an index to the selected tab, add a ChangeListener to the JTabbedPane and in the listener's stateChange method call getSelectedIndex.

  7. #7
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    i got it:rolleyes:.. the SSCCE would be something like this i guess..
    Java Code:
    package retail_manager;
    
    import javax.swing.*;
    
    public class mtabs extends javax.swing.JFrame {
    
        private JScrollPane jScrollPane;
        private JTable jTable;
    
        public mtabs() {
            [COLOR="Red"]initComponents();
            int count = 0;
            String str = "";
            while (count < 2) {
                str = "" + count;
                count++;
                str = str + "-"+count;
                jScrollPane = new JScrollPane();
    
                jTable = new JTable();
                jTable.setModel(new javax.swing.table.DefaultTableModel(
                        new Object[][]{null, null},
                        new String[]{"    NAME", "    ADDRESS"}));
    [COLOR="Blue"]            jScrollPane.setViewportView(jTable);
                jTabbedPane1.add(str, new JPanel().add(jScrollPane));[/COLOR]
            }
        }
    [/COLOR]
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jTabbedPane1 = new javax.swing.JTabbedPane();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE));
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE));
    
            pack();
        }// </editor-fold>
    
        public static void main(String args[]) {
            mtabs m = new mtabs();
            m.setBounds(100, 50, 1030, 700);
            m.setVisible(true);
        }
        private static javax.swing.JTabbedPane jTabbedPane1;
    }
    Last edited by Stephen Douglas; 07-04-2010 at 12:49 PM.
    The Quieter you become the more you are able to hear !

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I would use Phil's advice and create something closer to this:
    Java Code:
    import java.awt.BorderLayout;
    
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import javax.swing.table.DefaultTableModel;
    
    @SuppressWarnings("serial")
    public class MTabs extends javax.swing.JFrame {
    
      public MTabs() {
        initComponents();
    
        int count = 4;
        int i = 0;
        while (i <= count - 1) {
          String str = "String " + i;
          
          String[][] data = {
              {"Name [0, " + i + "]", "Address [0, " + i + "]"}, 
              {"Name [1, " + i + "]", "Address [1, " + i + "]"}};
          DefaultTableModel tableModel = new DefaultTableModel(data, new String[]{
            "    NAME", "    ADDRESS"});
          
          MyPanel myPanel = new MyPanel(tableModel);
    
          jTabbedPane1.add(str, myPanel);
          i++;
        }
        
        jTabbedPane1.addChangeListener(new ChangeListener() {
          public void stateChanged(ChangeEvent arg0) {
            MyPanel myPanel = (MyPanel) jTabbedPane1.getSelectedComponent();
            DefaultTableModel tableModel = myPanel.getTableModel();
            for (int j = 0; j < tableModel.getRowCount(); j++) {
              System.out.print("Row " + j + ": ");
              for (int k = 0; k < tableModel.getColumnCount(); k++) {
                System.out.print(tableModel.getValueAt(j, k) + ", ");
              }
              System.out.println();
            }
          }
        });
      }
    
      private void initComponents() {
    
        jTabbedPane1 = new javax.swing.JTabbedPane();
    
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE));
    
        pack();
      }
    
      public static void main(String args[]) {
        MTabs m = new MTabs();
        m.setBounds(100, 50, 1030, 700);
        m.setVisible(true);
      }
    
      private javax.swing.JTabbedPane jTabbedPane1;
    }
    
    @SuppressWarnings("serial")
    class MyPanel extends JPanel {
      private DefaultTableModel tableModel;
      
      public MyPanel(DefaultTableModel tableModel) {
        this.tableModel = tableModel;
        
        JTable table = new JTable(tableModel);
        JScrollPane sPane = new JScrollPane(table);
        
        setLayout(new BorderLayout());
        add(sPane, BorderLayout.CENTER);    
        
      }
      
      public DefaultTableModel getTableModel() {
        return tableModel;
      }
      
    }

  9. #9
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    actually i didn't understood the whole procedure.. suppose i first add the changelistener.. then get the selected index.. but what next.. how would i get reference to the panel?? and manage the changes made to the jtable.. added to that panel. so in that case i should seek for the refernce for the jtable or the panel?
    The Quieter you become the more you are able to hear !

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Stephen Douglas View Post
    actually i didn't understood the whole procedure.. suppose i first add the changelistener.. then get the selected index.. but what next.. how would i get reference to the panel?? and manage the changes made to the jtable.. added to that panel. so in that case i should seek for the refernce for the jtable or the panel?
    An example is in my SSCCE above.

  11. #11
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    ok. but in my case i have added the jtable on the scrollpane and not directly to the panel then in that case what modifications am i needed to make to your code.
    The Quieter you become the more you are able to hear !

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Stephen Douglas View Post
    ok. but in my case i have added the jtable on the scrollpane and not directly to the panel then in that case what modifications am i needed to make to your code.
    ?

    Clarify please?

  13. #13
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    i am wondering when i have added the jtable to the jscrollpane then :

    jTabbedPane1.getSelectedComponent() will return refernce to what??
    The Quieter you become the more you are able to hear !

  14. #14
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    in my above SSCCE i have painted it blue..
    The Quieter you become the more you are able to hear !

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Stephen Douglas View Post
    i am wondering when i have added the jtable to the jscrollpane then :

    jTabbedPane1.getSelectedComponent() will return refernce to what??
    It returns the component that was added to the JTabbedPane, in my situation, the MyPanel object. This JPanel uses BorderLayout and holds a JScrollPane BorderLayout.CENTER, so it behaves is as if you were adding the JScrollPane directly to the JTabbedPane.

  16. #16
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    i want to make the least possible modifications to my project(i am about to complete it. making modifications means making them at multiple other places which i can't afford in respect of time).. so i am hoping you could help me..

    to my above SSCCE..i am adding table to scrollpane and to panel as :
    jScrollPane.setViewportView(jTable);
    jTabbedPane1.add(str, new JPanel().add(jScrollPane));


    now i am wishing if i could "just add sufficiently" the changelistener.. something like..

    public void stateChanged(ChangeEvent arg0) { System.out.println(((javax.swing.JPanel)jTabbedPan e1.getSelectedComponent()).getTableModel().getValu eAt(0, 0));
    }

    please suggest. thnx
    The Quieter you become the more you are able to hear !

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Bottom line: Do your just sufficient changes work? If not, you may need to make more changes than that.

  18. #18
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    in your case you have explicilty defined the " MYpanel ".. but i have not... so any suggestions?
    The Quieter you become the more you are able to hear !

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

    Default

    If you had to, you could get to the JTable and its model by extracting out components and casting the components to what you know them to be, but this is a very messy "solution", one that can break easily with the slightest change to your program, and so I'll leave it to you to create the code as I really don't recommend you do this but rather do what Phil suggested.

  20. #20
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    one last question on this post.. How you people have expert answers to every questions.. Since how long are you working in Java??

    By the way, obviuosly i respect your recommendation. but how can i extract components?
    The Quieter you become the more you are able to hear !

Page 1 of 2 12 LastLast

Similar Threads

  1. persistent of same reference twice
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: 04-25-2010, 06:53 PM
  2. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 03:45 PM
  3. creating a reference to a value in a map
    By kellykellykelly in forum Advanced Java
    Replies: 1
    Last Post: 02-28-2009, 07:59 AM
  4. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 11:46 AM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 02:51 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
  •