I want to create a new UI within another UI which should be displayed on clicking a button using Java Swing. What should I do?
In my program on clicking next button I need to display a new UI. How can I link the new ui to the already existing one?
Do you mean wizard like? You can place all your UIs in CardLayout and switch them. Or remove old and add new calling
container.revalidate();
container.repaint();
One approach is to make the second UI not visible from the start.
setVisible(false);
and when you click the button make it visible:
setVisible(true);
Related
I am working with WindowBuilder at the moment but have a problem making it display all panels in the program in the "Components" Window. I have a "StartPanel" for example with a button which when clicked causes the program to switch from "startPanel" to "nextPanel". Everything fine but in this case, "nextPanel" isnt shown in the "components" window, why?
When I however copy all the code which creates the "nextPanel" and write it outside of the "ActionListener" so that I do not have to click a button to create it, it appears in the "components" window. Is there a way to make every panel appear in "Components"? At the moment I have a frame with a getContentPane which has the 2 Panels in it, but only 1 is shown if I add the second one with a button..
I suggest you to create the next panel as a separate component (separate class file) so that you can edit it using window builder any time, and then instantiate it in the action listener.
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
I need to make a Java desktop application for a client and the last time I did Java it was 2 years ago and a little odd.
My main query is regarding navigation between GUI.
In the past, I would just create a new JForm (JFrame maybe?) whenever a button was pressed and a new GUI form/window would open up.
This time, I'd like the GUI to be inside one JForm/JFrame with just the inner content changing, how most applications look when you press a button.
I assume this is done by putting all of my GUI elements in JPanels, and deleting/creating them when buttons are pressed on the same JForm?
If not how do I do it properly?
I'll also be using Netbeans GUI editor, if anyone has a better alternative for a Java GUI builder/IDE, let me know.
Thanks!
The simplest approach would be to use a CardLayout
This will allow you to add multiple components to the UI and control which one is actively visible
Another approach could be to create menus and menuItem and having multiple JFrames now you can get the same JFrame to be displayed whenever you click the same menuItme button. This way You can minimize the creation of JFrames.
I've been struggling with some problem while creating my app based on Swing. I have a main JFrame which consists of:
JMenu
JPanel containing a JButton and a JLabel
JButton is linked with ActionListener. Clicking JMenu (MenuListener) brings up a JDialog with some form. The problem is, when the JDialog is closed (it doesn't make difference whether I do it with dispose() or rather showVisible(false)) I need to click the JButton two times before it triggers for the first time. From now it normally works with one click.
Every time the JDialog is in front, the problem appears.
PS. The JDialog is set to be modal, with JFrame as parent.
It sounds like a focus issue.
The first click restores focus to the app and the second clicks the button. Typically, I have seen this when the JDialog has the wrong parent and focus can not be returned.
Thank you for your answers.
I have considered posting some code, but it involves 4 classes so will be quite long.
I have also tried things with focus before, but nothing helped. What is interesting: if I display the JDialog by new myDialog.showVisible(true) it behaves like I've described. But if I use construction like this:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JDialog.setVisible(true);
}
});
it closes normally and parent frame doesn't need to be clicked before responding, but on the other hand the displayed Dialog needs so. Additonally, what I do not understand, after opening the Dialog cursor is placed in the text field and I can write normally, but to click some button on it I must click once on the Dialog, only second and next clicks behave like I want to.
PS. Closing the dialog like in the second included example changes nothing.
I have several JPanels that contain buttons, labels, etc. that I want to switch between from a main JFrame. Currently I am trying to use the this.add(JPanelname); method and this.remove(JPanelname); with the validate(); and repaint(); methods
The problem is it will add the panel to the JFrame but it will not remove it. I am not sure how exactly to go about this.
Maybe you should be using a Card Layout.
Or maybe you should be using modal JDialogs. So whenever you click on the "widjet" a new window is displayed. Then when you close the dialog you are back on your main frame.
If you are constantly switching between JPanels, then a JTabbedPane may be the right thing to use. It should not be necessary to call "validate" or "repaint" when you add or remove a JPanel. Do you have a layout manager installed? Do you make sure to call add/remove only within the UI event thread? Also, typically one does not call "validate()" but rather "invalidate()" to invalidate the container for updates.