Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33467
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Swing - Using JTabbedPane |
| Date | 2016-01-17 10:08 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <n7gajo$40b$1@dont-email.me> (permalink) |
| References | <df13f200-8551-4401-9da0-ad0ecc203a5e@googlegroups.com> <n7e5m2$ojp$1@dont-email.me> <78ecf434-7366-4dd4-a083-7319f78aad4c@googlegroups.com> |
On 1/17/2016 9:35 AM, 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.
>> [... walks the component tree until finding the table ...]
This should work, but it feels fragile: If anything changes in the
containment pattern you're lost. (For example, if you decided to omit
the JScrollPane on a "special" table somewhere, or if the next version
of Java rearranges something about the way the viewport works, or ...)
You might improve robustness by giving each JTable a unique name
(see the setName() and getName() methods of java.awt.Component, from
which JTable eventually descends). You could then walk the component
tree looking for the desired name; this would make you immune to small
changes in the containment pattern.
Even better (perhaps) might be to subclass JScrollPane with your
own table-containing JComponent, giving it a getUltimateContent()
method:
class MyScrollableContainer extends JScrollPane {
private Component ultimateContent;
public MyScrollableContainer(Component content) {
super(content);
this.ultimateContent = content;
}
// Other constructors if desired ...
public Component getUltimateContent() {
return ultimateContent;
}
}
This way, you don't care how many layers of additional containment
(JViewport, whatever) the JScrollPane class may interpose: You've got
a direct line to the JTable with which you created the thing.
The larger question, though: Why do you want to find the JTable,
anyhow? Yes, there are reasons to manipulate it directly: Maybe you
want to auto-select a group of rows in response to something other
than a GUI action, maybe you want to sort or filter the rows, maybe
there's something else you want to do with the table itself. But IMHO
you'll usually be better off treating the JTable as a GUI gadget, much
like a JButton, and concerning yourself instead with the TableModel
that holds its data. You could just whack away at the TableModel and
not worry about which JTables might or might not happen to be
displaying it at the moment, whether any of those JTables happen to
be visible, and so on.
Okay, so that's not always viable (and one reason for wanting to
find the JTable is so you can call getModel() on it). But it's quite
often the case that directly manipulating the GUI gadget is doing things
backwards, and that working with the model instead will do better.
So: Can you explain why you want to get hold of the JTable?
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Swing - Using JTabbedPane galt_57@hotmail.com - 2016-01-16 07:53 -0800
Re: Swing - Using JTabbedPane Jeff Higgins <jeff@invalid.invalid> - 2016-01-16 19:29 +0000
Re: Swing - Using JTabbedPane galt_57@hotmail.com - 2016-01-17 06:35 -0800
Re: Swing - Using JTabbedPane Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-01-17 10:08 -0500
Re: Swing - Using JTabbedPane Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-01-17 09:49 -0800
Re: Swing - Using JTabbedPane Martin Gregorie <martin@address-in-sig.invalid> - 2016-01-17 20:00 +0000
Re: Swing - Using JTabbedPane Jeff Higgins <jeff@invalid.invalid> - 2016-01-17 14:21 +0000
Re: Swing - Using JTabbedPane Eric Douglas <e.d.programmer@gmail.com> - 2016-01-18 06:39 -0800
csiph-web