Replacing contents in a frame using the optionpane-GUI - java

updating a panel
I am designing a program for my assignment using java.
I have made a log on box where user enters name and password and clicks on submit or register. Once clicked, an optionpane is brought
upEX.(JOptionPane.showMessageDialog(null, "Account registered", "Account",
JOptionPane.INFORMATION_MESSAGE);)
I was wondering how can I add an action listener to the OK button in the option pane that would remove all contents (username,pass,2btns) and replaces it with something else? I found this link: updating a panel which basically just says to use frame.remove and then add.
Another question:
My java file looks something like this
class MainFrame extends JFrame {
public MainFrame() {
// All the log in panels buttons are here, etc
}
}
I was wondering if it would be efficient to add the new box details in the same area or make a new class?

I was wondering how can I add an actionlistener to the OK button in the option pane that would remove all contents (username,pass,2btns) and replaces it with something else? I found this link: updating a panel which basically just says to use frame.remove and then add.
Don't. Allow the dialog to close, ascertain the action the user took, show another dialog...
Better yet, create you own JPanel, use a CardLayout, add all your views to it and navigate between them. Place this on an instance of a JFrame...
See How to Use CardLayout for more details
I was wondering if it would be efficent to add the new box details in the same area or make a new class?
It would depend. A Class should be a self contained unit of work, focused on accomplishing it's designed task...if your main class is doing more work then it should, then yes, separate the logic into separate class

Related

New window inside a main window?

I am a beginner and I am trying to make a text editor and I want to create a pop up window for text format when I press a menu button where I can put all things like font face, font size , font style etc. Can you tell me how I can make this new window? Thanks for your patience!
For example Notepad:
I think what you're after is a dialog of some kind.
Take a look at How to Make Dialogs for more details.
What I would do is design the basic UI onto a JPanel. I would then add this JPanel to an instance of a JDialog (possibly even using a JOptionPane) and show this dialog, making sure to make it modal, so you can easily retrieve the values set by the user.
This means that you can decide how best to show the user interface or even show it in a number of different ways as it's not constrained to a single top level container
You can simply create a brand spanking new JFrame and it will still be counted as the same application.
Tip: Use Eclipse Window Builder

Swing menu in Java

I'm wondering, how can I make a menu system (not JMenuBar) with swing components (like in games, where a new 'screen' comes in)? Maybe switching between panels? How?
I've heard about card layout but i'm not sure it's for me, and I don't really understand how should I implement it.
Example (sorry for ugly drawing): http://i57.tinypic.com/14mckkk.png
Based on the screenshot you've provided, you should look into using Swing Dialogs. Dialogs are little pop up windows that can be activated upon button presses, and that can contain as many or as few components as you like.
So your grey JFrame in the image would have three buttons; the action on clicking one would be to open a dialog populated with "Content A". The "Button X" in that dialog could either be a vanilla "Close" button that the dialog API will provide for you for free, or it could be some other button of your design.
If I'm not mistaken, what you are asking is for something like this:
In this case you can split the frame with using a layout of your choice that can fit to the size you want (personally I'd opt for a BoxLayout). Then you can go on with adding the buttons to the side panel and add that to the box panel so you end up with something like this:
Now add a JPanel and you'll have your content section. You'll have to make sure you add Listeners to the buttons so that the content that is displayed is updated. It'll look something like this:
button.addActionListener(
new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(content) {
content.removeAll();
}
populateContent();
content.validate();
}
});
I could be way off because of the little amount of information at hand, this is just my interpretation of the question. I would have commented but I don't have the rep so I'm sorry in advance if this is completely off!

Java: single frame vs multiple frames

