Hi, I'm trying to add windowClosing into my program, so that when ran, I can exit by clicking on the X(top right corner).

The bold is what i added to try making the windowClosing work. I keep getting errors though. Anyone know what I can do to fix this?

errors:
1) DesktopEEG is not abstract and does not override abstract method windowDeactivated(java.awt.event.WindowEvent) in java.awt.event.WindowListener
2) cannot find symbol for method dispose()

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

public class DesktopEEG extends Panel [B]implements WindowListener[/B]
{
  Frame f;
  TextField filename;
  TextArea results;
  Panel toppanel,centerpanel,bottompanel,centerpanel2,centerpanel3;
  
public static void main(String [] args) 
{ 
   Frame f = new Frame(); 
   Font font = new Font("Arial", Font.BOLD, 14);
   f.setFont(font);
   f.setSize(500,400); 
   f.add(new DesktopEEG()); 
   f.show();    
}  
  
  public DesktopEEG()
  {
    this.setLayout(new BorderLayout());
    
    //top panel
    toppanel = new Panel();
    toppanel.setLayout(new BorderLayout());
    filename = new TextField(20);
    
    toppanel.add(new Label("Web EEG",Label.CENTER),BorderLayout.NORTH);
    toppanel.add((new Label("Enter the EEG file name (including full path e.g.:C:/trial1.txt):")),BorderLayout.CENTER);
    toppanel.add(filename,BorderLayout.SOUTH);
    
    this.add(toppanel, BorderLayout.NORTH);

    //center panel
    Font font2 = new Font("Arial", Font.PLAIN, 14);
    Font font3 = new Font("Arial", Font.BOLD, 14);
    
    centerpanel = new Panel();
    centerpanel2 = new Panel();
    centerpanel3 = new Panel();
    centerpanel.setLayout(new GridLayout (4, 3));
    centerpanel2.setLayout(new GridLayout(1,3));
    centerpanel3.setLayout(new BorderLayout());
    centerpanel.add(new Checkbox("Channel 1"));
    centerpanel.add(new Checkbox("Channel 5"));
    centerpanel.add(new Checkbox("Channel 9"));
    centerpanel.add(new Checkbox("Channel 2"));
    centerpanel.add(new Checkbox("Channel 6"));
    centerpanel.add(new Checkbox("Channel 10"));                  
    centerpanel.add(new Checkbox("Channel 3"));                
    centerpanel.add(new Checkbox("Channel 7"));                  
    centerpanel.add(new Checkbox("Channel 11"));
    centerpanel.add(new Checkbox("Channel 4"));                  
    centerpanel.add(new Checkbox("Channel 8"));                  

    
    Button Run = new Button("Run");
    Run.setFont(font3);
    Button Reset = new Button("Reset");
    Reset.setFont(font3);
    
    centerpanel2.add(new Label(" "));
    centerpanel2.add(new Button("Run"));
    centerpanel2.add(new Button("Reset"));
    

    centerpanel3.add(new Label("Choose a channel to calculate power for:"), BorderLayout.NORTH);
    centerpanel.setFont(font2);
    centerpanel3.add(centerpanel, BorderLayout.CENTER);
    centerpanel3.add(centerpanel2,BorderLayout.SOUTH);
    
    this.add(centerpanel3, BorderLayout.CENTER);

    //bottom panel                  
    bottompanel = new Panel();
    bottompanel.setLayout(new BorderLayout());
    bottompanel.add(new Label("Results"),BorderLayout.NORTH);
    bottompanel.add(new TextArea(5,1),BorderLayout.CENTER);
    bottompanel.add(new Label(" "), BorderLayout.SOUTH);
    
    this.add(bottompanel, BorderLayout.SOUTH);

    
  }
  
[B]public void windowClosing(WindowEvent e) 
{
		dispose();
		System.exit(0);
	}
[/B]


}

MasterGoGo for the first, since WindowListener is an interface. And DesktopEEG implements that interface , you'll need to implement the seven methods in WindowListener.

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/WindowListener.html

As for the second eclipse and netbeans run really slow for me so I will have to edit this post or post later.

But the things I noticed were:

no call for windowClosing
no addWindowListener() Method
and no dispose() method was ever created

