containers to use for JTabbedPanes - java

I am developing a math game and I was thinking about displaying the data like so:
Display the data that the user is to screencap and send to me, in a JFrame. This data will be displayed in the form of two JTabbedPanes, each tab showing a table (with five rows and 8 columns)
Ask the user (via JOptionPane.showConfirmDialog()) if they would like to see a graphical representation of their progress.
If they click "Yes", then show them a JFrame with their chart. This JFrame will have a JMenuBar with only one JMenu with two JMenuItems (an option for both of the two modes).
In this new JFrame, display via CardLayout the active JTabbedPane (the FloatMode has four difficulties, while the IntMode has all the FloatMode's difficulties plus Elementary School difficulty.
I know that multiple JFrames can be bad practice, and I was thinking about using JSplitPanes, but then it might get too crowded (and I wouldn't be able to let the users choose the mode!). Would a JDialog be recommended, or would you recommend something else? //I apologize if it sounds like I am asking you all to do some of my thinking for me...

Seems best suited to a CardLayout with a single control to select between cards (e.g. a JList or JComboBox, JSpinner, menus etc.).
E.G. as seen in this short example, that uses radio buttons in a button group to select the card.

I might have a potential solution: make the JMenuBar universal! That would eliminate the need for two JTabbedPanes and thus speed up performance (due to less Objects floating around, especially expensive GUI Objects)!

Related

Creating set of JButtons according to retrieved database values

I'm working on a POS system for a fast food restaurant. I've developed Adding, updating and deleting products using a MySQL Database. Now I need to create the POS GUI (using SWING) which the cashier uses to create the bill. The interface I have in mind is like what MacDonald's uses, there's a set of buttons with the product images. (I have stored BLOBS of products successfully)
example image :
I have no idea on how to accomplish this, it would be awesome if its possible to generate a set of dynamic Jbuttons which gets the image of a product along with the name and price. Is this the best way to accomplish this? and how do I achieve this? A few details to put me on the correct path will be greatly appreciated!
Thanks.
You have at least four containers, the top row, the bottom row and each row containing a separate container in the middle (for the buttons).
The basic layout for the rows might be a GridLayout, each row would probably use a BorderLayout with the nav buttons in the WEST and EAST positions. CENTER container could use a GridLayout, but won't give you the look you're after. You could use a FlowLayout and even a GridBagLayout, but you'd need to ensure the size of the buttons where correct for your needs
On the left I see a JTable and two JPanels, containing the buttons, held together with a GridBagLayout.
On the right I see a JList. See How to Use Lists for more details. You'll probably also want to have a look at Concepts: Editors and Renderers and Writing a Custom Cell Renderer for details about how you could customise the look of the cells and How to Write a List Selection Listener for details about how to determine when the user changes the selection
Together, they are probably maintained by a GridBagLayout within a single container for ease of use
Along the button is probably another container using a GridBagLayout.
Altogether, they are probably held together by a GridBagLayout
Have a look at Laying Out Components Within a Container, How to Use GridBagLayout, How to Use BorderLayout and How to Use FlowLayout for more details
You will need to look into the GridLayout.
In your case, it might be a little bit more complex since you have multiple grids, some of which seem to be nested within each other.
You should be able to allocate the grid dynamically and then leave it to the layout manager to distribute things evenly over the page.
Depending on the complexity of your layout, it might also be a good idea to look into the GridPane provided by JavaFX.
Hi I look for you this problem and I find this solution ;
http://www.javasrilankansupport.com/2012/06/create-dynamic-jbutton-with-image-and.html

switching between JTabbedPanes to be displayed in a JFrame

I have a math game that has two modes, each with five difficulties. I wish to give the players a choice to see a chart representation of their game history, as well as a table representation (for my purposes; this is a beta-test). The table part, I have already done, but the problem is now with implementing my chart idea. My idea is thus:
Make a menu with only two menu items (the two game modes)
For each of these modes, make a JTabbedPane that will house the five charts (the five difficulties)
The problem with this is, although I can make the menu items mutually exclusive (trivial!), making it to where only one JTabbedPane displays is another animal. I could have one item selected from the menu by default (which will display one JTabbedPane by default), and I know that I can check which JMenuItem was selected, and then do something like this:
if (otherTabbedPaneIsShowing)
{
tableFrame.remove(otherTabbedPane);
}
tableFrame.add(desiredTabbedPane);
//This is pseudocode, of course, but you get the idea.
My main concern about this approach is the hidden algorithmic complexity. Will this be a time-consuming effort, and if so, is there a "better" way to do what I am trying to do?

Navigate between windows in a simple quiz app

I have to make a simple quiz application in JAVA and I have no idea on how to navigate between windows, for example, how to go to the next question or how to go back. I have a single question per window. Can you give me an idea on how to navigate? Should I use repaint() or a CardLayout or something else?
Thanks!
Yes CardLayout can be a good idea for this purpose, if you need some example take a look at the oracle how to.
If the quiz is multiple choice, you have other options: change the data that the questions (probably JRadioButtons) displays. Otherwise, I vote for a CardLayout. Also, if you want the user to be able to easily go back to other questions, you could display them all as JPanels in a GridLayout using JPanel held by a JScrollPane.

Java Netbeans: How to layer panels on top of each other?

Please bear with me as I've just started using NetBeans for the first time! Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time. It'll start with one panel, and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I've tried looking into utilizing LayeredPanes since that's what I've come after hours of researching this only.. I don't understand how to do it! I think using different panels would be much easier than using different frames, so that's why I'm just going to stick with layering panels.
If anyone could explain LayeredPanes, I'd be very grateful! I'm not sure my coding will help here, but if anyone needs it I'll put it up.
This tutorial should point you in the right direction, however, if you want to have items positioned over each other, you might also want to take a look at the CardLayout:
The CardLayout class manages two or more components (usually JPanel
instances) that share the same display space.
Reading your question I dont think that what you need is a LayeredPane,
Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time.
Tell exactly what you want to achieve, I mean on what you are working on.
...and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I'd like to suggest a JDialog , show dialogs depending on the user inputs. To make sure if this is what you need, you want to provide us with more information. :)

similar input dialog-creation patterns/swing

I have a created a UI in swing using NetBeans.
There are some use cases where the user presses buttons and a JDialog appears as a result.
The JDialog accepts input via JComboBox, at least 4.
E.g.
User presses "ButtonA" and a JDialog appears that displays the following:
"select X:" combo
"select Y:" combo
"select Z:" combo
"select O:" combo etc
I.e. beside each combo is a descriptive label. There are a few other controls in each JDialog besides each combo, i.e. JCheckbox, JTextarea
If user presses "ButtonB" a JDialog appears that displays the following:
"select A:" combo
"select B:" combo
"select C:" combo etc
So the dialogs are not the same but have some pattern in their presentation format. I.e. usage of comboboxes.
Initially I though to create a single JDialog and pass some arguments for the text it should display on the labels of the JDialogs and whether the JCheckbox should appear or not, if the JTextArea should appear or not and with what text etc, all depending on parameters passed in the initialization of the JDialog so as to have a single class for all use cases, but the code started getting really complicated and I rejected this approach.
What I did was create via GUI designer a JDialog, exactly as needed for each button press and came up with about 10-11 such JDialogs.
This appoach has made the programming much simpler but I do not know if it is a standard way to deal with this since I created 10-11 extra classes for the input.
My question is, whether what I did is reasonable, or there are better ways to deal with situation like this.
Thank you!
I think I would write a DialogBuilder class that returns a JDialog.
EDIT:
Visual vs Programatic creation of Dialogs
I'm used to generating Dialogs by writing code. before Java I used to code in Delphi in which Dialogs were created using a GUI - so initially I did find this annoying and even tedious. Nowadays I rather enjoy it. So yes, I would probably throw out (most of) the dialog code created by netbeans. Unless your dialogs are more complicated than your question suggests.
Complexity
Yes, there is a danger that you'll end up taking a lot of time to create quite complex code. However, if you are like me, you'll learn a lot doing it. One thing you'll learn is how to use various patterns to avoid that complexity. Finding an elegant way to solve those issues is something I find pretty satisfying - it's one of the aspects of programming I find most enjoyable.
Parameters
When you mention your concern about "if argA&& argB && !argC then display JCheckbox etc for 10-11 different jdialogs" - I can only suggest you try to approach the problem from a different direction. For example, you could pass the DialogBuilder a list of pairs of labels and JComboboxes. Or maybe you could extend JCombobox so that it has an additional field and a getter for it's label.
e.g.
DialogBuilder builder = new DialogFactory();
builder.add("Select X:", new Xcombo(...), true); // with checkbox
builder.add("Select Y:", new Ycombo(...), false); // without
...
builder.getDialog.setVisible(true);
or
builder.add("Select X:", new MyCombo(xList), false);
builder.add("Select Y:", new MyCombo(yList), false);
You can see (I hope) that the DialogBuilder.add(String, JComboBox, boolean) method wouldn't need dozens of complex if ... then ... else clauses.
Just my $0.02 worth.

Categories

Resources