JMenuBar and a item array - bad idea? - java

I've poked around the internet a bit and my lack of findings makes me guess yes, but would using an array to fill the items of a JMenuBar be a less than good idea? I also think yes since whenever you wonder "is this a bad idea?" it usually is. However, my newness to programming makes me question if I know a bad from good idea at this point.
Originally I thought it'd be good since most of the time arrays are beneficial and save space. However, when I began thinking through the code I realized it wasn't exactly practical. To start if you do all the items in one array you are trying to count to figure out which number goes with which menu.
I suppose you can do an array for the items in each menu, but if you decide to change an item to a menu so you can have sub-menus you're back to which-number-is-it issues.
Edit: It also makes creating and adding the items and adding action listeners much quicker
Opinions?

I'm going to go with no. although it looks like it would save alot of time, and is a very good idea, i'm afraid that it will not work.
I am saying this because the JMenu, or JMenuBar 'add' method is only good for use with single JMenus or JMenuItems. I have tried and I got an error saying that it is used with JMenu not JMenu[].
If you would like, it might be possible to make a class that adds arrays to JComponents, by going through each array item until null, and adding it seperatly. That will be alot of extra work, but it might just pay of for speed and efficiency.

Related

How do I make one component visible in more than one pane in a JTabbedPane?

I was wondering if there was a way to make one component (in this case a JPanel) visible in more than one of the tabs.
I have a simple JFrame which is completely filled with a JTabbedPane. Each tab has a slightly different function, which is why they're in different tabs. However, they each output the same type of information. I was wondering if there was a way to make it so the same output panel could be seen in all the tabs, without having to create one panel for each tab.
I realize that it is impossible to add one component multiple times (and have it display independently each time), which is why I'm not optimistic about this being possible, however if it is, it would make my code much cleaner. In the case that this is not possible, I am completely open to alternate suggestions that achieve a similar result. I am in the very beginning of my project, so it won't be too difficult to change things.
This image gives a rough idea of what I'm trying to make it look like:
I have searched around for anyone dealing with this issue, however I have had little luck finding anything relevant. If I missed something, I apologize for wasting your time.

Develop a software with navigation Button

I'm trying to write a program that has several navigation buttons, for example the classical "Back" and "Forward" buttons. I would need a way to let the user to navigate through the different pages (JPanels, to be true). How can I do that? Or better, how would a real programmer (I'm a beginner jet) do that? I'm using for the moment CardLayout and I thought to store the "path" that the user does and use previous() and next() methods that CardLayout provides. What about this solution? Is it a bit "dummy"? Does a kind of "navigation pattern" exist?
You may simply store created JPanels in a List or a Stack as long as they keep they internal states unchanged by any other JPanels. However this might be a little resources hungry, thats why its better to store just internal state of an object rather than whole object.
Take a look at Memento Pattern:
http://en.wikipedia.org/wiki/Memento_pattern

Java setting button values on memory game

I have been working on a memory game i asked a question about yesterday, I got some brilliant suggestions but none of them seem to be helping my problem i can get the buttons to shuffle around the grid layout fine, but i cannot figure out how to keep the set values i tried using "setActionCommand" but i don't think this is the way i can do it for matching the values of two buttons within my memory game.
gameBtn[0].setActionCommand("1");
gameBtn[1].setActionCommand("2");
gameBtn[2].setActionCommand("3");
gameBtn[3].setActionCommand("4");
gameBtn[4].setActionCommand("5");
gameBtn[5].setActionCommand("6");
gameBtn[6].setActionCommand("7");
gameBtn[7].setActionCommand("8");
gameBtn[8].setActionCommand("1");
gameBtn[9].setActionCommand("2");
gameBtn[10].setActionCommand("3");
gameBtn[11].setActionCommand("4");
gameBtn[12].setActionCommand("5");
gameBtn[13].setActionCommand("6");
gameBtn[14].setActionCommand("7");
gameBtn[15].setActionCommand("8");
above is how i used the setActionCommand but i cant find out how you would compare them to each other to find a match for example matching club1 to diamond1 and club2 to diamond2. would this not mean i would have to do something like this for every single match i wanted?.
if (gameBtn[0].getActionCommand().equals(gameBtn[8].getActionCommand())){
sameValues();
}
if (gameBtn[1].getActionCommand().equals(gameBtn[9].getActionCommand())){
sameValues();
}
although the above might seem to work as it does not throw any errors im left thinking there must be a more efficient better way to do it i accomplished this at first before i added the shuffle to move the buttons on the grid i was using another list of values, but know its randomized the card on the button no longer actually matches the buttons set value.
have a look at the link at the bottom of the question if you need more information from my last question, Any help would be much appreciated as this has been bugging me for a while know thank you.
link: Java concentrate is there a way to shuffle jbuttons

