I'm pretty new to GUI programming in general, and have turned to using WindowBuilder Pro to help me design a bit smoother. I'm not sure if maybe some of you guys here will frown upon that, but I hope not
I'm currently working on a database client, and was considering a layout as follows...
Basically, one should be able to view different table-entries in a component. However, as the type of table that the current entry belongs to will vary, so will the different fields for the displayed entry.
For example, a user-table entry will have a name and a telephone number, etc. Whereas a item-table entry will have specifications for the item, like serial number and other stuff. Now I have a few questions about how to design this application...
1: I am building my main application by using HAS-A relationship to a JFrame. I remember from long ago when I coded a few GUIs by hand that it was a common approach to just extends JFrame for an application. But now I feel like I want to build my application as flexible as possible, so I am currently using a field to store the JFrame instead. Do you guys have any opinions about why this might be a bad idea, or would you say it's a good approach (as opposed to having the main application subclass JFrame that is) ?
2: I am currently considering a layout with for example a "main" JScrollPane that is used to view the table entries, but depending on what kind of entry is selected, I would like the JScrollPane to change its viewport-settings (JScrollPane.setViewportView(Component)) to different JPanels. This is ofcourse so that I can populate the various JPanels with the corrent fields depending on what kind of table the entry is selected from, since they will have different properties. What I'm wondering is if you think it makes sense to use a JScrollPane to view the different JPanels, where I have one JPanel-design for each table-type entry. Is there even any alternative to JScrollPane which also allows me to dynamically change its viewport-settings to another component, or is JScrollPane my only choice?
3: Also, is there any reason I should consider using something else than JPanel to view the different types of table entries? The information contained will be "simple" text-data, no images or so, which is why I personally think a basic JPanel will do fine...
If you have any other general advice on how to structure a GUI like this it's greatly appreciated, this is my first bigger GUI application and I feel like I have a ton of questions in my head...
I usually have a reference to the frame because I need to dispose it myself (after doing some custom stuff in the WindowClosing event).
Joined: Jan 17, 2011
Darryl Burke wrote:1. Yes on not extending JFrame. But do you even need it as a field? Does any code in any method other than the one which constructs and populates the frame require a reference to it?
2. I would prefer a CardLayout with a separate JScrollPane in each card to updating the viewport view of a single scroll pane.
3. What 'something else' did you have in mind?
... any other general advice ...
Follow MVC principles and you can't go wrong.
1: Hmm, good question, I just felt that it "should" be a member... is it perhaps more common to just initialize it and then not reference it anymore? I have absolutely no idea if I'll need to later or not...
2: I'm gonna look into that, although I'm not sure why you say you would use a JScrollPane for each card? Do you mean in case there are so many fields they are not all visible, or does the JScrollPane other functionality/methods that you think might be useful? (The main reason I myself planned on using it is because of the functionality JScrollPane.setViewportView() supplies, so I can swap between the different panels, but with a cardlayout I don't think that functionality would be needed?)
2b: I would also like to know if the functionality of JScrollPane.setViewportView() method exists in any other class, ie; is there any other class that could be used as a view into a set of JPanels, so that one can change which panel is active dynamically?
3: I don't know what else it could be, just thought maybe you guys might have ideas for a better class that I'm unaware of
Do you mean in case there are so many fields they are not all visible
That was implied by your OP.
, or does the JScrollPane other functionality/methods that you think might be useful?
The main reason I myself planned on using it is because of the functionality JScrollPane.setViewportView() supplies, so I can swap between the different panels,
but with a cardlayout I don't think that functionality would be needed?
is there any other class that could be used as a view into a set of JPanels, so that one can change which panel is active dynamically?
Primarily, JTabbedPane or a JPanel with a CardLayout. Then there's (not recommended, but...) setContentPane(...)
Joined: Jan 17, 2011
Alright fair enough, although don't you agree that if I was absolutely set on using a design with a single viewport to see different JPanels, then the JScrollPane with its setViewportView() method would be quite ideal to implement it? I mean is there even any other class that has an equivalent setViewportView() method?
Still I totally get your point and will be looking into the other two ways you mentioned. One last question (for now ;)...
I looked into the MVC model and feel like that's probably a great way to implement this application (thanks for the tip on that too btw), however... If I do it like that, I feel inclined to have my View extend JFrame after all. Do you still think it would be better for the View to be implemented with a JFrame variable (local or member) when using the MVC pattern?
Joined: Jan 17, 2011
Bumping this cus I'd really like to know, is anyone aware of a container class other than JScrollPane that supplies a setViewportView() method so one can easily change what is viewed ?