• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Handling Java Swing JButton Events outside of the Class it's being implemented in

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having issues passing the implements ActionClass over to another class in order to add ActionListeners to the buttons.



I have 7 classes, I need to implement the ActionListener in ControlPanel, what I need to happen is any time any of the student buttons are pressed I need the GPA to recalculate and display on the same button and then recalculate the AverageGPA.



Heres my code:

app.java

public class app {



`public static void main(String args[]) {`

   `MainFrame mjf = new MainFrame();`

`}`

}



Mainframe.java

import java.awt.*;

import javax.swing.*;



public class MainFrame extends JFrame {



`ControlPanel mjp;`



`public MainFrame() {`

   `super("Assignment 05 Starter");`

   `mjp = new ControlPanel();`

   `getContentPane().add(mjp, "Center");`

   `setDefaultCloseOperation(EXIT_ON_CLOSE);`

   `setSize(800, 600);`

   `setVisible(true);`

`}`

}



ControlPanel.java

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.net.NetworkInterface;



import javax.swing.*;



public class ControlPanel extends JPanel implements ActionListener {



`CenterPanel cp;`

`TopPanel tp1;`

`group g1;`

`JButton bt1, bt2, bt3, bt4;`

`student st1, st2, st3, st4;`



`public ControlPanel() {`

   `super();`

   `BorderLayout border = new BorderLayout();`

   `setLayout(border);`

   `setBackground(Color.gray);`

   `g1 = new group("Semester 1 Students");`

   `st1 = new student("Mike", "Myers");`

   `st2 = new student("Bill", "Moore");`

   `st3 = new student("Tara", "Smith");`

   `st4 = new student("Greg", "Wilson");`



   `g1.getStudentgrouplist().add(st1);`

   `g1.getStudentgrouplist().add(st2);`

   `g1.getStudentgrouplist().add(st3);`

   `g1.getStudentgrouplist().add(st4);`

   `cp = new CenterPanel(st1, st2, st3, st4);`

   `tp1 = new TopPanel(g1);`

   `add(tp1, "North");`

   `add(cp, "Center");`



   `cp.jb1.addActionListener(this);`

   `cp.jb2.addActionListener(this);`

   `cp.jb3.addActionListener(this);`

   `cp.jb3.addActionListener(this);`



`}`



`public group getG1() {`

   `return g1;`

`}`



`public void setG1(group g1) {`

   `this.g1 = g1;`

`}`



`@Override`

`public void actionPerformed(ActionEvent e) {`

   `if(e.getSource() == cp.getJb1());`

   `{`



   `}`



`}`



}



CenterPanel.java

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.*;



public class CenterPanel extends JPanel {



`JButton jb1, jb2, jb3, jb4;`



`public CenterPanel(student st1, student st2, student st3, student st4) {`



   `super();`

   `GridLayout grid = new GridLayout(4, 1);`

   `setLayout(grid);`

   `setBackground(`[`Color.green`](https://Color.green)`);`

   `jb1 = new JButton();`

   `jb1.setText(st1.getInfo());`

   `jb2 = new JButton();`

   `jb2.setText(st2.getInfo());`

   `add(jb2);`

   `jb3 = new JButton();`

   `jb3.setText(st3.getInfo());`

   `add(jb3);`

   `jb4 = new JButton(st4.getInfo());`

   `jb4.setText(st4.getInfo());`

   `add(jb4);`



`}`

`public JButton getJb1() {`

   `return jb1;`

`}`



`public void setJb1(JButton jb1) {`

   `this.getJb1();`

`}`



`public JButton getJb2() {`

   `return jb2;`

`}`



`public void setJb2(JButton jb2) {`

   `this.getJb2();`

`}`



`public JButton getJb3() {`

   `return jb3;`

`}`



`public void setJb3(JButton jb3) {`

   `this.getJb3();`

`}`



`public JButton getJb4() {`

   `return jb4;`

`}`



`public void setJb4(JButton jb4) {`

   `this.getJb4();`

`}`

}



TopPanel.java

import java.awt.*;

import java.text.Format;

import java.util.ArrayList;



import javax.swing.*;



public class TopPanel extends JPanel {



`private group g1;`



`JButton jb1, jb2, jb3;`



`public TopPanel(group g1) {`

   `super();`

   `this.g1 = g1;`

   `setBackground(Color.gray);`

   `;`

   `jb1 = new JButton();`

   `ImageIcon psu = new ImageIcon("images/psu.jpg");`

   `jb1.setIcon(psu);`

   `jb1.setBackground(Color.white);`

   `add(jb1);`

   `jb2 = new JButton(g1.getGroupName());`

   `jb2.setBackground(Color.white);`

   `add(jb2);`

   `jb3 = new JButton(String.format("%.2f", g1.groupAvg()));`

   `jb3.setBackground(Color.white);`

   `add(jb3);`



`}`



`public JButton getJb1() {`

   `return jb1;`

`}`



`public void setJb1(JButton jb1) {`

   `this.jb1 = jb1;`

`}`



`public JButton getJb2() {`

   `return jb2;`

`}`



`public void setJb2(JButton jb2) {`

   `this.jb2 = jb2;`

`}`



`public JButton getJb3() {`

   `return jb3;`

`}`



`public void setJb3(JButton jb3) {`

   `this.jb3 = jb3;`

`}`



`public group getG1() {`

   `return g1;`

`}`



`public void setG1(group g1) {`

   `this.g1 = g1;`



`}`

}



student.java

public class student {

`String firstName;`

`String lastName;`

`double semesterGPA;`



`public student(String stFirstName, String stLastName) {`

   `firstName = stFirstName;`

   `lastName = stLastName;`

   `double gpa = Math.random();`

   `semesterGPA = (double) (gpa * 4.0);`

`}`



`public String getFirstName() {`

   `return firstName;`

`}`



`public void setFirstName(String firstName) {`

   `this.firstName = firstName;`

`}`



`public String getLastName() {`

   `return lastName;`

`}`



`public void setLastName(String lastName) {`

   `this.lastName = lastName;`

`}`



`public double getSemesterGPA() {`

   `return semesterGPA;`

`}`



`public void setSemesterGPA(double semesterGPA) {`

   `this.semesterGPA = semesterGPA;`

`}`



`public String getInfo() {`

   `return (getFirstName() + " " + getLastName() + " " + String.format("%.2f", getSemesterGPA()));`

`}`



}



group.java

import java.util.ArrayList;



public class group {



`String groupName;`

`ArrayList<student> studentgrouplist;`



`public group(String groupName1) {`

   `groupName = groupName1;`

   `studentgrouplist = new ArrayList<>();`

`}`



`public String groupList() {`

   `String info = "";`

   `for (int i = 0; i < studentgrouplist.size(); i++) {`

       `this.getStudentgrouplist().get(i);`

       `student temp = this.getStudentgrouplist().get(i);`

       `info = info + "\n" + temp.getFirstName() + " " + temp.getLastName() + ": " + temp.getSemesterGPA();`

   `}`

   `return getGroupName() + info;`

`}`



`public double groupAvg() {`

   `double total = 0;`

   `for (int i = 0; i < studentgrouplist.size(); i++) {`

       `total = total + this.getStudentgrouplist().get(i).getSemesterGPA();`

   `}`

   `return total / studentgrouplist.size();`

`}`



`public String getGroupName() {`

   `return groupName;`

`}`



`public void setGroupName(String groupName) {`

   `this.groupName = groupName;`

`}`



`public ArrayList<student> getStudentgrouplist() {`

   `return studentgrouplist;`

`}`



`public void setStudentgroup(ArrayList<student> studentgrouplist) {`

   `this.studentgrouplist = studentgrouplist;

`

`}`

}



}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic