5 Replies - 273 Views - Last Post: 06 March 2013 - 09:15 AM Rate Topic: -----

#1 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Callin class from J button Click

Posted 28 February 2013 - 06:06 AM

See code below for JBUTTON addpatient. The code below the button code is for a class i created to display the addpatient GUI. WHat code do I need to add to addnew button to get the addpatient form to fire up.
//Add New Patient Button
                JButton addNew = new JButton("Add Patient");
                editandSearchOptionsPanel.add( addNew );
                addNew.addActionListener( new ActionListener()
                {
                	public void actionPerformed(ActionEvent add) 
                    {
                      // JOptionPane.showMessageDialog(null, "Adding New Patient") ;
                        frame.validate();
                       // addPatientgui addPatient = new addPatientgui();
                 }

------------------------------------------------------
 public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
    	
    	
    	//Set look and feel to NImbus.
    	
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception  UnsupportedLookAndFeelException) {
		    // If Nimbus is not available
			UIManager.getSystemLookAndFeelClassName();
		}
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


Sorry i did not select all code for addpatientgui. See full class now.
 
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.TextArea;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
 
public class addPatientgui {
    public static void addComponentsToPane(Container pane) {
        pane.setLayout(null);
 
        //JFrame addPatientgui = new JFrame("Pats details"); 
        //addPatientgui.setBorder( new TitledBorder("Enter new patient details") );
       // addPatientgui.setSize(1024,768);
        JLabel lmain,lpi,lname,ladd,ltel,lbloodGrp,ldob,lgender,lhis,lppsno,lcur,lwork,lworkfrom,lworkto,lfee;
    	JTextField patientNameField, adressField,tfname,PatientNoField,ppsField,dobfield,tfworkf,tfworkt,tffee;
    	TextArea taadd,tacur,hisField;
    	JButton addBut,clearBut;
    	Choice chbg;
    	CheckboxGroup cbmf;
    	Checkbox cbm,cbf;
        
       lmain = new JLabel("Patient Details");
    	lmain.setBounds(440,35,107,15);
    	pane.add(lmain);   
        
    	

    	lpi=new JLabel("Patient Information");
    	lpi.setBounds(40,70,120,15);
    	pane.add(lpi);

    	lname=new JLabel("Name :");
    	lname.setBounds(104,97,70,25);
    	pane.add(lname);

    	
    	patientNameField = new JTextField();
    	//patientNameField.setPreferredSize(new Dimension(70, 50));
    	//patientNameField.setHorizontalAlignment(0);
    	//patientNameField.setMaximumSize( patientNameField.getPreferredSize() );
    	pane.add(patientNameField);
    	patientNameField.setBounds(270,97,100,25);
    	
    	

    	ladd=new JLabel("Address :");
    	ladd.setBounds(104,138,70,15);
    	pane.add(ladd);
    	
    	
    	adressField = new JTextField();
    	pane.add(adressField);
    	adressField.setBounds(270,138,250,100);
    	
        
        
    	ltel=new JLabel("TEL: :");
    	ltel.setBounds(575,138,50,25);
    	pane.add(ltel);

    	lppsno=new JLabel("PPS No.:");
    	lppsno.setBounds(570,97,70,25);
    	pane.add(lppsno);
    	
    	ppsField = new JTextField();
    	pane.add(ppsField);
    	ppsField.setBounds(643,97,250,25);
    	
    	
    	PatientNoField = new JTextField();
    	pane.add(PatientNoField);
    	PatientNoField.setBounds(640,138,150,25);
    	
    	

    	lbloodGrp=new JLabel("Blood Group :");
    	lbloodGrp.setBounds(104,306,79,15);
    	pane.add(lbloodGrp);
    	

    	chbg=new Choice();
    	chbg.setBounds(270,306,53,15);
    	chbg.addItem("A -ve");
    	chbg.addItem("A +ve");
    	chbg.addItem("B -ve");
    	chbg.addItem("B +ve");
    	chbg.addItem("AB -ve");
    	chbg.addItem("AB +ve");
    	chbg.addItem("O +ve");
    	chbg.addItem("O -ve");
    	pane.add(chbg);
    	
    	ldob=new JLabel("Date of Birth :");
    	ldob.setBounds(575,306,135,15);
    	pane.add(ldob);

    	dobfield=new JTextField(15);
    	dobfield.setBounds(720,305,80,20);
    	pane.add(dobfield);
    	
    	lgender=new JLabel("Gender :");
    	lgender.setBounds(596,223,50,15);
    	pane.add(lgender);


    	
    	cbmf=new CheckboxGroup();
    	cbm=new Checkbox("Male",cbmf,true);
    	cbf=new Checkbox("Female",cbmf,false);
    	cbm.setBounds(698,223,50,15);
    	pane.add(cbm);
    	cbf.setBounds(760,223,60,15);
    	pane.add(cbf);
    	
    	
    	
    	lhis=new JLabel("History :");
    	lhis.setBounds(104,365,50,15);
    	pane.add(lhis);

    	hisField=new TextArea();
    	hisField.setBounds(270,365,250,100);
    	pane.add(hisField);

    	lcur=new JLabel("Current Problem :");
    	lcur.setBounds(575,365,100,15);
    	pane.add(lcur);
    	
    	tacur=new TextArea();
    	tacur.setBounds(720,365,250,100);
    	pane.add(tacur);
    	
    	clearBut=new JButton("CLEAR");
    	clearBut.setBounds(400,643,100,30);
    	pane.add(clearBut);

    	addBut=new JButton("ADD");
    	addBut.setBounds(600,643,100,30);
    	pane.add(addBut);
       
    }
 
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame addPatientgui = new JFrame("Add Patient Form");
        addPatientgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Set up the content pane.
        addComponentsToPane(addPatientgui.getContentPane());
 
        //Size and display the window.
        
        addPatientgui.setSize(1024,768);
        addPatientgui.setVisible(true);
    }
 
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
    	
    	
    	//Set look and feel to NImbus.
    	
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception  UnsupportedLookAndFeelException) {
		    // If Nimbus is not available
			UIManager.getSystemLookAndFeelClassName();
		}
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Callin class from J button Click

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Callin class from J button Click

