Need to create a WIZARD on Netbeans (jDialog vs CardLayout)? - java

I would like to create a "wizard" on a JDialog using the CardLayout, triggered by user pressing the New button from the menubar. In Netbeans I have created a JDialog through which I have a series of jPanels in CardLayout format. In my "New" menu item I wrote the following code to initiate the jDialog as follows,
CardLayout cl = (CardLayout) jDialogNew.getLayout();
cl.preferredLayoutSize(jDialogNew);
cl.show(jDialogNew, "card1");
However, the compiler comes up with the following error,
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.awt.BorderLayout cannot be cast to java.awt.CardLayout
If anyone is out there that can take me through creating a wizard on "Netbeans" I'd be eternally grateful

Your jDialogNew has a BorderLayout set as its layout and and not a CardLayout, meaning that when you call getLayout() to try to fit it into a variable that cant hold a BorderLayout an exception is thrown. The classes are different so you cannot cast from one to another, causing a ClassCastException.
A possible solution to this is to set your own layout for the jDialogNew. I dont have code infront of me so I cant check myself, but try looking for a method like setLayout(), and pass in a new layout of your choice.

you can do with following
create JFrame -> Add "CARD LAYOUT"
add JPanels to project. Design JPanels. Customize init code of JFrame. Insert JPanels with this.add(jpanel name). for all jpanels setVisible(false) - then setVisible true which jpanel you want to start with.

The way I did it in Netbeans was very easy! All I had to do was to was to introduce a separate JFrame in my resources package (being a part of my overall package) and in that JFrame I created a JPanel with the CardLayout, under which I created all my other JPanels relating to that top JPanel. Now having the JFrame I could set my fixed canvas plus everything else I needed to construct and activate my CardLayout "Wizard" dialogue box! Then I had to call the new JFrame from with my application whenever the event was triggered. It made life a whole lot easier and it works just great!

Related

Open elements and struct of jFrame in another like a menu

i have 5 jFrames in my java project. And i want to make like a Main Menu.
I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame.
And if i click other button the main frame will clean and charge other jframe.
It is possible? im programming with java jdk 8 and netbeans.
Thanks
Edit:
I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated
i have 5 jFrames in my java project.
And that's a problem.
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
Yes this can be solved by getting the contentPane (usually a JPanel) from the JFrame whose content you want to display within the currently displayed JFrame, and making it the contentPane of the displayed JFrame, for example:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
But this extra gymnastics is bulky, prone to bugs and unnecessary. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
For this situation what I recommend is that you do that, that your GUI classes create JPanels, and that you add the ones that you want to swap to a JPanel that uses a CardLayout. And then whenever you want to show a different "card", call show(...) on the CardLayout object, passing in the JPanel that uses it, as well as the String key that was used when adding the "card" JPanel to the CardLayout-using JPanel. This is all well-explained in the CardLayout Tutorials.
Other useful links:
For rationale on why to avoid manually swapping please see: What's so special about CardLayout vs manual adding/removal of JPanels?
For using a CardLayout to help control a "multi-page" application with multiple classes, please see: How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes

How to refresh a JPanel at runtime?

I have found that there are many similar topics here, but my problem is something more complicated.
Background of my problem:-
I have a JFrame called Main. On this JFrame I have two buttons and one JPanel called WorkingPanel. Then I have another JPanel(called PlayerPanel) but this one is a seprate file (as a class).
Now I want that when I click a button, it should change WorkingPanel to PlayerPanel. I have wrote following code.
private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
WorkingPanel = new PlayerPanel();
System.out.println(WorkingPanel.getName());
WorkingPanel.revalidate();
WorkingPanel.repaint();
WorkingPanel.setVisible(true);
Window.revalidate();
Window.repaint();
}
Please guide me, Thanks.
I have found that there are many similar topics here, but my problem is something more complicated.
On the contrary, your description is of a rather basic problem that is very easily solved by using a CardLayout. I suggest that you do this now. If you had it in place your method could be as simple as:
private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
cardLayout.show(cardPanel, WORKING_PANEL);
}
where cardLayout is your CardLayout variable, cardPanel is the JPanel that displays the "cards" that displays the swapping JPanels, and WORKING_PANEL is a String constant that you used when you added your WorkingPanel instance to the cardPanel.
Point 2:
Don't use a MouseListener on a JButton as it won't behave correctly. For instance, if you disable the button via setEnabled(true) the button won't truly be disabled. Instead use an ActionListener with JButtons as the tutorials will show you. That is what they are for.
Edit
For examples of CardLayout-using GUI's, please check out:
getting Jcomponent from other class changes frame size
Java CardLayout Main Menu Problem
Change size of JPanel using CardLayout
Java CardLayout JPanel moves up, when second JPanel added
Java swing; How to toggle panel's visibility?
Clear components of JFrame and add new componets on the same JFrame
gui multiple frames switch
JLabel displaying countdown, java
This one is unusual in that it uses a CardLayout and has one panel fading into the other panel:
CardLayout showing two panels, flashing.
You could use CardLayout instead of that approach. You will be able to switch between different panels very easy and efficiently. It's also wort of mentioning that use of CardLayout is less verbose approach.
Use a CardLayout, containing your two panels, but only showing one at a time. The CardLayout is documented, with examples, in the Swing tutorial.

How to load Jpanel in a Jframe using button click (Netbeans)

I'm using netbeans IDE and I created a Jframe with two Jpanels one is for Jbuttons and other one is for load another Jpanels to it when clicks those buttons.
I tried to do it from buttonclick action.
Jpanel2 j2=new Jpanel2();
JPanel1.add(j2);
j2.setVisible(True);
but this code is not working. I want to know how can I do this.
(I think this is also same as loading JinternelFrames)
Try to call revalidate() method.
Use a card layout and do it correctly.You can learn how to use card layout here

how to start, developing swing based application with few panels with next buttons for each

I'm new to java.I'm creating a swing based UI. I've created 2 frames, each one in separate .java file inside same package.
These two frames represents 2 screens (panels) of application. When Next button in first frame is clicked, it should move to second frame.
When I checked, these two classes are having main method, I think it should be correct way for creating applications. there should be only one main method.
When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?
As I'm beginner, Can somebody suggest me in how to start up with these kind of applications? what are the guidelines that need to be followed? And please help me in finding documentation related to starting up with the development of such applications.
After going through the answers, My comments are:
I used the following code to go to next panel from first panel, but didn't worked.
private void gotoNextPanel(){
// jPanelFirstScreen.setVisible(false);
JPanelSecondScreen jpanelSecondScreen= new JPanelSecondScreen();
jpanelSecondScreen.setVisible(true);
UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();
upgradeUtilityGUI.removeAll();
validate();
repaint();
// upgradeUtilityGUI.add(jpanelSecondScreen);
upgradeUtilityGUI.getContentPane().add(jpanelSecondScreen, "card2");
jpanelSecondScreen.setVisible(true);
validate();
repaint();
}
I'm using netbeans, and 've added two panels to the cardlayout of frame. And when I use the above code to change panels, Nothing is happening, the first panel is still appearing. Can somebody tell me, how to write code for moving from one panel to another when both the panels 've been added to cardlayout of jFrame ?
Use a CardLayout, as shown here (and one frame) as mentioned by others.
When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?
Make the panels public access level and they will be available from other packages.
One problem in that code snippet is implied by the line:
UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();
It goes out of scope before ever being added to a container. Also, their should be no need to remove anything when adding a new card to the layout, and no need to call repaint().
If your application is as simple as having only two panels you shouldn't create two JFrames. You should create a JFrame with two JPanel each of them contains the neccessary information for you. If you are ready with your first panel you can call setVisible(false) on it, and call setVisible(true) on the 2nd frame. It is the one of the most easy-to-understand solution.
But, it only depends on you if it is good for you or you would like to use some more detailed solution.
Don't use two or more JFrames, nor with separated and compiled Jar files, this is road to the hell, better would be look at CardLayout,
What you should do is have a single JFrame for the application, then you add and remove JPanels as you want to move between screens.
Each of your JPanels should basically have the following...
1. A JButton called "Next"
2. A ButtonListener for each button, that tells the JFrame to load panel2, panel3, etc.
As part of the ButtonListener, you basically just want to call something like JFrame.removeAll() to remove the existing panel, then JFrame.add(JPanel) to add the next panel.
By having 1 JFrame, you also only have 1 main() method.

Draw a JPane in JFrame

I have search on internet and found a lot of information about drawing in Java. But when I add new JFrame class in Netbeans then I cannot add a own JPane in the JFrame. Hopefully somebody can help me with this issue/question.
Drawing the JPane is possible when I make a new JFrame in a class, but I would like to use the design view in Netbeans. That is not possible when I make a new JFrame.
I look forward to receiving an answer.
It looks like you made a JFrame instance yourself, and then tried to add a JPane(l) to it using NetBeans swing builder. This wont work. Try creating a new swing class using the swing builder and let netbeans make the Jframe.
Also, make sure you set a correct layout for your JFrame.
Also, like BenCole said, I think you mean a JPanel, not a JPane.
As an alternative, create your own top level container and add your designer panel to its content pane. Here's a simple example. In this way you can limit the use of the designer to a single panel, while you explore other layouts.

Categories

Resources