This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Best strategy for handling actions across multiple windows? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Best strategy for handling actions across multiple windows?" Watch "Best strategy for handling actions across multiple windows?" New topic
Author

Best strategy for handling actions across multiple windows?

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Hi,

I am writing an application at the moment and want to know the best way (if there is any) of what the subject bar says. I have a main JFrame window with a bunch of 'option windows' that pop up when I want to confirm an action on the main window. For example, the main window has a tab section using jtabbedpane where I can add and remove tabs on the pane with buttons. Each tab is named in order ie. tab 1, tab 2, tab 3, etc and i can add more on the fly with an 'add tab' button on the main window. When I go to remove a tab I use a 'remove tab' button on the main window that pops up another window (another jframe) as a 'select the number of the tab you want to remove and confirm removal (or cancel)' window. Everytime I remove a tab I have to rename all the tabs so they are in order again. If I remove tab 3 out of a group of 5 tabs the list will read incorrectly ie tab 1, tab 2, tab 4, tab 5. I have them renaming properly but I am just questioning my method. I use a static void method from the main window in the pop up window class therefore not having to worry about passing all the tabs across to the pop up window to rename the tabs every time I remove one. I cant call the 'renameTabs' method in the main window because everytime i click remove tab (in the pop up windows action performed method) I have to rename all the tabs after every removal. I hope that makes sense!

Edit: I might as well share some code with you!



As you can see im using static methods from the main window 'GUICode' to add and remove tabs through the pop up window. If I instantiate the GUICode class in the pop up window class the constuctor will create a new window everytime. I hear you say "take the code that creates the window out of the constructor" but is that neccessary?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Why are you using static methods? Those cannot add tabs to a particular object.
If you have many buttons, you end up with a dreadful procedural method full of if elses. I tend to quote my old    posts about that, rather than writing a new post.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Campbell Ritchie wrote:Why are you using static methods? Those cannot add tabs to a particular object.
If you have many buttons, you end up with a dreadful procedural method full of if elses. I tend to quote my old    posts about that, rather than writing a new post.


Dont understand what you mean by those cant add tabs to a particular object. Its just one tabbed pane I am adding tabs to. I dont know any other way to work on components from one frame in another frames actionperformed method except using static methods from the other class to access them directly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
A static member cannot access an object. If you are adding tabs to a particular pane, you cannot be using a static method.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Campbell Ritchie wrote:A static member cannot access an object. If you are adding tabs to a particular pane, you cannot be using a static method.


The component is a JTabbedPane which I have declared static, therefore I can access it through a static method "getTabbedPane" (which returns it) in the other class. I am adding and removing tabs to it that way.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You shouldn't have to declare anything static. Just don't do that.

And what? You're naming your tabs "Tab 1" and "Tab 2" and so on and then you have to renumber them if you delete them? What's the point of that? Just call them all "Tab" and be done with it. That's equally understandable and easier to program. Or preferably, give them meaningful names.

As for the "popup windows", hopefully they are JDialogs. A JDialog shouldn't actually do anything itself, it should just ask the user what to do. The creator of the JDialog should then perform the requested action, if the user clicked OK.

It's also easy to overuse JDialogs, and it sounds like you might be doing that. Don't pop up a box asking the user which tab to delete; just provide each tab with a "Close" button which allows the user to delete that tab, for example.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best strategy for handling actions across multiple windows?
 
Similar Threads
HashMap problem
jQuery tabs UI issue
eclipse3.x short cut keys.
Help JMenu is getting frozen and won't go away
jtabbedpane is a pain in the butt; how do I place labels and move them around?