Posted 28 February 2013 - 07:29 AM

Just looking at methods and not code you just have to make a static call to createAndShowGUI.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Callin class from J button Click

Posted 28 February 2013 - 09:11 AM

orrect terminolgy
you can't call a class you call a method which resides in a class if it is static or belongs to an instance of a class (object)

And I don't see why AddPatientGUI should have static methods
If you want you can create an instance of that class, make the GUI setVisible(false) and toggle its visibility when required.
The class that has the actionPerformed() can receive that object as parameter to its constructor so the actionPerformed() can access it
Was This Post Helpful? 1
  • +
  • -

#4 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Callin class from J button Click

Posted 01 March 2013 - 04:52 AM

It works by placing the following line of code in the addpatient button.
addPatientgui.createAndShowGUI();
Is this the correct way to have these talking to each other PBL or is the logic incorrect?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Callin class from J button Click

Posted 01 March 2013 - 12:14 PM

This is not the way I would do it at all

I would have

class AddPatientGui extends JFrame {

    AddPatientGui() {   // constructor
      .. create all the elements
      .. add all the component
      setDeFualtCloseOperation(...
      setSize(....
      ....  but NOT setVisible(true);
    }
}



Then in the class method that creates the class ABC that has the actionPerformed()

      AddPatientGui apg = new AddPatientGui();
      ABC abc = new ABC(apg);



class ABC {
   
      JFrame addPatientGui;
      
      // constructor
      ABC(JFrame addPatientGui) {
         this.addPatientGui = addPatientGui;
         ...
      }

      void actionPerformed(ActionEvent e) {
         ...
         addPatientGui.setVisible(true);



You don't need static method in AddPatientGui
Was This Post Helpful? 1
  • +
  • -

#6 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Callin class from J button Click

Posted 06 March 2013 - 09:15 AM

I still cant get it to work that way. The code below resides in DocAppMainGUI for the addbutton.
addNew.addActionListener( new ActionListener()
                {
                	public void actionPerformed(ActionEvent add) 
                    {
                       JOptionPane.showMessageDialog(null, "Adding New Patient") ;
                        frame.validate();
                        addPatientgui.createAndShowaddPatientGUI();
                        
                        addPatientgui apg = new addPatientgui();
                      
                        addPatientgui.setVisible(true);
                     }
                } );


This is the code for addPatientgui

public class addPatientgui 
{
    public static void addComponentsToPane( Container pane) 
    {
        pane.setLayout(null);
        
        
        
        try
		{
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
		    {
		        if ("Nimbus".equals(info.getName())) 
		        {
		            UIManager.setLookAndFeel(info.getClassName());
		            
		            break;
		        }
		    }
		} 
		catch (Exception  UnsupportedLookAndFeelException) 
		{
		    // If Nimbus is not available
			UIManager.getSystemLookAndFeelClassName();
		}
        
 
        
        JLabel lmain,lpi,lname,ladd,ltel,lbloodGrp,ldob,lgender,lhis,lppsno,lcur,lwork,lworkfrom,lworkto,lfee;
    	JTextField patientNameField, adressField,tfname,PatientNoField,ppsField,dobfield,tfworkf,tfworkt,tffee;
    	JTextArea taadd,tacur,hisField;
    	JButton addBut,clearBut, cancelBut;
    	Choice chbg;
    	CheckboxGroup cbmf;
    	Checkbox cbm,cbf;
        
       lmain = new JLabel("Patient Details");
    	lmain.setBounds(440,35,107,15);
    	pane.add(lmain);   
        
    	

    	lpi=new JLabel("Patient Information");
    	lpi.setBounds(40,70,120,15);
    	pane.add(lpi);

    	lname=new JLabel("Name :");
    	lname.setBounds(104,97,70,25);
    	pane.add(lname);

    	
    	patientNameField = new JTextField();
    	//patientNameField.setPreferredSize(new Dimension(70, 50));
    	//patientNameField.setHorizontalAlignment(0);
    	//patientNameField.setMaximumSize( patientNameField.getPreferredSize() );
    	pane.add(patientNameField);
    	patientNameField.setBounds(270,97,100,25);
    	
    	

    	ladd=new JLabel("Address :");
    	ladd.setBounds(104,138,70,15);
    	pane.add(ladd);
    	
    	
    	adressField = new JTextField();
    	pane.add(adressField);
    	adressField.setBounds(270,138,250,100);
    	
        
        
    	ltel=new JLabel("TEL: :");
    	ltel.setBounds(575,138,50,25);
    	pane.add(ltel);

    	lppsno=new JLabel("PPS No.:");
    	lppsno.setBounds(570,97,70,25);
    	pane.add(lppsno);
    	
    	ppsField = new JTextField();
    	pane.add(ppsField);
    	ppsField.setBounds(643,97,250,25);
    	
    	
    	PatientNoField = new JTextField();
    	pane.add(PatientNoField);
    	PatientNoField.setBounds(640,138,150,25);
    	
    	

    	lbloodGrp=new JLabel("Blood Group :");
    	lbloodGrp.setBounds(104,306,79,15);
    	pane.add(lbloodGrp);
    	

    	chbg=new Choice();
    	chbg.setBounds(270,306,53,15);
    	chbg.addItem("A -ve");
    	chbg.addItem("A +ve");
    	chbg.addItem("B -ve");
    	chbg.addItem("B +ve");
    	chbg.addItem("AB -ve");
    	chbg.addItem("AB +ve");
    	chbg.addItem("O +ve");
    	chbg.addItem("O -ve");
    	pane.add(chbg);
    	
    	ldob=new JLabel("Date of Birth :");
    	ldob.setBounds(575,306,135,15);
    	pane.add(ldob);

    	dobfield=new JTextField(15);
    	dobfield.setBounds(720,305,80,20);
    	pane.add(dobfield);
    	
    	lgender=new JLabel("Gender :");
    	lgender.setBounds(596,223,50,15);
    	pane.add(lgender);


    	
    	cbmf=new CheckboxGroup();
    	cbm=new Checkbox("Male",cbmf,true);
    	cbf=new Checkbox("Female",cbmf,false);
    	cbm.setBounds(698,223,50,15);
    	pane.add(cbm);
    	cbf.setBounds(760,223,60,15);
    	pane.add(cbf);
    	
    	
    	
    	lhis=new JLabel("History :");
    	lhis.setBounds(104,365,50,15);
    	pane.add(lhis);

    	hisField= new JTextArea();
    	hisField.setBounds(270,365,250,100);
    	pane.add(hisField);

    	lcur=new JLabel("Current Problem :");
    	lcur.setBounds(575,365,100,15);
    	pane.add(lcur);
    	
    	tacur= new JTextArea();
    	tacur.setBounds(720,365,250,100);
    	pane.add(tacur);
    	
    	clearBut=new JButton("CLEAR");
    	clearBut.setBounds(400,643,100,30);
    	pane.add(clearBut);
    	
    	cancelBut=new JButton("Cancel");
    	cancelBut.setBounds(500,643,100,30);
    	pane.add(cancelBut);
    	
    	
    	cancelBut.addActionListener( new ActionListener()
         {
         	public void actionPerformed(ActionEvent cancel) 
             {
                JOptionPane.showMessageDialog(null, "Cancelling and returning to main screen.") ;
                
                 
              }
         } );

    	addBut=new JButton("ADD");
    	addBut.setBounds(600,643,100,30);
    	pane.add(addBut);
    	
    	addBut.addActionListener( new ActionListener()
        {
        	public void actionPerformed(ActionEvent add) 
            {
               JOptionPane.showMessageDialog(null, "Adding New Patient") ;
                
               
                
              
                
            }
        } );
       
    }
 
    

	/**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    static void createAndShowaddPatientGUI() {
    	
    	
        //Create and set up the window.
        JFrame addPatientgui = new JFrame("Add Patient Form");
        addPatientgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Set up the content pane.
        addComponentsToPane(addPatientgui.getContentPane());
 
        //Size and display the window.
        
        addPatientgui.setSize(1068,800);
        
        //addPatientgui.setVisible(true);
       
    }
 
   
            public void run()
            {
            	createAndShowaddPatientGUI();
            }
      
    }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1