Java Observable/Observer vs just passing references

I'm trying to get a grasp on a couple concepts. Lets imagine you have a JFrame and in that JFrame there are two panels, we'll say left_half and right_half. If I click a button in right_half, I want something to change in left_half. The issue is that the right half doesn't know the left half exists. So, you could tell the Frame, but technically neither panel knows the Frame exists either, right? The Frame can change the panels, but panels can't change the Frame, or so it seems to me.
So, I pass a reference to the Frame into the panels. Now the right_half can call Frame.setVariable(data) and the Frame can, from that same method, say left_half.setStuff(data). That just seems wrong to me and I have been looking for a way to do it without passing references up and down the hierarchy.
Next, someone says "That's why Observable exists!!" Cool, I think to myself. I then found many confusing examples of how to use Observable that didn't help at all. Finally I see this one, which makes sense.
http://www.javaquizplayer.com/blogposts/blogpost7.html
However, it has this: "observable.addObserver(mainWindow);" mainWindow is the equivalent of Frame in my example above, and it had to pass a reference to attach the Obserable to! So even with Observable, I have to pass references down the hierarchy?
It just seems wrong. If it's not wrong, that's fine... I can do it this way. However, my question once all that back-story is finished is simply this: how are you supposed to pass data between two panels? I'm okay with abstract answers if they're in plain language, I'm okay with code samples if they're short and easy to follow. I'm not a pro Java coder, I can't just search through 29 API pages and 1400 lines of code and just suddenly understand how it works... yet. I'll get there.
The observable pattern (in the form of event listeners) is OK and is good practice. You see, even though RightPanel knows someone might be listening to all its events, it does not know who is listening. Well, technically, it could go through all its listeners and use reflection to find out who they are; now, that would be bad practice.
As it is, RightPanel knows someone might be listening to it, and that's all. Components are always aware someone might be listening, since the whole Swing is based on it. What matters is that RightPanel compiles without LeftPanel (or frame), and is completely decoupled from it, except via the listener interface. Not only cool, but standard.
As a side note, your class structure does not have to mirror your nesting panel hierarchy. Depending on what you are doing, it may be entirely fine to control behavior of both left and right panels from inner classes within Frame. (Personally, I'd use a JPanel, not a JFrame, since it allows for more flexibility, but this can be easily refactored). Otherwise, you risk splitting your View/Controller into too many closely coupled classes, and that would violate encapsulation and cause a lot of boilerplate code. Normally, I don't code big fat classes and try to refactor them into smaller ones; Swing is usually an exception. Better one big fat class than a maze of densely coupled classes. Unless of course you have a reusable component or some piece of functionality that can be clearly and intuitively decoupled; not just a couple of buttons or checkboxes that have no meaning in and of themselves. The simple fact that some subcomponents are situated within a certain panel in a component tree should not be a major factor IMHO.
For that matter, I usually don't nest JPanels; I use MigLayout and make all components into siblings. Matter of taste! I'd just encourage you to check out MiGLayout first and see whether you like it.

java-gui creation approaches via description files?

I have the following requirement for a GUI, that the user will have a number of available actions to perform.
Currently, it is implemented a series of JButtons that the user presses.
But the actions are a lot, and in each version more "actions" would be possibly offered.
I am not sure how this is better presented/done in GUIs.
I have read that there are ways to form a gui describing relevant information in an xml file.
I am using Netbeans and swing. Is the xml a good idea, to describe the actions available and for example create the same number of buttons? If yes, how would I start on this?Additionally would a jtoobar be a good idea for the actions? I mean add as many buttons as needed in the Jtoolbar.
I guess this is a general question but I am not experienced in GUIs.
Any help would be appreciated!
Thank you.
There seems to be two separate issues involved:
How to describe and create a large number of actions?
How to represent a large number of actions in GUI?
#Tom's answer is for #1, and #Fabio's for #2.
You can use any textual format for #1, xml, csv, whatever you like. It's indeed a good idea to separate that from code. XML is great for hierarchical data, so it may be overkill if you just need a flat list:
<doc>
<action name="Action 1" id="ACT1" description="blah blah" icon="icon1.gif"/>
<action name="Action 2" id="ACT2" description="yada yada" icon="icon2.gif"/>
...
</doc>
But parsing such a simple XML is basically free, so you may as well just use it. You do not, however, need a full-featured XML GUI toolkit like SwiXML, unless you want to add many other GUI widgets with complex layout to your app.
Note the attributes I have in the above sample. id would map to a unique action command. You can display description and icon (I suppose you use icon already) any way you want. You could also have other properties like mnemonic, accelerator, etc., at which point using XML starts to pay off: you can add arbitrary attributes that you need.
One obvious omission in the XML is the actual actions themselves. I do not think you should put Java code in XML. It defeats the separation of concern. Instead you can define your action code in a generic way (e.g. extend AbstractAction) and map them with the action IDs. If you do use AbstractAction, you can trivially map your attributes to action property keys like Action.NAME, Action.LONG_DESCRIPTION, etc.
Now you have parsed the XML into a list of action objects, and here comes the second question: how do you display them?
JList (per #Fabio) is indeed the most efficient way. It's much more compact than a whole bunch of individual buttons, yet unlike JComboBox you can see many items at once, and you can easily add sort/search/filter.
But list is not very flexible. You could use a custom ListCellRenderer to display icon and tooltip (for description), but you'll start to stretch it when you want to group items.
I think the most flexible way would be a tree table, which allows you to have multi-level hierarchy. You can start with 2 columns, the first column showing action names hierarchically, the second column showing description.
You could put the table in a collapsible panel so that it can be hidden when user wants to focus on the results.
Now w.r.t. JToolBar, you're right that it's standard, however as Fabio's comment pointed out, it's bad usability when you have too many buttons on a toolbar (like M$ Word before ribbon).
What would be a great use of a toolbar, however, is to allow user to place actions of their choice onto the toolbar, like most popular desktop apps do. You could use a "Customize Toolbar" dialog, or simply let user drag-n-drop items from your list or table.
Unfortunately, many of the GUI constructs that I mentioned above are not available in JDK. You can find all of them (tree table, search/filter, collapsible panel, customizable toolbar, etc.) from the excellent commercial JIDE libraries (disclaimer: I don't work for them, though I sometimes wish I do), or you can find FOSS alternatives.
I don't know if i did understand your question well, but it seems to me that the JButton approach isn't the most efficient way to do such thing. Imagine that the number of actions starts to be really big, drawing too much buttons leads to an unintuitive and non-appealing interface. An alternative would be using a JComboBox or a JList, to list all the actions not requiring much space and having a single button "do!" to execute the action.
I hope it was useful ;)
Contrary to popular belief around a decade ago, XML is not usually a good idea. An appropriate language to express this "programming" in is Java.

Categories