edit again

They loaded up to slow so I download BlueJ

Is working with Frame a must? Or can you use JFrame? I switched it all to JFrame and the close works fine.

And I dont see you using WindowListener, so instead of implementing all the methods, just get rid of it. Everything should still work properly with it gone.

If I implement JFrame, do I just convert all Frame's to JFrame only or do I do it for Label and buttons as well?

I would change the Panel, Label, and Button to JPanel, JLabel, and JButton to be consistent, but I don't think it is mandatory.

Really? I'm using DrJava and I see the labels not bolded.
All I did with the original codes is replace Frame with JFrame.


[IMG]http://i4.photobucket.com/albums/y109/blue-star-acid/Jframe.jpg[/IMG]


Also, If I'm using JFrames, do I have to use JLabels and JButtons as well or can I just keep them as Labels and Buttons? I just got into GUI a few days ago, and I'm not familiar with some stuff.

Ok I added it back to Labels and Buttons and it wasnt bold. But when I added them as JLabels and JButtons the text was bold.

really? Mine didn't work at all if I changed labels and buttons to their J counterparts.

What can I use though if I use Frames instead of JFrames?

Error: I get a run error of
java.lang.NullPointerException
at DesktopEEG.<init>(DesktopEEG.java:24)
at DesktopEEG.main(DesktopEEG.java:121)

Can anyone offer input? I'm kind of confused as to what to do.

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

public class DesktopEEG extends Panel implements WindowListener,ActionListener
{
  Frame f;
  TextField filename;
  TextArea results;
  Panel toppanel,centerpanel,bottompanel,centerpanel2,centerpanel3;
  

  
  public DesktopEEG()
  {
    
    this.setLayout(new BorderLayout());
    f.addWindowListener(this);

    
    //top panel
    toppanel = new Panel();
    toppanel.setLayout(new BorderLayout());
    filename = new TextField(20);
    
    toppanel.add(new Label("Web EEG",Label.CENTER),BorderLayout.NORTH);
    toppanel.add((new Label("Enter the EEG file name (including full path e.g.:C:/trial1.txt):")),BorderLayout.CENTER);
    toppanel.add(filename,BorderLayout.SOUTH);
    
    this.add(toppanel, BorderLayout.NORTH);

    //center panel
    Font font2 = new Font("Arial", Font.PLAIN, 14);
    Font font3 = new Font("Arial", Font.BOLD, 14);
    
    centerpanel = new Panel();
    centerpanel2 = new Panel();
    centerpanel3 = new Panel();
    centerpanel.setLayout(new GridLayout (4, 3));
    centerpanel2.setLayout(new GridLayout(1,3));
    centerpanel3.setLayout(new BorderLayout());
    centerpanel.add(new Checkbox("Channel 1"));
    centerpanel.add(new Checkbox("Channel 5"));
    centerpanel.add(new Checkbox("Channel 9"));
    centerpanel.add(new Checkbox("Channel 2"));
    centerpanel.add(new Checkbox("Channel 6"));
    centerpanel.add(new Checkbox("Channel 10"));                  
    centerpanel.add(new Checkbox("Channel 3"));                
    centerpanel.add(new Checkbox("Channel 7"));                  
    centerpanel.add(new Checkbox("Channel 11"));
    centerpanel.add(new Checkbox("Channel 4"));                  
    centerpanel.add(new Checkbox("Channel 8"));                  

    
    Button Run = new Button("Run");
    Run.setFont(font3);
    Run.addActionListener(this);
    Button Reset = new Button("Reset");
    Reset.setFont(font3);
    
    centerpanel2.add(new Label(" "));
    centerpanel2.add(new Button("Run"));
    centerpanel2.add(new Button("Reset"));
    

    centerpanel3.add(new Label("Choose a channel to calculate power for:"), BorderLayout.NORTH);
    centerpanel.setFont(font2);
    centerpanel3.add(centerpanel, BorderLayout.CENTER);
    centerpanel3.add(centerpanel2,BorderLayout.SOUTH);
    
    this.add(centerpanel3, BorderLayout.CENTER);

    //bottom panel                  
    bottompanel = new Panel();
    bottompanel.setLayout(new BorderLayout());
    bottompanel.add(new Label("Results"),BorderLayout.NORTH);
    bottompanel.add(new TextArea(5,1),BorderLayout.CENTER);
    bottompanel.add(new Label(" "), BorderLayout.SOUTH);
    
    this.add(bottompanel, BorderLayout.SOUTH);

    
  }
  
  
public void windowClosing(WindowEvent e) 
{
  System.exit(0);
 }

  
  
public void actionPerformed(ActionEvent e)
{
   System.out.println( " I got a callback from " + e.getActionCommand( ) );
}

public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}





