Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 10th, 2007, 6:03 PM   #1
Intimidat0r
Hobbyist Programmer
 
Intimidat0r's Avatar
 
Join Date: May 2005
Location: Don't know, but the padded walls are a nice touch.
Posts: 126
Rep Power: 0 Intimidat0r is an unknown quantity at this point
Send a message via ICQ to Intimidat0r Send a message via AIM to Intimidat0r Send a message via MSN to Intimidat0r Send a message via Yahoo to Intimidat0r
Having difficulty with Swing.

I'm just starting out with learning about Swing, and I'm running into some trouble already. Basically what's happening is I'm trying to make an array of 9 buttons (well a 3x3 array) but, well, let me show you:

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

public class guiTest implements ActionListener { //error 1
	private static JButton[] bbox;

	public static void main(String args[]) {
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3,3));
		bbox = new JButton[9];
		for (int h = 0; h < 9; h++) {
			panel.add(bbox[h]);
			bbox[h].addActionListener(this); //error 2
		}
		Container c = getContentPane(); //error 3
		c.add(panel, BorderLayout.CENTER);
		JFrame f = new JFrame("GUI Test!!!");
		f.add(c);
		}
	}
}

I've commented the three lines in which the following three errors are reported by the compiler:

guiTest is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class guiTest implements ActionListener {
       ^
non-static variable this cannot be referenced from a static context
            bbox[h].addActionListener(this);
                                                  ^
cannot find symbol
symbol  : method getContentPane()
location: class TicTacToe
        Container c = getContentPane();
                              ^

Any help? Thanks in advance!
__________________
Children in the dark cause accidents, and accidents in the dark cause children.

http://www.ronincoders.org
Intimidat0r is offline   Reply With Quote
Old Mar 10th, 2007, 7:27 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Why are you inheriting from ActionListener?
Arevos is offline   Reply With Quote
Old Mar 10th, 2007, 8:29 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
No offense, but the multitude of fundamental problems with your code most likely indicates that your understanding of the Java language is insufficient to be working with GUIs. Instead of taking the time to write out an explanation for each problem, I rewrote the code for you to study.

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

public class GUITest extends JFrame implements ActionListener {
    private static JButton[] bbox;
    
    public GUITest() {
        
        super("GUI Test");
        
        setLayout(new GridLayout(3,3));
        
        bbox = new JButton[9];
        
        for (int i = 0; i < bbox.length; i++) {
            bbox[i] = new JButton(Integer.toString(i));
            bbox[i].addActionListener(this);
            add(bbox[i]);
        }
        
        pack();
        
        setVisible(true);
        
    }

    public static void main(String args[]) {
        new GUITest();
    }

    public void actionPerformed(ActionEvent evt) {
        // This method is called when a button is clicked
    }
    
}

Last edited by titaniumdecoy; Mar 10th, 2007 at 8:48 PM.
titaniumdecoy is offline   Reply With Quote
Old Mar 10th, 2007, 10:36 PM   #4
Intimidat0r
Hobbyist Programmer
 
Intimidat0r's Avatar
 
Join Date: May 2005
Location: Don't know, but the padded walls are a nice touch.
Posts: 126
Rep Power: 0 Intimidat0r is an unknown quantity at this point
Send a message via ICQ to Intimidat0r Send a message via AIM to Intimidat0r Send a message via MSN to Intimidat0r Send a message via Yahoo to Intimidat0r
Thanks. I am pretty new to Java. Let me ask you one more thing? Is there a way (presumably in actionPerformed()) to tell which button of the array was clicked? Or do I have to use a different method for each one?
__________________
Children in the dark cause accidents, and accidents in the dark cause children.

http://www.ronincoders.org
Intimidat0r is offline   Reply With Quote
Old Mar 10th, 2007, 10:42 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I prefer to use the getSource() method of the ActionEvent object:

public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == bbox[0]) {
        // bbox[0] was pressed
    }
}
You can get the name of the button with the getActionCommand() method, but remember that if you change the button's text, that change will be reflected in the value returned by this function.

public void actionPerformed(ActionEvent evt) {
    if (evt.getActionCommand().equals("0")) {
        // The button labeled "0" was pressed
    }
}
titaniumdecoy is offline   Reply With Quote
Old Mar 10th, 2007, 10:48 PM   #6
Intimidat0r
Hobbyist Programmer
 
Intimidat0r's Avatar
 
Join Date: May 2005
Location: Don't know, but the padded walls are a nice touch.
Posts: 126
Rep Power: 0 Intimidat0r is an unknown quantity at this point
Send a message via ICQ to Intimidat0r Send a message via AIM to Intimidat0r Send a message via MSN to Intimidat0r Send a message via Yahoo to Intimidat0r
Alright, thanks man.
__________________
Children in the dark cause accidents, and accidents in the dark cause children.

http://www.ronincoders.org
Intimidat0r is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SWING stuff... dsbabyGurl Java 6 Oct 27th, 2006 10:45 PM
Java Swing FocusTraversalPolicy hemanth.balaji Java 0 May 11th, 2006 11:25 PM
Swing || AWT Tutorials Java_König Java 2 Jan 28th, 2006 11:19 PM
ASCII conversion loop difficulty crmpicco Visual Basic 2 May 3rd, 2005 1:55 PM
Swing books? Mjordan2nd Java 0 Mar 2nd, 2005 8:50 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC