Re: Trouble with my applet
What do you mean when you say that it "will not work"?
Re: Trouble with my applet
Sorry, i should have been more clear. When the first JFrame opens up in my editor's (BlueJ) applet viewer, it displays the two JButtons "save" and "view". When "view" is clicked on, another JFrame is opened that would contain the ArrayList full of contacts were it filled with some. When "save" is pressed, it should open a new JFrame that contains several JLabels and JTextFields that allow the user to input information such as a name, address, phone number etc. Instead, clicking the button does nothing, not even opening a new JFrame. I'm not sure if this is due to the ButtonListener classes, or if i'm missing something else entirely. If past experience is anything to go by, then it's probably something relatively simple that i can't seem to find. If you need any additional code, like possibly the Storage interface, i can post that as well.
Re: Trouble with my applet
Hmm, this seems like it should work. Are you sure the JFrame isn't just popping up under the window the applet is in?
I'd love to test this, but I can't do that without an SSCCE- the shorter, the better.
Re: Trouble with my applet
I checked under the window, it's not there. The other button pops up fine enough, so i'm not entirely sure. As for the SSCCE, it compiles but i can't exactly guarantee that i didn't accidentally take out the problem; hopefully that's not the case.
Code :
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPage2 extends JApplet
{
public void init()
{
JButton save = new JButton("New Contact");
save.addActionListener(new ButtonListener());
Container c = getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
c.add(save);
}
private class ButtonListener implements ActionListener
{
private JTextField input, i1, i2, i3, i4;
public void actionPerformed(ActionEvent event)
{
JFrame frame = new JFrame("New Contact");
frame.getContentPane().setBackground(Color.WHITE);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
frame.setSize(100, 50);
frame.setVisible(true);
}
private class ButtonListener2 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
}
}
Re: Trouble with my applet
Ah, the problem is this line:
frame.getContentPane().setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
You're setting the layout of the JFrame's contentPane, but you're passing the JFrame (not the contentPane) into the BoxLayout constructor. This is generating an exception, which causes the method to exit before it shows the JFrame.
Exception in thread "AWT-EventQueue-1" java.awt.AWTError: BoxLayout can't be shared
Re: Trouble with my applet
Ahh, i would have never seen that. Thank you!
Re: Trouble with my applet
No problem. It can be confusing because JFrame contains a few methods that actually are just convenience methods that access the contentPane automatically. So calling JFrame.setLayout() is actually the same as calling JFrame.getContentPane().setLayout(). Same with the add() method, and a couple others.
Anyway, glad you got it sorted.