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?
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 oldposts about that, rather than writing a new post.
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 oldposts 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 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.
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.