Solved

JButton

Posted on 2013-11-21
3
205 Views
Last Modified: 2013-11-22
I am having trouble using
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;


public class Market extends JFrame{
       
    public Market(){
         
            
    }
    
      public static void main(String[] args){
          
          float values[] = {98,62,82,69,45,12,98,69,87,85,100,96,97,98,99};
	  Person [] people = new Person[2];
	  people[0] = new Teacher("Test ", "Test", 98);
	  people[1] = new Student("Test2 ", "Test2", 45);
	  ((Student)people[1]).inputGradeList(values);
	  for (int i=0; i< people.length; ++i) System.out.println (people[i]);
      
      Market frame = new Market(); 
      frame.setTitle("Listing");
      frame.setSize(400, 400);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      
      
      JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1)); 
      p1.add(new JLabel("" + people[0]), BorderLayout.SOUTH);
      p1.setBackground(Color.BLACK);
      p1.setForeground(Color.PINK);
      p1.setBorder(new TitledBorder("Name"));  

      frame.add(p1);
      JButton jbtRight = new JButton("Next");
      jbtRight.setBackground(Color.BLACK);
      jbtRight.setForeground(Color.WHITE);
      p1.add(jbtRight);
      
      ActionListener listener1 = new NewListener();
      jbtRight.addActionListener(listener1);
 
    class NewListener implements ActionListener {
    public void actionPerformed(Action e)
    {
        for (int i=0; i < people.length; ++i)
            System.out.println (people[i]);
    }
      }
  }

Open in new window

JButton to advance through an array. Here is what I have.
0
Comment
Question by:lostinspace9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Expert Comment

by:jb1dev
ID: 39667573
If you wish to create you ActionListener like this:
        ActionListener listener1 = new NewListener();
Then you will need to put the class definition outside of the main() method. Either in another file, or you can make it an inner class in your Market class. If you make it an inner class, be sure it is defined as "static" otherwise you cannot instantiate it from within the static main() method.

(Alternatively, you can implement it as an anonymous inner class, providing the implementation within the main() method. I'm not sure if that's the purpose of this assignment, but I can provide info on how to do that as well.)

Then you will also need to define a constructor for NewListener() and pass it your "people" array. Store that in a member variable of your NewListener class, such that the actionPerformed() will have access to it, and be able to print it.

Also the actionPerformed() method takes an ActionEvent as parameter, not an Action.
http://docs.oracle.com/javase/7/docs/api//java/awt/event/ActionListener.html
You will need to change the method signature otherwise the compiler will complain that NewListener does not implement ActionListener.
0
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39667589
This should get you started.
I have removed / altered some of your main() method code and used a dummy Person class, since I do not have access to that class definition.

You should delete my Person class which is defined as an inner class in the Market class.
And you should change your main() method to use the appropriate Person/Teacher/Student class definitions which were presumably provided as part of the assignment.

So really this is only an example of how you might refactor your NewListener definition. And get it to compile.

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

public class Market extends JFrame{
       
    public Market(){
         
    }
   
    public static class Person {
        String s1, s2;
        int i1;
        public Person(String s1, String s2, int i1) {
            this.s1 = s1; 
            this.s2 = s2;
            this.i1 = i1;
        }
    }

    public static void main(String[] args){
          
        float values[] = {98,62,82,69,45,12,98,69,87,85,100,96,97,98,99};
        Person [] people = new Person[2];
        people[0] = new Person("Test ", "Test", 98);
        people[1] = new Person("Test2 ", "Test2", 45);
        // ((Student)people[1]).inputGradeList(values);

        for (int i=0; i< people.length; ++i) System.out.println (people[i]);
      
        Market frame = new Market(); 
        frame.setTitle("Listing");
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        
        JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1)); 
        p1.add(new JLabel("" + people[0]), BorderLayout.SOUTH);
        p1.setBackground(Color.BLACK);
        p1.setForeground(Color.PINK);
        p1.setBorder(new TitledBorder("Name"));  
    
        frame.add(p1);
        JButton jbtRight = new JButton("Next");
        jbtRight.setBackground(Color.BLACK);
        jbtRight.setForeground(Color.WHITE);
        p1.add(jbtRight);
        
        ActionListener listener1 = new NewListener(people);


        jbtRight.addActionListener(listener1);
    
    }

    public static class NewListener implements ActionListener {
        Person[] m_people;
        public NewListener(Person[] people) {
            this.m_people = people;
        }

        public void actionPerformed(ActionEvent e)
        {
            for (int i=0; i < m_people.length; ++i)
                    System.out.println (m_people[i]);
        }
    }

}

Open in new window

0
 

Author Comment

by:lostinspace9
ID: 39669263
This is great! Thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 67
Html Table Looping (part 2) 5 41
junit example issue 2 55
convert Absolute path to relative path of FTP location 7 50
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question