public static void main(String [] args) 
{ 
   Frame f = new Frame(); 
   Font font = new Font("Arial", Font.BOLD, 14);
   f.setFont(font);
   f.setSize(500,400); 
   f.add(new DesktopEEG()); 
   f.show();    
}  


  


}

This code

f.addWindowListener(this);

in your constructor, gives you the NullPointerException, f hasn't been initialized. its currently null.

As for the previous post I never worked with Frames, so I am not sure.

MasterGoGo. . is there any reason you're using Panel, Frame, Button, etc instead of JPanel, JFrame, JButton, etc? Reason being because I believe the latter are more up to date. As another note, variable names are by convention lowercase for the first letter, for example names that follow convention: reset, theResetButton, otherButton, etc.

Oh: And it would probably be easy to help you with the NullPointerException, but you need to post in CODE=Java tags, because I don't know what line number your NullPointerException is referring to.

I have something like this right now.

I'm experimenting with the ActionListener on the Reset JButton, but it doesn't seem to work when I click it.

I've tried different actionlistener methods
http://academicjava.com/Java_help/Tutorial_Examples_Swing_31.html
http://academicjava.com/Java_help/Tutorial_Examples_Swing_9.html
http://academicjava.com/Java_help/Tutorial_Examples_Swing_10.html

There's no compiling issues or run-time errors though.

Anyone know what's wrong?

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

public class DesktopEEG
{
  JFrame f;
  JTextField filename;
  JTextArea results;
  JPanel toppanel,centerpanel,bottompanel,centerpanel2,centerpanel3;

  public static void main (String [] args)
  {
    new DesktopEEG();
  }
  
