Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


Linking a button to another class


Hey peeps, i have created a programme, and it has 4 classes. The 4th class i made was the interface and that works fine now as i figured out how to get it to display using the action listner tutorial on this site. Now i am really stuck though,   on the interface i have a button, and when i click it at the moment i have it to display a message that i have written, but i dont want that to display in my text area. what i want is for the text area to  store the information that is printed from another class (the infroamtion in the other class is a generated match list - im using bluej for this)  for example, the other class "generate match list" generates a set of fixtures and prints them out in blue j, how do i link my button to this class so when its compiled it displays in the text area, i hope this all makes sence  thanks for your time (hope i made sence)

i think you mean that you want the button to perform differently from different classes?  if so, just override the ActionListener of the button in whichever class implements it.

no, not exactly (well i dont think so, please bear with me as i am really struggling with java)  i have 4 classes, one is student, the other is student details, the next is a generate match list and the last is my interface  the generate match list class checks some conditions from the other classes and pairs people up to play each other, this prints out the match list on the screen when compiled,   what i want to do is in the interface class, when the user clicks generate match list button, the generate match list class compiles, prints the results and displays them in to my text box?   is that a little clearer? sorry, as previously said, im struggling massivley

give your interface a copy of the generate match list class, and call whatever method you need in it in your actionlistener. You could also create the generate match list class from your interface.  class GUI {     GenerateMatchList gml = new GenerateMatchList();       //other interface code       //in your action listener:     gml.generate(); }   class GenerateMatchList {     public void generate() { 	//your code here     } } 

ok, well first, let's correct your terminology.  "compile" is what happens before you execute your program at all.  you want the button action in the interface to create the generateMatch class, correct? Question, does the generateMatch implement the interface?  like: class GenerateMatch implements myInterface{ ... }

JacobsB wrote: ok, well first, let's correct your terminology.  "compile" is what happens before you execute your program at all.  you want the button action in the interface to create the generateMatch class, correct? Question, does the generateMatch implement the interface?  like: class GenerateMatch implements myInterface{ ... }  I'm 90% sure that when he says 'interface', he means a GUI, and not the Java definition. Another terminology issue.

If GenerateMatch class doesn't implement myInterface, then the above solution is correct.    But if it does, the proper thing to do is override the interface button's actionListener inside the GenerateMatch class.

ok well here is the code i have for the two classes,  generate match list class  public class Matchlist {     private studentdetails sd= new studentdetails();         public Matchlist()     {              }         public void CreateMatch()         {         sd.createstudentdetails();                  for(int g=0;g<4;g++)//g = game         {             for(int y=0; y<18; y++)//y = green house             {                 if (sd.getgh(y).getGame(g)==-1)                 {                      for(int x=0;x<18;x++) //x = yellow house                 {       if (sd.getyh(x).getGame(g)==-1){                         if(sd.getgh(y).getC_lass()!=sd.getyh(x).getC_lass()){                         sd.getyh(x).changeGame(g,x);                          sd.getgh(y).changeGame(g,y);                         sd.getyh(x).changeOponent(y);                          sd.getgh(y).changeOponent(x);                         System.out.println(sd.getyh(x).getName()+" vs "+sd.getgh(y).getName());                     }                             }                         }                     }                 }             }         }     }   this is the class for the interface  import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;   public class MyPanel extends JPanel implements ActionListener {   //add the "implements ActionListener" line so the class knows its going to take an input     private JButton btngenerate;     private JTextArea txtbdisplaymatch;     private JLabel lblvs;     private JLabel lblgreenhouse;     private JLabel lblyellowhouse;     private JLabel lblname;     private JTextArea txtbgreenentry;     private JTextField txtbyellowentry;     private JTextArea txtbenterscoreg;     private JTextField txtbenterscorey;     private JButton btnfinalresults;       public MyPanel() {         //construct components         btngenerate = new JButton ("Generate Match List");         txtbdisplaymatch = new JTextArea (5, 5);         lblvs = new JLabel (" vs");         lblgreenhouse = new JLabel (" Green House");         lblyellowhouse = new JLabel (" Yellow House");         lblname = new JLabel (" Name");         txtbgreenentry = new JTextArea (5, 5);         txtbyellowentry = new JTextField (5);         txtbenterscoreg = new JTextArea (5, 5);         txtbenterscorey = new JTextField (5);         btnfinalresults = new JButton ("View Final Results");           //adjust size and set layout         setPreferredSize (new Dimension (542, 542));         setLayout (null);           //add components         add (btngenerate);         add (txtbdisplaymatch);         add (lblvs);         add (lblgreenhouse);         add (lblyellowhouse);         add (lblname);         add (txtbgreenentry);         add (txtbyellowentry);         add (txtbenterscoreg);         add (txtbenterscorey);         add (btnfinalresults);           //set component bounds (only needed by Absolute Positioning)         btngenerate.setBounds (185, 25, 165, 20);         txtbdisplaymatch.setBounds (60, 70, 415, 320);         lblvs.setBounds (285, 415, 25, 25);         lblgreenhouse.setBounds (120, 415, 100, 25);         lblyellowhouse.setBounds (385, 415, 100, 25);         lblname.setBounds (30, 445, 45, 25);         txtbgreenentry.setBounds (75, 445, 155, 20);         txtbyellowentry.setBounds (355, 445, 155, 20);         txtbenterscoreg.setBounds (235, 445, 20, 20);         txtbenterscorey.setBounds (330, 445, 20, 20);         btnfinalresults.setBounds (185, 495, 205, 25);   		//add action listener 		btngenerate.addActionListener(this);    //use the components name and use the ".addActionListener()" method to enable this component to take inputs     }   	public void actionPerformed(ActionEvent e){	  //create a method called actionPerformed, this method will contain the code which is run when the component is pressed. 		String a = "This is a test...";	   //create a string variable called "a" and store some text in it. 		if(e.getSource() == btngenerate){ //use the getSource() method and use it to see if it is equal to the compoenent. 			txtbdisplaymatch.setText(a);  //use the setText() method to add text from variable a to the txtbdisplaymatch which will display in the text area when the generate button is pressed. 		} 	}       public static void main (String[] args) {         JFrame frame = new JFrame ("MyPanel");         frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);         frame.getContentPane().add (new MyPanel());         frame.pack();         frame.setVisible (true);     }   }   so no in answering the question it doesnt implement my interface (the generate match class), so according to the code above what changes would i need to make? if you need any of the other 2 classes just say and i will post them  thanks for trying to help me peeps

I already showed you the code you need to add. Just change the class names in my code to MyPanel and MatchList. Your MyPanel class needs to have some sort of MatchList object, whether it creates it's own or gets passed one in its constructor.

yes, then newark was correct  import java.awt.*;* *import java.awt.event.*; import javax.swing.*;* *import javax.swing.event.*;   public class MyPanel extends JPanel implements ActionListener {   //add the "implements ActionListener" line so the class knows its going to take an input     private JButton btngenerate;     private JTextArea txtbdisplaymatch;     private JLabel lblvs;     private JLabel lblgreenhouse;     private JLabel lblyellowhouse;     private JLabel lblname;     private JTextArea txtbgreenentry;     private JTextField txtbyellowentry;     private JTextArea txtbenterscoreg;     private JTextField txtbenterscorey;     private JButton btnfinalresults;       private MatchList;       public MyPanel() {         //construct components         btngenerate = new JButton ("Generate Match List");         txtbdisplaymatch = new JTextArea (5, 5);         lblvs = new JLabel (" vs");         lblgreenhouse = new JLabel (" Green House");         lblyellowhouse = new JLabel (" Yellow House");         lblname = new JLabel (" Name");         txtbgreenentry = new JTextArea (5, 5);         txtbyellowentry = new JTextField (5);         txtbenterscoreg = new JTextArea (5, 5);         txtbenterscorey = new JTextField (5);         btnfinalresults = new JButton ("View Final Results");           //adjust size and set layout         setPreferredSize (new Dimension (542, 542));         setLayout (null);           //add components         add (btngenerate);         add (txtbdisplaymatch);         add (lblvs);         add (lblgreenhouse);         add (lblyellowhouse);         add (lblname);         add (txtbgreenentry);         add (txtbyellowentry);         add (txtbenterscoreg);         add (txtbenterscorey);         add (btnfinalresults);           //set component bounds (only needed by Absolute Positioning)         btngenerate.setBounds (185, 25, 165, 20);         txtbdisplaymatch.setBounds (60, 70, 415, 320);         lblvs.setBounds (285, 415, 25, 25);         lblgreenhouse.setBounds (120, 415, 100, 25);         lblyellowhouse.setBounds (385, 415, 100, 25);         lblname.setBounds (30, 445, 45, 25);         txtbgreenentry.setBounds (75, 445, 155, 20);         txtbyellowentry.setBounds (355, 445, 155, 20);         txtbenterscoreg.setBounds (235, 445, 20, 20);         txtbenterscorey.setBounds (330, 445, 20, 20);         btnfinalresults.setBounds (185, 495, 205, 25);   		//add action listener 		btngenerate.addActionListener(this);    //use the components name and use the ".addActionListener()" method to enable this component to take inputs     }   	public void actionPerformed(ActionEvent e){	  //create a method called actionPerformed, this method will contain the code which is run when the component is pressed. 		String a = "This is a test...";	   //create a string variable called "a" and store some text in it. 		if(e.getSource() == btngenerate){ //use the getSource() method and use it to see if it is equal to the compoenent. 			txtbdisplaymatch.setText(a);  //use the setText() method to add text from variable a to the txtbdisplaymatch which will display in the text area when the generate button is pressed.   matchList.CreateMatch(); 		} 	}       public static void main (String[] args) {         JFrame frame = new JFrame ("MyPanel");         frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);         frame.getContentPane().add (new MyPanel());         frame.pack();         frame.setVisible (true);   matchList = new MatchList();       }   }   Edited by: JacobsB on Mar 3, 2008 12:12 PM


Related Links

Java TM Runtime
boolean or Boolean
how to prevent timer from repeats
ear file, war file , and jar file.
how to change jvm memory heap using command line
How to load images and set a download directory for an online applet?
Build an XML file from a text file
What does this code mean?
My java program runs fine even if i don't specify access specifier of class
Could not find the main class.
A JTable Question?
Receiving infinity when running code.
java equivalent of ado components
Strange new issue
SET into string array using toArray()
What is going on?