Results 1 to 20 of 30
Thread: reference issue
- 06-22-2010, 05:03 PM #1
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 !
- 06-22-2010, 05:13 PM #2
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
- 07-04-2010, 06:19 AM #3
how can i obtain the index to the panel that was clicked??
The Quieter you become the more you are able to hear !
-
Please create an SSCCE which will let us see and play with your code and let's see if we can better help you.
- 07-04-2010, 12:00 PM #5
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 !
-
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.
- 07-04-2010, 12:30 PM #7
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 !
-
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; } }
- 07-04-2010, 12:34 PM #9
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 !
-
- 07-04-2010, 12:46 PM #11
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 !
-
- 07-04-2010, 12:49 PM #13
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 !
- 07-04-2010, 12:50 PM #14
in my above SSCCE i have painted it blue..
The Quieter you become the more you are able to hear !
-
- 07-04-2010, 01:24 PM #16
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. thnxThe Quieter you become the more you are able to hear !
-
Bottom line: Do your just sufficient changes work? If not, you may need to make more changes than that.
- 07-04-2010, 01:30 PM #18
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 !
-
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.
- 07-04-2010, 01:47 PM #20
Similar Threads
-
persistent of same reference twice
By smackdown90 in forum Web FrameworksReplies: 0Last Post: 04-25-2010, 06:53 PM -
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM -
creating a reference to a value in a map
By kellykellykelly in forum Advanced JavaReplies: 1Last Post: 02-28-2009, 07:59 AM -
removing reference
By ajith_raj in forum Advanced JavaReplies: 4Last Post: 02-12-2009, 11:46 AM -
Getting the Object Reference Name
By Deathmonger in forum New To JavaReplies: 2Last Post: 03-12-2008, 02:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks