Results 1 to 4 of 4
  1. #1
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default How to use JButtons from a different class

    Hi,

    How do i use components from different classes. For Example I have a class called Button and i have some JButtons there. I have another class called Customer in which I would like to Add customers to an array list, but i can't figure out how to use the buttons from the other class. Do i need to create a get method?

    Java Code:
    public class Buttons extends JPanel 
    {
    	JPanel panel = new JPanel();
    	JFrame frame = new JFrame();
    	private JButton addButton;
    	private JButton modifyButton;
    	private JButton deleteButton;
    	
    	public JButton getAdd()
    	{
    		return addButton;
    	}
    	
    	public Buttons()
    	{  
    		super();
    		addButton = new JButton("Add");
    		modifyButton = new JButton("Modify");
    		deleteButton = new JButton("Delete");
    		
    		panel.setLayout(new FlowLayout());
    		panel.add(addButton);
    		panel.add(modifyButton);
    		panel.add(deleteButton);
    		add(panel);
    		
    		
    		 
    	}
    
    	
    	
    }
    Java Code:
    public class Customer extends JPanel
    { 
    	JPanel panel = new JPanel();
    	private String [] columnNames = {"First Name", "Last Name", "Customer Status", "Dependent Age", "Customer ID"};
    	private String [][] rowData = new String [15][15];
    	public Customer()
    	{
    				
    		panel.setLayout(new BorderLayout());
    		panel.add(new Buttons(), BorderLayout.NORTH);
    		panel.add(new CustomerInformation(), BorderLayout.CENTER);
    		panel.add(new CustomerDisplay(rowData, columnNames), BorderLayout.SOUTH);
    		
    		add(panel);
    		
    		Buttons button = new Buttons();
    		button.getAdd();
    		ButtonHandler handler = new ButtonHandler();
    		addButton.addActionListener(handler);
    		
    	}
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			JOptionPane.showMessageDialog(null, "Hello World");
    		}
    	}
    
    	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way is to create a ButtonHandler class. For example, say I have a class that holds two JButtons, fuButton and barButton that I want to have handled:

    Java Code:
    class FuBarButtons extends JPanel {
      private JButton fuButton;
      private JButton barButton;
    I can create a button handler class, FuBarButtonHandler that has public methods that I want called when the buttons are pushed:
    Java Code:
    class FuBarButtonHandler {
    
      public void fuActionPerformed(ActionEvent e) {
        System.out.println("fu button pushed");
      }
    
      public void barActionPerformed(ActionEvent e) {
        System.out.println("bar button pushed");
      }
    }
    and then I can add a FuBarButtonHandler variable to my button class, and a method to allow an outside class to set the handler:

    Java Code:
    class FuBarButtons extends JPanel {
      private JButton fuButton;
      private JButton barButton;
    
      private FuBarButtonHandler handler; // my handler variable
      
      //....
    
      // allows outside class to set the handler for my buttons
      public void setButtonHandler(FuBarButtonHandler handler) {
        this.handler = handler;
      }
    and then in FuBarButtons class's constructor, I give the buttons ActionListeners that call the handler code. But I also want to make sure that the handler has been set, that it's not null, before I call its methods:

    Java Code:
      public FuBarButtons() {
        fuButton = new JButton("Fu");
        barButton = new JButton("Bar");
        
        fuButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (handler != null) {
              // call the handler's method for the fu button
              handler.fuActionPerformed(e);
            }
          }
        });
        barButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (handler != null) {
              // call the handler's method for the bar button
              handler.barActionPerformed(e);
            }
          }
        });
    
        setLayout(new FlowLayout());
        add(fuButton);
        add(barButton);
      }


    All together, the code could look something like this:

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
       
    public class FuBarButtonTest extends JPanel {
      private FuBarButtons buttons = new FuBarButtons();
      private FuBarButtonHandler handler = new FuBarButtonHandler();
      
      public FuBarButtonTest() {
        buttons.setButtonHandler(handler);
        add(buttons);
      }
      
      private static void createAndShowGUI() {
        JFrame frame = new JFrame("ButtonTest Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new FuBarButtonTest());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
    }
    
       
    class FuBarButtons extends JPanel {
      private JButton fuButton;
      private JButton barButton;
    
      private FuBarButtonHandler handler;
      
      public FuBarButtons() {
        fuButton = new JButton("Fu");
        barButton = new JButton("Bar");
        
        fuButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (handler != null) {
              handler.fuActionPerformed(e);
            }
          }
        });
        barButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (handler != null) {
              handler.barActionPerformed(e);
            }
          }
        });
    
        setLayout(new FlowLayout());
        add(fuButton);
        add(barButton);
      }
      
      public void setButtonHandler(FuBarButtonHandler handler) {
        this.handler = handler;
      }
    }
    
    class FuBarButtonHandler {
     
      public void fuActionPerformed(ActionEvent e) {
        System.out.println("fu button pushed");
      }
      
      public void barActionPerformed(ActionEvent e) {
        System.out.println("bar button pushed");
      }
    }

  3. #3
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    I must still be doing something wrong. When i click the ADD Button nothing happens.

    Java Code:
    public class Buttons extends JPanel 
    {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        protected JButton addButton;
        private JButton modifyButton;
        private JButton deleteButton;
        
        
        public Buttons()
        {  
            super();
            addButton = new JButton("Add");
            modifyButton = new JButton("Modify");
            deleteButton = new JButton("Delete");
            
            panel.setLayout(new FlowLayout());
            panel.add(addButton);
            panel.add(modifyButton);
            panel.add(deleteButton);
            add(panel);
            
            
             
        }
    
        
    }
    
    class Customer extends JPanel
    { 
        
        JPanel panel = new JPanel();
        
        public Customer()
        {
                    
            panel.setLayout(new BorderLayout());
            panel.add(new Buttons(), BorderLayout.NORTH);
            panel.add(new CustomerInformation(), BorderLayout.CENTER);
            panel.add(new CustomerDisplay(), BorderLayout.SOUTH);
            
            add(panel);
            
            
        }
        
    
    
    }
    
    class CustomerInformation extends JPanel
    {
        JPanel panel = new JPanel();
        // Declare JLabels
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JLabel label4;
        private JLabel label5;
        
        // Declare JTextFields
        private JTextField text1 = new JTextField(10);
        private JTextField text2 = new JTextField(10);
        private JTextField text3 = new JTextField(10);
        private JTextField text4 = new JTextField(10);
        private JTextField text5 = new JTextField(10);
        private JTextField text6 = new JTextField(10);
    
        
        
        String stringText1 = text1.getText();
        String stringText2 = text2.getText();
        String stringText3 = text3.getText();
        String stringText4 = text4.getText();
        String stringText5 = text5.getText();
        
        //Properties
        public void setText1(String _text1)
        {
            stringText1 = _text1;
        }
        
        public String getText1()
        {
            return stringText1;
        }
        
        public void setText2(String _text2)
        {
            stringText2 = _text2;
        }
        
        public String getText2()
        {
            return stringText2;
        }
        
        public void setText3(String _text3)
        {
            stringText3 = _text3;
        }
        
        public String getText3()
        {
            return stringText3;
        }
        
        public void setText4(String _text4)
        {
            stringText4 = _text4;
        }
        
        public String getText4()
        {
            return stringText4;
        }
        
        public void setText5(String _text5)
        {
            stringText5 = _text5;
        }
        
        public String getText5()
        {
            return stringText5;
        }
        
        public CustomerInformation()
        { 
            // Instantiate labels
            label1 = new JLabel("First Name");
            label2 = new JLabel("Last Name");
            label3 = new JLabel("Customer Status");
            label4 = new JLabel("Dependendt Age");
            label5 = new JLabel("Customer ID");
            
            
            // Set the Grid Layout
            panel.setLayout( new GridLayout(5, 2));
            
            // Add items to grid
            panel.add(label1);
            panel.add(text1);
            panel.add(label2);
            panel.add(text2);
            panel.add(label3);
            panel.add(text3);
            panel.add(label4);
            panel.add(text4);
            panel.add(label5);
            panel.add(text5);
            
            
            add(panel);
        }
        
        
        
        
    }
    class CustomerDisplay extends JPanel
    {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        private DefaultTableModel model;
        private JTable table;
        CustomerInformation cust = new CustomerInformation();
        
         private String[] columnNames = {"First Name", "Last Name", "Customer Status", "Dependent Age", "Customer ID"};
        
    public CustomerDisplay()
    {
        super();
        model = new DefaultTableModel();
        model.addColumn("First Name");
        model.addColumn("Last Name");
        model.addColumn("Customer Status");
        model.addColumn("Dependent Age");
        model.addColumn("Customer ID");
        
        Buttons add = new Buttons();
        add.addButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                String [] customer = {cust.stringText1, cust.stringText2, cust.stringText3, cust.stringText4, cust.stringText5};
                model.addRow(customer);
            }
        });
        
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        panel.add(scrollPane);
        add(panel);
        
        
    
    }
    }
    Java Code:
    public class AirlineReservationDemo {
    
    
    
        public static void main(String[] args) 
        {
            Customer cust = new Customer();
            JTabbedPane tab = new JTabbedPane();
            JFrame frame = new JFrame("Airline");
            tab.add("Customer", cust);
            tab.add("Flight", null);
            tab.add("Reservations", null);
            frame.add(tab, BorderLayout.CENTER);
            frame.setSize(800,500);
            frame.setVisible(true);
            
            new CustomerDisplay();
            
        }
    
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by geekchick View Post
    I must still be doing something wrong. When i click the ADD Button nothing happens.
    Well, for one, you're not doing anything that I suggested.

Similar Threads

  1. Help; Missing JButtons
    By Cyprusice in forum New To Java
    Replies: 3
    Last Post: 01-10-2010, 11:50 PM
  2. JButtons and If statements
    By calculuslord in forum New To Java
    Replies: 6
    Last Post: 10-20-2009, 10:52 PM
  3. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 09:38 AM
  4. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 02:49 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •