[SOLVED BUT NEED HELP] Java:actionPerformed isn't performing

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

[SOLVED BUT NEED HELP] Java:actionPerformed isn't performing

Post by afbase »

ok this is my first java gui. Honestly, I can't figure out why my buttons do not perform. this is just a snippet of the code with constructor and all

Code: Select all

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

public class LoopPanel extends JFrame implements ActionListener {
private wine dionysus;
private book stine;
private cheese cheddar;
private int button_count = 0;
public final static boolean RIGHT_TO_LEFT = false;

private JFrame frame;
/*Wine*/
private JButton wineButt;

private JLabel [] wine_Label;
private JLabel [] wine_Label_copy;
private JLabel [] wine_Output;
private JTextField [] wine_Field;

/*book*/
private JButton bookButt;

private JLabel [] book_Label;
private JLabel [] book_Label_copy;
private JLabel [] book_Output;
private JTextField [] book_Field;

/*cheese*/
private JButton cheeseButt;

private JLabel [] cheese_Label;
private JLabel [] cheese_Label_copy;
private JLabel [] cheese_Output;
private JTextField [] cheese_Field;
////////////MAIN////////////////////////
public static void main(String[] args) {

        LoopPanel tpo = new LoopPanel();               // create 'the program object'

        tpo.addWindowListener(new WindowAdapter() {   // this exits the program when X box clicked
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });}

////////////CONSTRUCTOR/////////////////////

public LoopPanel ()  {
    dionysus = new wine();
    stine = new book();
    cheddar = new cheese();
  wine_Output = new JLabel[7];
  wine_Field =  new JTextField[3];
  wine_Label = new JLabel[3];
  wine_Label[0]= new JLabel("Name of the Wine:");
  wine_Label[1] = new JLabel("Origin of the Wine:");
  wine_Label[2] = new JLabel("Alcohol Content:");
  wine_Label_copy = wine_Label.clone();
 
  book_Output = new JLabel[7];
  book_Field = new JTextField[3];
  book_Label = new JLabel[3];
  book_Label[0] =new JLabel("Author of Book:");
  book_Label[1] = new JLabel("Title of the Book:");
  book_Label[2] = new JLabel("Number of Pages:");
  book_Label_copy = book_Label.clone();
  
  cheese_Output = new JLabel[7];
  cheese_Field = new JTextField[3];
  cheese_Label = new JLabel[3];
  cheese_Label[0] = new JLabel("Type of Cheese:");
  cheese_Label[1] = new JLabel("Color of the Cheese:");
  cheese_Label[2] = new JLabel("Weight of the Cheese:");
  cheese_Label_copy = cheese_Label.clone();

        frame = new JFrame("testing");
        addComponentsToPane(frame.getContentPane());
        shoutpane (frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
}
public void shoutpane (Container pane){
       /*if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(
                ComponentOrientation.RIGHT_TO_LEFT);
        }
        setLayout(new GridLayout(0,7));*/
        shout_panelize(wine_Output, wine_Label_copy, pane);
        shout_panelize(book_Output, book_Label_copy, pane);
        shout_panelize(cheese_Output, cheese_Label_copy, pane);
}
public void shout_panelize(JLabel[] output, JLabel[] label, Container pane){
	  int label_count = label.length;
	  int output_count = output.length;
	  int i = 0;
	    while (i < label_count){
	     //label[i] = new JLabel();
	     //output[i] = new JLabel();
	      //if (i > output.length){
	    	  label[i] = new JLabel();
	    	  output[i] = new JLabel();
	          pane.add(label[i]);
	          pane.add(output[i]);
	     /* }
	      else{
    	  output[i] = new JLabel();
	      pane.add(label[i]);
	      pane.add(output[i]);
	      }*/
	      i++;
	    }
	}

public void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(
            ComponentOrientation.RIGHT_TO_LEFT);
        }
  pane.setLayout(new GridLayout(0,7));
  panelize (wine_Label, wine_Field, wineButt, pane);
  panelize (book_Label, book_Field, bookButt, pane);
  panelize (cheese_Label, cheese_Field, cheeseButt, pane);

}


public void panelize (JLabel[] label , JTextField[] field, JButton silly, Container pane){

    int label_count = label.length;
    //int output_count = output.length;
   // int field_count = field.length;
    int i = 0;
      while (i < label_count){
        //label[i] = new JLabel();
    	field[i] = new JTextField("",7);
        pane.add(label[i]);
        pane.add(field[i]);
        i++;
      }
    silly= new JButton(String.valueOf(button_count));
    silly.setActionCommand(String.valueOf(button_count));
    //silly.setName();
    button_count++;
  	silly.addActionListener(this);
    pane.add(silly);
    }