  public DesktopEEG()
  {
    super();
    JFrame f = new JFrame();
    Container cp = f.getContentPane();
    cp.setLayout(new BorderLayout());
    
    
    f.addWindowListener(new WindowListener() 
    {
         public void windowClosing(WindowEvent e) 
         {
            System.out.println("Window Closing");
         }
         public void windowClosed(WindowEvent e) 
         {
            System.out.println("Window Closed");
         }
         public void windowIconified(WindowEvent e) 
         {
            System.out.println("Window Iconified");
         }
         public void windowDeiconified(WindowEvent e) 
         {
            System.out.println("Window Deiconified");
         }
         public void windowOpened(WindowEvent e) 
         {
            System.out.println("Window Opened");
         }
         public void windowActivated(WindowEvent e) 
         {
            System.out.println("Window Activated");
         }
         public void windowDeactivated(WindowEvent e) 
         {
            System.out.println("Window Deactivated");
         }
      });

    
    
    //top panel
    JPanel toppanel = new JPanel();
    toppanel.setLayout(new BorderLayout());
    JTextField filename = new JTextField(20);
    
    toppanel.add(new JLabel("Web EEG",JLabel.CENTER),BorderLayout.NORTH);
    toppanel.add((new JLabel("Enter the EEG file name (including full path e.g.:C:/trial1.txt):")),BorderLayout.CENTER);
    toppanel.add(filename,BorderLayout.SOUTH);

    cp.add(toppanel, BorderLayout.NORTH);
    
    Font font = new Font("", Font.PLAIN, 12);
    
    //center panel
    JPanel centerpanel = new JPanel();
    JPanel centerpanel2 = new JPanel();
    JPanel centerpanel3 = new JPanel();
    centerpanel.setLayout(new GridLayout (4, 3));
    centerpanel2.setLayout(new GridLayout(1,3));
    centerpanel3.setLayout(new BorderLayout());
    centerpanel.add(new JCheckBox("Channel 1")).setFont(font);
    centerpanel.add(new JCheckBox("Channel 5")).setFont(font);
    centerpanel.add(new JCheckBox("Channel 9")).setFont(font);
    centerpanel.add(new JCheckBox("Channel 2")).setFont(font);
    centerpanel.add(new JCheckBox("Channel 6")).setFont(font);
    centerpanel.add(new JCheckBox("Channel 10")).setFont(font);                  
    centerpanel.add(new JCheckBox("Channel 3")).setFont(font);                
    centerpanel.add(new JCheckBox("Channel 7")).setFont(font);                  
    centerpanel.add(new JCheckBox("Channel 11")).setFont(font);
    centerpanel.add(new JCheckBox("Channel 4")).setFont(font);                  
    centerpanel.add(new JCheckBox("Channel 8")).setFont(font);                  

    
    JButton Run = new JButton("Run"); 
    JButton Reset = new JButton("Reset");  

    
    centerpanel2.add(new JLabel(" "));
    centerpanel2.add(new JButton("Run"));
    centerpanel2.add(new JButton("Reset"));
    

    centerpanel3.add(new JLabel("Choose a channel to calculate power for:"), BorderLayout.NORTH);
    centerpanel3.add(centerpanel, BorderLayout.CENTER);
    centerpanel3.add(centerpanel2,BorderLayout.SOUTH);
    
    cp.add(centerpanel3, BorderLayout.CENTER);

    
[B][I]    Reset.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        System.out.println("yes");
      }
    });[/I][/B]
    
    
    //bottom panel
    JPanel bottompanel = new JPanel();
    bottompanel.setLayout(new BorderLayout());
    bottompanel.add(new JLabel("Results"),BorderLayout.NORTH);
    bottompanel.add(new JTextArea(5,1),BorderLayout.CENTER);
    bottompanel.add(new JLabel(" "), BorderLayout.SOUTH);
    
    cp.add(bottompanel, BorderLayout.SOUTH);

    
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    f.setSize(400,300);
    f.setVisible(true);
    
  }
  
  
}
JButton Reset = new JButton("Reset"); 
    centerpanel2.add(new JButton("Reset"));

Should be:

JButton Reset = new JButton("Reset"); 
    centerpanel2.add(Reset);

Again, follow coding conventions - it should be named "reset" not "Reset" - this would have made it easier to see this mistake. Anyway, simple mistake, your program should work now. You added the actionListener correctly, but you then created a completely different JButton which didn't have the actionListener attached. Anyway, what you should get out of this is to avoid assuming your problem lies in one area, always investigate all possibilities.

Comments
Fixed my problem

I seemed to have encountered a problem again.

the bold text indicates an error

Am I doing something wrong?

What I'm trying to do is clear the form.

reset.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        System.out.println("Reset works");
      [B]  filename.setText(" ");[/B]
      }
    });

I must've forgot, the error is:

local variable filename is accessed from within inner class; needs to be declared final

I must've forgot, the error is:

Hi
Anonymous inner classes require final variables because of the way they are implemented in Java. The anonymous inner class is using a copy. of the local variable. By declaring the variable final (means that the inner class can not change its value) it is guarantied that the inner class copy of a local variable will always match the actual value.

At java.sun.com you can read more about how it works, or try Google.

So probably you have declared the variable as private and that is why you get this message.

Hope this will help you =)

Hi
Anonymous inner classes require final variables because of the way they are implemented in Java. The anonymous inner class is using a copy. of the local variable. By declaring the variable final (means that the inner class can not change its value) it is guarantied that the inner class copy of a local variable will always match the actual value.

At java.sun.com you can read more about how it works, or try Google.

So probably you have declared the variable as private and that is why you get this message.

Hope this will help you =)

I'm still lost though. I didn't use private or protected at all.

If it is declared as public then you should not get this error I believe, strange, but
what happens if you declare the variable as final? Does it work then?

If you declare the variable as final, it will work. Otherwise you won't be able to access from within an inner class. And I'd be wary of taking sneaker's advice because I'm not sure that it is completely correct. You might want to do more research if you are concerned about the issue.

