"void cannot be dereferenced" error..

J

justineee

Hi everyone, I really suck at this..

I have made many frames for my project but my problem is that when I
close one of them.. the whole program closes

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

public class ConfigGui extends JPanel implements ActionListener {
private JButton okButtonConfig;
private JButton cancelButtonConfig;
private JTextField inputConfig;

public ConfigGui() {
okButtonConfig = new JButton ("Ok");
cancelButtonConfig = new JButton ("Cancel");
inputConfig = new JTextField (5);

setPreferredSize (new Dimension (229, 84));
setLayout (null);

add (okButtonConfig);
add (cancelButtonConfig);
add (inputConfig);

okButtonConfig.setBounds (35, 55, 50, 20);
cancelButtonConfig.setBounds (95, 55, 95, 20);
inputConfig.setBounds (35, 10, 155, 25);

okButtonConfig.addActionListener(this);
cancelButtonConfig.addActionListener(this);
}


public void actionPerformed(ActionEvent jnd)
{
if (jnd.getSource()==cancelButtonConfig)
{
configGui().exit(0);
}
}

public void configGui()
{
JFrame frame = new JFrame ("Set the Dictionary");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new ConfigGui());
frame.pack();
frame.setVisible (true);
}
}

my problem here is in the if statement.. it says "void cannot be
dereferenced"..
I made the frame in void method because I am calling it in another
class..
Any tips please? :(
 
M

Mark Space

justineee said:
public void actionPerformed(ActionEvent jnd)
{
if (jnd.getSource()==cancelButtonConfig)
{
configGui().exit(0);
}
}

public void configGui()
{

As Peter says, it's normal to be very frustrated when starting out.
GUIs aren't easy, and while Swing isn't the hardest to learn, it's
nevertheless got a pretty steep learning curve.

That said, you seem to be trying stuff at random without really thinking
about it. Can I ask where "exit(0)" comes from? It's like you are
copying incorrectly from some other source, and it would be helpful to
know what that source is, since it might help us understand what you are
trying to do.

Based on the just the basic variable name ("cancelButton") I guess you
are trying to hide a (modal?) dialog window. The normal way to do this
is call "setVisible(false)" and then I like to call "dispose()" too,
just to clean up any resources that might be unneeded for a while.


Also I think you mentioned that your entire app closes sometimes. This
might be doing it:
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
"EXIT" means close the whole program, not close the window. You might
want "HIDE_ON_COSE" or "DISPOSE_ON_CLOSE" which just make that one
window go away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top