public void actionPerformed(ActionEvent e) {

String s = e.getActionCommand();
String newtext;
int ant;
double whopper;
//System.out.println(e.getSource());
	if(e.getActionCommand() == String.valueOf(0) || e.getSource() == wineButt){
		Toolkit.getDefaultToolkit().beep();
		System.out.println(e.getSource());
		/*wine name*/
		//newtext = ;
		dionysus.setWine_name(wine_Field[0].getText());
		wine_Output[0].setText(dionysus.getWine_name());
		/*wine origin*/
		//newtext = wine_Field[1].getText();
		dionysus.setWine_origin(wine_Field[0].getText());
		wine_Output[1].setText(dionysus.getWine_origin());
		/*alcohol content*//*
		whopper = Double.parseDouble(wine_Field[2].getText());
		dionysus.setAlcohol_content(whopper);
		wine_Output[2].toString().setText(dionysus.toString().getAlcohol_content());*/
	
	}
}
}

Sorry, it is a very big snippet but I'm not sure as to why my actionPerformed will not work. I do have three other classes with get and set methods but that is all those classes contain with an empty constructor.

the problem I'm having is to click on a button ( the first button in the snippet), work, or do something. Any help is appreciated and a thanks in advance.
Last edited by afbase on Tue May 01, 2007 5:49 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

What does this do when you place view the console?

Code: Select all

public void actionPerformed(ActionEvent e) { 

System.out.println("I got here ok!");

String s = e.getActionCommand(); 
String newtext; 
int ant; 
double whopper; 
//System.out.println(e.getSource()); 
        if(e.getActionCommand() == String.valueOf(0) || e.getSource() == wineButt){ 
                Toolkit.getDefaultToolkit().beep(); 
                System.out.println(e.getSource()); 
                /*wine name*/ 
                //newtext = ; 
                dionysus.setWine_name(wine_Field[0].getText()); 
                wine_Output[0].setText(dionysus.getWine_name()); 
                /*wine origin*/ 
                //newtext = wine_Field[1].getText(); 
                dionysus.setWine_origin(wine_Field[0].getText()); 
                wine_Output[1].setText(dionysus.getWine_origin()); 
                /*alcohol content*//* 
                whopper = Double.parseDouble(wine_Field[2].getText()); 
                dionysus.setAlcohol_content(whopper); 
                wine_Output[2].toString().setText(dionysus.toString().getAlcohol_content());*/ 
        
        } 
} 
Do you get any errors/stack traces on the command line?
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Post by afbase »

When i click the first buuton, it should make a noise, kinda like that default error noise in windows and print "Swing JButton" or something of that extent onto the console. But really that is just a test to see if my if stament is being used.
if(e.getActionCommand() == String.valueOf(0) || e.getSource() == wineButt)
My code doesn't even recognize the action command. The place where I've put the buttons are in this method here:

Code: Select all

public void panelize (JLabel[] label , JTextField[] field, JButton silly, Container pane){

    int label_count = label.length;
    //int output_count = output.length;
   // int field_count = field.length;
    int i = 0;
      while (i < label_count){
        //label[i] = new JLabel();
        field[i] = new JTextField("",7);
        pane.add(label[i]);
        pane.add(field[i]);
        i++;
      }
    silly= new JButton(String.valueOf(button_count));
    silly.setActionCommand(String.valueOf(button_count));
    //silly.setName();
    button_count++;
        silly.addActionListener(this);
    pane.add(silly);
    } 

I'm not sure as to why this doesn't actionPerformed isn't listening. I might be using my JButton array or/and i'm using the " silly.addActionListener(this);" improperly but I'm not sure.
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

[solved]

Post by afbase »

Ok the big big thing that I didn't know why it was working was a subtle trick. Exhibit A:

Code: Select all

public void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(
            ComponentOrientation.RIGHT_TO_LEFT);
        }
  pane.setLayout(new GridLayout(0,7));
  panelize (wine_Label, wine_Field, wineButt, pane);
  panelize (book_Label, book_Field, bookButt, pane);
  panelize (cheese_Label, cheese_Field, cheeseButt, pane);

} 

Please correct me if I'm wrong but the problems are the buttons wineButt, bookButt, and cheeseButt are "passed by reference" and not "passed by value". Ya it took a buddy of mine about 10 minutes to figure out the problem :-)

The Solution

Code: Select all

public void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(
            ComponentOrientation.RIGHT_TO_LEFT);
        }
  pane.setLayout(new GridLayout(0,7));
  wineButt = panelize (wine_Label, wine_Field, wineButt, pane);
  bookButt = panelize (book_Label, book_Field, bookButt, pane);
  cheeseButt = panelize (cheese_Label, cheese_Field, cheeseButt, pane);

} 
The code works now, but I'd appreciate it if someone could explain why this makes it work in lamen terms. My buddy just solves problems, he doesn't really get into the nitty gritty as to why it works.
Post Reply