PS an IDE like Eclipse would have suggested to you that you should make the variable final, and all you would have had to do is click the link on their suggestion and it would have done it for you.

If you declare the variable as final, it will work. Otherwise you won't be able to access from within an inner class. And I'd be wary of taking sneaker's advice because I'm not sure that it is completely correct. You might want to do more research if you are concerned about the issue.

PS an IDE like Eclipse would have suggested to you that you should make the variable final, and all you would have had to do is click the link on their suggestion and it would have done it for you.

My advise was to make the variable final =)

When MasterGoGo answered that he/she still was lost I assumed that it did not work and wondered what happened when the variable was declared final.
Since the variable never was declared private or protected I just assumed it was public. I never use anonymous inner classes and ordinary inner classes takes public variables so I was merely expressing my confusion, it was not meant as an advice.

Sorry if I did express myself vaguely and thank you BestJewSinceJC for making it clearer =)

Sorry for the late response. I'll clarify, I was lost since I don't understand why I have to put final, so I just read up on it. The prof didn't cover much with us, all the extra stuff I had to google, go figure I paid tuition to teach myself haha.

I think I'm passed that point, I put final in front of my variable and it solved my problem, but I encountered a different problem.

Here is the code

run.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        FFT gg = new FFT();
        String filepath = filename.getText();
        int electrodeNum = 2;    //2 is arbitrary, had to initialize it
        
        if(channel1.isSelected() == true)
        {
          electrodeNum = 1;
        }
        
        
        gg.analyzeFile(filepath,electrodeNum);

Explaination: Basically, when my channel1 JCheckBox is checked(selected), the electrodeNum should be equal to 1, but for some reason, electrodeNum still equals to 2. No compiling or runtime issues though. How can I fix this so that when JCheckBox channel1 is checked, the electrodeNum will be equal to 1, and thus, I get the following

gg.analyzeFile(filepath,1); instead of gg.analyzeFile(filepath,2);

Explaination: Basically, when my channel1 JCheckBox is checked(selected), the electrodeNum should be equal to 1, but for some reason, electrodeNum still equals to 2. No compiling or runtime issues though. How can I fix this so that when JCheckBox channel1 is checked, the electrodeNum will be equal to 1, and thus, I get the following
gg.analyzeFile(filepath,1); instead of gg.analyzeFile(filepath,2);

I should first of all test if the event e is taken care of inside this method by putting a simple System.out.println() inside the if where you want electrodeNum to change to 1.

Think about where in this method you catch the event e.

Good luck =)

I should first of all test if the event e is taken care of inside this method by putting a simple System.out.println() inside the if where you want electrodeNum to change to 1.

Think about where in this method you catch the event e.

Good luck =)

Thanks for the tip. I added the system.out.println, it didnt even show up. I'm trying to fix it.

Anyone else have a clue?

haha quick update

I fixed my problem. It turns out that I created two of the same individual buttons, thus, my if statement not working.

I have a question though.

The code below shows up in my interaction pane in DrJava, but I want it to display in my JTextarea (called textResults). How do I go about doing that?

One idea I had was to put everything in a string, but won't everything end up in the same line? I will give it a try nonetheless.

System.out.println ("Electrode\tWaveType\tPower");
      System.out.println ("=======================================================");
      System.out.println (nElectrodeNumber + "\t\tDelta\t\t" +  htblDelta);
      System.out.println ("\t\tTheta\t\t" +  htblTheta);
      System.out.println ("\t\tAlpha\t\t" +  htblAlpha);
      System.out.println ("\t\tBeta\t\t" +  htblBeta);
      System.out.println ("\t\tGamma\t\t" +  htblGamma);

Hey guys, I'm passed my previous problem, anyone know how to use \n (newline) in a textarea?

String s = textResults.getText();
 textResults.setText(s+[B]"\n"[/B]line);  //  error with \n

Yeah - you can put \n in any String Object and it will do the same thing. Also, click 'Mark as Solved' once people solve your issues or once you work them out on your own.

@ Sneaker:

No problem, I was just making sure the OP was aware.

I worked my butt off and finished my program. I would like to thank everyone that helped me when I was stuck. =)

This question has already been answered. Start a new discussion instead.