Think about the classic installation process, where you have a "next" button and when you click it the content of the window changes. To represent this situation I thought of two possible solutions:
-when "next" is clicked destroy the current JFrame and create a new JFrame, maybe passing to his constructor useful information (e.g. actual window size, content inserted by the user in the current frame, ...)
-when "next" is clicked remove all the components from the current JFrame and add new components as needed
The first solution looks way better about OOprogramming, because I can keep separate classes for different frames and I can avoid huge methods that empty the frame and repopulate it. However the first solution sounds a bit "dirty" and I should pass lots of parameters to the new frame. To represent this situation I would choose the second solution.
Now think about a menu with an "option" component: in this situation I would create a new JFrame when "option" is clicked, so that I can populate it with option items. Is this a correct solution? Is there a way I can always know which one is the best solution? Are there any solutions I didn't think about?
Destroying the main JFrame would be silly -- not to mention jarring for the user. Just use a single JFrame and change its contents.
To implement an installer wizard, use a single JFrame containing one large JPanel on top and a smaller one containing the "Next", "Back", "Cancel" buttons along the bottom. When the Next or Back buttons are pressed, you replace the large JPanel. You can have many different JPanel subclasses, one for each "page" of the wizard.
There's a LayoutManager called CardLayout which is ideal for implementing this scenario -- it manages a "stack" of components, and only shows one of those components at a time. Use a BorderLayout in the JFrame. Into the center position put a JPanel with a CardLayout. Then add the individual pages of the wizard to that JPanel, so the CardLayout can manage them.
The CardLayout is well suited for this. You just swapout the JPanel contents when the "Next" button is pressed.

on click add dynamic text area -like object in Java

The title is a bit confusing, but I will be using Java and Jframe. Basically, I want to be able to click anywhere on the form and have a "text area/box" show up (maybe use a JTextField or JTextArea ?). I want the user to be able to edit, delete and move this string around as well.
I am thinking I need an actionlistener to listen for clicks on the form. Each click will call for a new text"box" to be created. I am not sure how to make this "box" editable, deleteable, or moveable by the user though.
I need a way to store the string and co-ordinate data too. Would it be a good idea to simply extend the JTextField or JTextArea to add co-ordinate information to them? I see that swing is event based, so I need some kind of trigger to "save" the text (was thinking the enter key, but I realize I'd like the user to be able to enter multi-line strings).
Any thoughts would be appreciated. I am familiar with Java but only have a bit of experience with the UI portion.
Instead of an ActionListener you will need a MouseListener to track clicks.
Sounds like you need an undecorated JInternalFrame with a text box in it on JDesktopPane. However, I don't think you can create an undecorated JInternalFrame, maybe start with a normal JInternalFrame with a TextBox in it and create new frames on mouse clicks on the Desktop Pane. Then see if you can make the JInternalFrame more like a Window.
Another route is a custom component that does everything you need. This is possible, just a lot more custom code.

How can i display new GUI based on menu click

I am learning java and building one project to test basics.
I have one menu item FILE and then sub menu item like
1)Front
2)Admin
3)Booking
I have separate gui made in separate files but i want that they should be visible in one area , with click on submenus
I am using swing , JmenuBar . Also the other guis are using Jframe
I have separate gui made in separate files but i want that they should be visible in one area
Most applications should only ever have a single JFrame, which indeed is your requirement since you want the separate GUI to be visible in the same area.
Therefore your other GUI, should not extend JFrame but instead should extend JPanel. Then you can just use a CardLayout on your real GUI to swap the panels in/out depending on which panel is selected from your menu. All these basic are covered in the Swing tutorial. I guess you would start with the section on:
How to Use Card Layout
How to Use Menus
Other people have already talked about ActionListeners and stuff, so that's half of the problem. The other half is how to actually deal with the multiple windows. I would probably not use one JFrame per different GUI, given that the spirit of the JFrame suggests you should only have one instance of it per application. Instead, I would look at using either JDialog or JInternalFrame. I'm not sure what you mean by
...should be visible in one area...
but JInternalFrame will allow you to implement something like a multiple document interface, where all the sub-GUIs would be contained within the frame of the main UI. JDialog would be give you independent windows like JFrame does.
If with "they should be visible in one area" you mean modal, then you should change all your JFrames to JDialogs and leave only the JFrame that contains your main-menu.
To do this, you need an ActionListener for each of the menu items. Then have each listener pass the instance of the JFrame you want to a method that controls where you want to position the window and show it.
//Make menu items
JMenuItem font = new JMenuItem();
font.addActionListener(new ActionListener() {
showWindow(new FontFrame());
});
JMenuItem admin = new JMenuItem();
admin.addActionListener(new ActionListener() {
showWindow(new AdminFrame());
});
...
//define frame handling method
void showWindow(JFrame f) {
...
f.setVistible(true);
}

Categories