Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Handling two button events?

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Handling two button events?

    Hey,

    I'm fairly new to programming and as a project for one of my classes I have to do a visualization of Quicksort somehow in Java (how I'm not sure yet), but I was wondering how I could handle two button listener events? I can't seem to see what I'm doing wrong.

     import javax.swing.*;
       import java.awt.event.*;
       import java.awt.*;
       import java.lang.*;
     
        public class Test
       {
          public JFrame frame;
          public JPanel buttonPanel;
          public JPanel arrayPanel;
          public JPanel bottomPanel;
          public JLabel label;
          public JTextField t1;
          public JTextField t2;
          public JTextField t3;
          public JTextField t4;
          public JTextField t5;
          public JTextField t6;
          public JTextField t7;
          public JTextField t8;
          public JButton button;
          public JButton button2;
          public int[] numbers = new int[8];
     
           public Test()
          {
             frame = new JFrame("Quicksort");
             this.buildButtonPanel();
             frame.add(buttonPanel, BorderLayout.NORTH);
             this.buildArrayPanel();
             frame.add(arrayPanel, BorderLayout.CENTER);
             this.buildBottomPanel();
             frame.add(bottomPanel, BorderLayout.SOUTH);
             frame.setSize(500,500);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
          }
     
           private void buildButtonPanel()
          {
             button = new JButton("Generate Numbers");
             button2 = new JButton("Quicksort Numbers");
             button.addActionListener(new ButtonListener());
             buttonPanel = new JPanel();
             buttonPanel.add(button);
             buttonPanel.add(button2); 
          }
     
           private void buildArrayPanel()
          {
             t1 = new JTextField("",2);
             t2 = new JTextField("",2);
             t3 = new JTextField("",2);
             t4 = new JTextField("",2);
             t5 = new JTextField("",2);
             t6 = new JTextField("",2);
             t7 = new JTextField("",2);
             t8 = new JTextField("",2);
             arrayPanel = new JPanel();
             arrayPanel.add(t1);
             arrayPanel.add(t2);
             arrayPanel.add(t3);
             arrayPanel.add(t4);
             arrayPanel.add(t5);
             arrayPanel.add(t6);
             arrayPanel.add(t7);
             arrayPanel.add(t8);  
          }
     
           public void buildBottomPanel()
          {
             bottomPanel = new JPanel();
             label = new JLabel("Ugh...");
             label.setPreferredSize(new Dimension(6,6));
             label.setText("Ugh...");
             bottomPanel.add(label);
          }
     
           public class ButtonListener implements ActionListener
          {
     
              public void actionPerformed(ActionEvent e)
             {
                if(e.getSource() == button)
                {
                //Variables
                   int num1 = (int)(Math.random()*100);
                   int num2 = (int)(Math.random()*100);
                   int num3 = (int)(Math.random()*100);
                   int num4 = (int)(Math.random()*100);
                   int num5 = (int)(Math.random()*100);
                   int num6 = (int)(Math.random()*100);
                   int num7 = (int)(Math.random()*100);
                   int num8 = (int)(Math.random()*100);
     
                //Show array on screen
                   t1.setText(Integer.toString(num1));
                   t2.setText(Integer.toString(num2));
                   t3.setText(Integer.toString(num3));
                   t4.setText(Integer.toString(num4));
                   t5.setText(Integer.toString(num5));
                   t6.setText(Integer.toString(num6));
                   t7.setText(Integer.toString(num7));
                   t8.setText(Integer.toString(num8));
     
                //Copy numbers into array
                   numbers[0] = Integer.parseInt(t1.getText());
                   numbers[1] = Integer.parseInt(t2.getText());
                   numbers[2] = Integer.parseInt(t3.getText());
                   numbers[3] = Integer.parseInt(t4.getText());
                   numbers[4] = Integer.parseInt(t5.getText());
                   numbers[5] = Integer.parseInt(t6.getText());
                   numbers[6] = Integer.parseInt(t7.getText());
                   numbers[7] = Integer.parseInt(t8.getText());
                }
                if(e.getSource() == button2)
                {
                   quickSort(numbers, 0, 7);
                   t1.setText(Integer.toString(numbers[0]));
                   t2.setText(Integer.toString(numbers[1]));
                   t3.setText(Integer.toString(numbers[2]));
                   t4.setText(Integer.toString(numbers[3]));
                   t5.setText(Integer.toString(numbers[4]));
                   t6.setText(Integer.toString(numbers[5]));
                   t7.setText(Integer.toString(numbers[6]));
                   t8.setText(Integer.toString(numbers[7]));
                }
             }
          }
     
           public void quickSort(int array[], int start, int end)
          {
             int i = start;                          
             int k = end;                            
     
             if (end - start >= 1)                   
             {
                int pivot = array[start];       
     
                while (k > i)                   
                {
                   while (array[i] <= pivot && i <= end && k > i)  
                      i++;                                    
                   while (array[k] > pivot && k >= start && k >= i) 
                      k--;                                        
                   if (k > i)                                       
                      swap(array, i, k);                      
                }
                swap(array, start, k);          
     
                quickSort(array, start, k - 1); 
                quickSort(array, k + 1, end);   
             }
             else           {
                return;                     
             }
          }
     
           public void swap(int array[], int index1, int index2) 
          {
             int temp = array[index1];           
             array[index1] = array[index2];      
             array[index2] = temp;               
          }
     
           public static void main(String[] args)
          {
             Test test = new Test();
          }
       }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Handling two button events?

    The button listener class doesn't know about the buttons because they aren't in it's scope. What you can do is pass them as parameters to the constructor of your button listener:

    public ButtonListener(Button button, Button button2)
    {
         this.button = button;
         this.button2 = button2;
         // any other initializations you want
    }
     
    JButton button, button2;
    // rest of ButtonListener code

    You will also likely run into problems passing values back to your main GUI.

    The second solution is to have your Test class implement the ActionListener, then it has access to all of it's members and can listen to ActionEvents for itself.

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Handling two button events?

    Sorry, what problems might I run into passing values back into my GUI? I don't want to keep working this way if it is going to give me problems in the long run. Our professor didn't prepare us for this project at all, and I haven't been programming very long at all.

Similar Threads

  1. JComboBoxes & Events
    By KevinGreen in forum AWT / Java Swing
    Replies: 3
    Last Post: April 21st, 2010, 05:11 AM
  2. Connecting events
    By Olufemi in forum AWT / Java Swing
    Replies: 0
    Last Post: April 21st, 2010, 04:58 AM
  3. Regarding File Handling
    By ravjot28 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:45 PM
  4. 'MouseUp' Drag and Drop Events
    By copeg in forum AWT / Java Swing
    Replies: 0
    Last Post: November 10th, 2009, 07:21 PM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM