Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: Swing - Using JTabbedPane Date: Sun, 17 Jan 2016 20:00:28 -0000 (UTC) Organization: A noiseless patient Spider Lines: 100 Message-ID: References: <78ecf434-7366-4dd4-a083-7319f78aad4c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Sun, 17 Jan 2016 20:00:28 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e5160e2be48f1ea550cf61512446f525"; logging-data="11748"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9C78926uz6fRPp7lvKgblPSR71LmT1gw=" User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Cancel-Lock: sha1:0GJPqEEaw0WdIyyiyty6/3ZP3ho= Xref: csiph.com comp.lang.java.programmer:33469 On Sun, 17 Jan 2016 09:49:45 -0800, Knute Johnson wrote: > On 1/17/2016 06:35, galt_57@hotmail.com wrote: >> On Saturday, January 16, 2016 at 1:32:07 PM UTC-6, Jeff Higgins wrote: >>> On Sat, 16 Jan 2016 07:53:54 -0800, galt_57 wrote: >>> >>>> So I'm throwing JTables into JScrollPanes and then adding them as new >>>> tabs, >>> >>> You've not shown any of this code. >>> >>>> but when I want to read the selected table again later, I'm not sure >>>> what to do. >>> >>> Here are some resources to help you figure it out. >>> >>> Java SE API Documentation >>> https://docs.oracle.com/javase/8/docs/api/index.html >>> >>> The Java(tm) Tutorials - Creating a GUI with Swing >>> https://docs.oracle.com/javase/tutorial/uiswing/index.html >>> >>>> Right now the code below seems to be working, >>> >>> Great! >>> >>>> but I'm not sure if it is a good approach. >>> >>> Impossible to tell without the rest of the code. >>> >>>> Also JViewport is a mystery. >>> >>> See the above resources. >>> >>> >> Thanks for the references. >> >> Let me post a correction to my original code and add a summary of how I >> create the tab... >> >> JTable jt; >> DefaultTableModel mod; >> . >> . //fill the table body . >> JScrollPane sp = new JScrollPane(jt); >> jTabbedPane01.addTab(tabname,sp); // so here the tab has been created >> >> ---------------- >> >> // in the routine below I want to read the jtable in the selected >> tab... >> >> int tab = jTabbedPane01.getSelectedIndex(); >> >> if (tab == -1){ >> statusMessageLabel.setText("Error: no tab selected."); >> return; >> } >> >> Component csp = jTabbedPane01.getComponentAt(tab); >> Component cvp; >> Component cjt; >> >> JTable jt = null; >> >> if (csp instanceof JScrollPane){ >> cvp = ((JScrollPane)csp).getComponent(0); >> if (cvp instanceof JViewport){ >> cjt = ((JViewport)cvp).getComponent(0); >> if (cjt instanceof JTable){ >> jt = (JTable)cjt; >> } >> } >> } >> >> if (jt == null){ >> statusMessageLabel.setText("Error: selected tab "+ tab + " is not >> a table."); >> return; >> } >> >> > This seems like a lot of work to find your JTables when you could just > keep a list of references to them somewhere. That way, you wouldn't > care what your component hierarchy was and you could change it without > having to rewrite your code. > Nice one. A further thought: if you use a Map to store the reference list you can use the name of the corresponding as the reference key for each JTable. That could also be generalised into a list of class instances, each holding the data and class references you need externally to manage the content of each JScrollPanel. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |