loading new content on same window

已回答

I am trying to load another pane but within the same window. i have 2 basic buy/sell buttons and when user clicks on either button it clears the current one but loads new buttons, labels, etc.

I already set up the forms for the buy and sell screens i want i only linked the main first screen with a class.

This is the code i have so far.

[code]

package guistuff;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GuiTest {
private JButton btnpurchase;
private JPanel panelMain;
private JButton btnrefund;

private JPanel panelPurchase;
private JLabel lblnullbuy;
private JLabel lblamtbuy;

private JPanel panelRefund;
private JLabel lblnullsell;
private JLabel lblamtsell;

public GuiTest() {
btnpurchase.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Component component = (Component) e.getSource();
JFrame frame = (JFrame) SwingUtilities.getRoot(component);
frame.setContentPane(new GuiTest().panelPurchase);
frame.setVisible(true);
}
});
btnrefund.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Component component = (Component) e.getSource();
JFrame frame = (JFrame) SwingUtilities.getRoot(component);
frame.setContentPane(new GuiTest().panelRefund);
}
});
}
public void purchasecontent(){

}
public void refundcontent(){

}
public static void main(String[] args){
JFrame frame = new JFrame("GuiTest");
frame.setContentPane(new GuiTest().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(480,320);
frame.setVisible(true);
}
}
[/code]

when i click on the button refund it spits out an error...

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:621)
at javax.swing.JFrame.setContentPane(JFrame.java:698)
at guistuff.GuiTest$2.actionPerformed(GuiTest.java:35)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
评论操作 固定链接

Your panels are null, they are not initialized anywhere in your code. You need to bind them to classes and make a method returning the panel from the bound classes. Then call this method in the main form to obtain the instance of the initialized panels.

1
评论操作 固定链接

yes i realized what i was doing wrong. on a side note. why is it that i try to run a program i get nothing just edit configurations? i went to project structure but its all set to 1.8.0.

0
评论操作 固定链接

yea i found out it was missing a main.

I uploaded a new project for this.

http://24.57.9.199/etc/GUIHelloWorld.rar

check that out i redid it so each form has a class binding to it.

When i load the Choice class i want it so the button purchase would bring the user to purchase. i tried calling it with Purchase.main(); but it doesnt work right saying its expecting a result. Also the setsize function isnt working., is there an overlapping parameter im missing here?

Also. when im trying to design the purchase form all the rows are messed up and i cant get my buttons to group together nicely. Whenever i try to readd it just screws everything else up. when you check it out you will know what i mean.

Also, thank you for your help.

0
评论操作 固定链接

You need only one main method, the second form needs to return the JPanel you want to use in the main form. Create a getter method, instantiate the form class and get the panel from it.

Using card layout is recommended in case you want to dynamically change the panels displayed in the form. See https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html .

1
评论操作 固定链接

while i work on the card stuff can you please explain to me why the gui designer mode is messy in terms of rows and columns?

0
评论操作 固定链接

Not sure, please check the GUI designer tutorials, you may be using the layouts incorrectly. If you think it's a bug, please report at https://youtrack.jetbrains.com/issues/IDEA with the exact steps to reproduce.

1
评论操作 固定链接

please review the new project http://24.57.9.199/etc/GUI.rar

when i run the program nothing loads in the window.

I was using this link https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html as a reference.

0
评论操作 固定链接

You are adding 2 empty JPanels to to the card layout, what do you expect to see?

 JPanel choiceGUI = new JPanel();
JPanel purchaseGUI = new JPanel();
1
评论操作 固定链接

i realized my mistake but how do i port in the already made layout? 

0
评论操作 固定链接

I've already explained it above. Create an instance of the class bound to the form and return the root panel of this class.

1
评论操作 固定链接

.....im not exactly sure how i would go about that...im still fairly new to java.

0
评论操作 固定链接

http://24.57.9.199/etc/GUI.rar

please review, im getting tons of repeating errors.

 

Exception in thread "main" java.lang.StackOverflowError
at java.awt.Window.init(Window.java:507)
at java.awt.Window.<init>(Window.java:436)
at java.awt.Window.<init>(Window.java:591)
at java.awt.Dialog.<init>(Dialog.java:665)
at java.awt.Dialog.<init>(Dialog.java:409)
at javax.swing.JDialog.<init>(JDialog.java:272)
at javax.swing.JDialog.<init>(JDialog.java:206)
at javax.swing.JDialog.<init>(JDialog.java:154)

at src.Choice.<init>(Choice.java:18)

at src.Choice.<init>(Choice.java:23) (repeating)

Exception in thread "main" Exception in thread "main" 

 

i redid purchase.java to include a GETTER

and the Choice.java errors are at 

public Choice(){

cards.add(new Choice().ChoiceGUI(), MainPage);
0
评论操作 固定链接

This support channel is for IntelliJ IDEA IDE specific questions, not for the generic Java programming questions. You can try http://stackoverflow.com/ if you are you new to Java and have problems with your first applications.

I've provided the project above which shows how to reuse different forms in the main form.

1
评论操作 固定链接

So i loaded your demo project correctly and ran it with no issues. But...i tried to manipulate it so when i click form 2 that it would clear the entire pane and slap form 2 form on there. This includes removing the buttons.

I tried to manipulate the main form to try and have cardlayout cover the entire contentpane but i was getting compiling errors.

When im trying to visualize it it feels like its "inception-like"

I uploaded a video in hopes to explain what i am trying to achieve.

 

https://youtu.be/cBhV8uj0UjM

0
评论操作 固定链接

yes but ofcourse without the ok and cancel.

 

i tried to replicate your style and creating jpanels.

please review the new program. i left it as basic as possible and im getting the exception error...still...

 

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to com.intellij.uiDesigner.core.GridConstraints
at com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:133)
at java.awt.Container.addImpl(Container.java:1127)
at java.awt.Container.add(Container.java:973)
at index.<init>(index.java:20)
at index.main(index.java:41)

 

once again link still the same

 

http://24.57.9.199/etc/GUI.rar 

0
评论操作 固定链接

You should get the idea from my project and apply it to yours, or just remove the OK/Cancel buttons.

Note that the layout manager of the panel which will show your cards has to be set in the GUI Designer to CardLayout:

1
评论操作 固定链接

this is the error im getting.

1
评论操作 固定链接

See my example, you JPanel must not have any buttons in order to change the layout manager.

1
评论操作 固定链接

ok i got it working, but when i try to put in a button it takes up the whole panel. i cant adjust it and when i try to put the buy and sell button in there it just overlaps...also when i try to run the program the button i put in cardpanel disappears.

0
评论操作 固定链接

Please see how my example works. You don't put anything on the card layout in the GUI designer. The code puts JPanels from the other forms there and switches between them.

1
评论操作 固定链接

OHHHHHHH i never thought to have the index.java as nothing but a placeholder for the other forms to fill it. It just clicked.

 

http://24.57.9.199/etc/GUI.rar

So i added new code and i added an addition class called choice.java

For choice.java i added the buy/sell buttons in there.

I added code in index.java to include the new card panel so i now have 3. It also loads the choice.form as default.

But when i try to compile i get this error...

 

Exception in thread "main" java.lang.NullPointerException
at index.<init>(index.java:26)
at index.main(index.java:42)

 

btnbuy.addActionListener(new ActionListener() {
index dialog = new index();
0
评论操作 固定链接

The code working with buttons has to go the classes bound to buy/sell forms. Since your index form no longer has any buttons, btnbuy/btnsell are null (GUI designer doesn't initialize them as they do not belong to the index form).

1
评论操作 固定链接

if i have to move the code for the action listeners to choice.java then i have to redeclare the cardlayouts everytime?

i think this is where "Inception" comes into mind. This is where i get really confused...

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class choice extends JDialog{
private JPanel contentPane;
private JButton btnbuy;
private JButton btnsell;
private JPanel cardPanel;

final static String FORM_ZERO = "Form Zero";
final static String FORM_ONE = "Form One";
final static String FORM_TWO = "Form Two";

public choice(){
setContentPane(contentPane);
setModal(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//cardPanel.add(new choice().getPanel(), FORM_ZERO);
cardPanel.add(new buy().getPanel(), FORM_ONE);
cardPanel.add(new sell().getPanel(), FORM_TWO);
final CardLayout cl = (CardLayout) cardPanel.getLayout();
//cl.show(cardPanel, FORM_ZERO);
btnbuy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
cl.show(cardPanel, FORM_ONE);
}
});
btnsell.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
cl.show(cardPanel, FORM_TWO);
}
});
}

public JPanel getPanel() {
return contentPane;
}
}
0
评论操作 固定链接

Yes, you do have to handle the events in the separate forms where the controls belong to.

1
评论操作 固定链接

so for the choice.java i would have to redeclare cardlayout and thus meaning i have to redo all declarations in index to also be in choice.java seeing as how when i click buttons it would change the card layout? Does this mean i have to make another cardpanel in choice too?

 

Im confused as to how choice.java would look like. the code i posted above would that be correct? And if so when i try to build it i get these errors.

 

Exception in thread "main" java.lang.NullPointerException
at choice.<init>(choice.java:21)
at index.<init>(index.java:18)
at index.main(index.java:25)

 These errors are related to the cards.add

Serge. I want to take a moment and say Thank You for being incredibly patient with me while i try to learn this and i wanted to say i greatly appreciate your help with me. This may very well be the last piece i need to bring all my other projects together (bitcoinj, QRCode).

0

请先登录再写评论。