• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing Anonymous Classes From a JPanel

 
Elam Daly
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a JPanel, in which I am adding anonymous JComboBoxes per request of a user. In fact, it is some what of a table structure:



So if the user added a new BBox, which would be BBox4, then I would have to add the corresponding ResultBoxes's all the way through the last entered ABox, which would be ABox3 in the example. And vice-versa with a ABox, as all ResultBoxes would have to be created to the last BBox.

The problem arises when I need to delete either an ABox or BBox, because I have no way of deleting the corresponding ResultBoxes, as they are all anonymous.

Does anyone have any design suggestions for correcting this? The user has specified that they want this format, with a table of JComboBoxes, but I'm pretty sure that the anonymous approach won't work.

Cheers.
[ June 09, 2005: Message edited by: Elam Daly ]
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't think it will work without solid references to the combo boxes that you are adding. You can probably get a list of the combo boxes added anonymously by calling:



But then you have to figure out exactly which combo is the one you are after from the resulting array entries.

If I was doing it, I would keep a 2 dimensional (row,column) ArrayList data structure to hold references to the combo boxes. ArrayLists can grow and shrink as needed which is probably what you need in this case (as the user adds, or deletes components).

Cheers, Jared.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An "anonymous class" is an actual class defined in terms of inheritance, without giving it a name; for example



"r" is an instance of an anonymous class that implements Runnable. It's anonymous because we don't know it's name.

You're using "anonymous" to mean what most people would probably call "unreferenced." As the previous poster said, just because you create things at runtime this way, that's no reason for them not to have variables pointing to them; you just have to use arrays or Lists.

Finally, note that you would find that this performed better for large arrays, and was also easier to manage, if you used a JTable of JComboBoxes instead. JTables are actually very clever. You'd write a TableModel which kept track of the information for each cell (the selection, the items in the combo box) and the JTable could present the whole GUI while physically creating only one JComboBox object, which would be moved all over the screen, as needed. The memory savings are vast for a large-enough array, and the layout performs much better.
[ June 10, 2005: Message edited by: Ernest Friedman-Hill ]
 
Elam Daly
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for both of your replies.

Ernest, I don't see how I could use a JTable in this situation. The only static field is the A/B header, everything else is a JComboBox. Could you elaborate?

Cheers
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Elam Daly:
Thanks for both of your replies.

Ernest, I don't see how I could use a JTable in this situation. The only static field is the A/B header, everything else is a JComboBox. Could you elaborate?

Cheers


JTable allows you to use a JComboBox as the editor for each field. The fact that the headers are the only part that is static seems to lend itself to using a JTable instead of laying out all the ComboBoxes yourself. I don't know all of the details, so I'll just suggest that you google for more information.

Layne
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here would be one place to find out how to use a combo box for an editor. When I first started digging into JTables I found them very confusing, I still do in many respects, but they are very powerful and give you a lot of control over how the data is displayed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!