Results 1 to 5 of 5
  1. #1
    Lets Assume is offline Assuming
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Unhappy Is it possible to detect buttons pressed?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class autoreview extends JFrame implements ActionListener
    {
      
    
      JLabel L1 = new JLabel("BrettLaekzBananas",JLabel.CENTER);
      JLabel L2 = new JLabel("",JLabel.CENTER);
      JLabel L3 = new JLabel("",JLabel.CENTER);
      JLabel L4 = new JLabel("",JLabel.CENTER); 
      JLabel L5 = new JLabel("",JLabel.CENTER);
      JLabel L6 = new JLabel("",JLabel.CENTER); 
      JLabel L7 = new JLabel("",JLabel.CENTER);
      JLabel L8 = new JLabel("",JLabel.CENTER); 
      JLabel L9 = new JLabel("",JLabel.CENTER);
      JLabel L10 = new JLabel("",JLabel.CENTER); 
      JLabel L11 = new JLabel("",JLabel.CENTER);
        
      JButton B1 = new JButton("Battlefield Bad Company 2");
      JButton B2 = new JButton("Battlefield 3");
      JButton B3 = new JButton("Borderlands 2");
      JButton B4 = new JButton("Call of Duty 3");
      JButton B5 = new JButton("Call of Duty 4");
      JButton B6 = new JButton("Black Ops");
      JButton B7 = new JButton("Black Ops 2");
      JButton B8 = new JButton("Modern Warfare 2");
      JButton B9 = new JButton("Modern Warfare 3");
      JButton B10 = new JButton("World at War");
      JButton B11 = new JButton("Crysis 2");
      JButton B12 = new JButton("Gears of War 3");
      JButton B13 = new JButton("Gears of War: Judgment");
      JButton B14 = new JButton("Halo 4");
      JButton B15 = new JButton("Halo: Reach");
      JButton B16 = new JButton("Medal of Honor");
      JButton B17 = new JButton("Medal of Honor: Warfighter");
      JButton B18 = new JButton("Minecraft");
      JButton B19 = new JButton("Red Dead Redemption");
      JButton B20 = new JButton("Gaming");
      JButton B21 = new JButton("Boosting");
      JButton B22 = new JButton("Zombies/Spec Ops/Horde");
        
      JTextField T1 = new JTextField();
      JTextField T2 = new JTextField();
      JTextField T3 = new JTextField();
      JTextField T4 = new JTextField();
      JTextField T5 = new JTextField();
      JTextField T6 = new JTextField();
      JTextField T7 = new JTextField();
      JTextField T8 = new JTextField();
      JTextField T9 = new JTextField();
      JTextField T10 = new JTextField();
      JTextField T11 = new JTextField();
      JTextField T12 = new JTextField();
      JTextField T13 = new JTextField();
      
      //start activation for button here 
      public void button_B1()
      {
        B2.setEnabled(false); B3.setEnabled(false); B4.setEnabled(false);
        B5.setEnabled(false); B6.setEnabled(false); B7.setEnabled(false);
        B8.setEnabled(false); B9.setEnabled(false); B10.setEnabled(false);
        B11.setEnabled(false); B12.setEnabled(false); B13.setEnabled(false);
        B14.setEnabled(false); B15.setEnabled(false); B16.setEnabled(false);
        B17.setEnabled(false); B18.setEnabled(false); B19.setEnabled(false);
      }
        
      //stop activation of button
        
        
    //do not copy below this point por favor  
      public autoreview() 
      {
          
        super("BrettLaekzBananas");
        Container container = getContentPane(); 
        setSize(800,500);
        setLayout(new GridLayout(4,1));
          
          
          
        JPanel panel01 = new JPanel();
        panel01.setLayout(new GridLayout(1, 1));
        panel01.setBackground(Color.white);
        panel01.add(L1);
        container.add(panel01);
        
        JPanel panel02 = new JPanel();
        panel02.setLayout(new GridLayout(3, 1));
        panel02.setBackground(Color.white);
        panel02.add(B1);
        panel02.add(B2);
        panel02.add(B3);
        panel02.add(B4);
        panel02.add(B5);
        panel02.add(B6);
        panel02.add(B7);
        panel02.add(B8);
        panel02.add(B9);
        panel02.add(B10);
        panel02.add(B11);
        panel02.add(B12);
        container.add(panel02);
        
        JPanel panel03 = new JPanel();
        panel03.setLayout(new GridLayout(2, 1));
        panel03.setBackground(Color.white);
        panel03.add(B13);
        panel03.add(B14);
        panel03.add(B15);
        panel03.add(B16);
        panel03.add(B17);
        panel03.add(B18);
        panel03.add(B19);
        container.add(panel03);
        
        JPanel panel04 = new JPanel();
        panel04.setLayout(new GridLayout(3, 1));
        panel04.setBackground(Color.white);
        panel04.add(B20);
        panel04.add(B21);
        panel04.add(B22);
        container.add(panel04);
     
        B1.setFocusPainted(false);    B1.addActionListener(this);
        B2.setFocusPainted(false);    B2.addActionListener(this);
        B3.setFocusPainted(false);    B3.addActionListener(this);
        B4.setFocusPainted(false);    B4.addActionListener(this);
        B5.setFocusPainted(false);    B5.addActionListener(this);
        B6.setFocusPainted(false);    B6.addActionListener(this);
        B7.setFocusPainted(false);    B7.addActionListener(this);
        B8.setFocusPainted(false);    B8.addActionListener(this);
        B9.setFocusPainted(false);    B9.addActionListener(this);
        B10.setFocusPainted(false);   B10.addActionListener(this);
        B11.setFocusPainted(false);   B11.addActionListener(this);
        B12.setFocusPainted(false);   B12.addActionListener(this);
        B13.setFocusPainted(false);   B13.addActionListener(this);
        B14.setFocusPainted(false);   B14.addActionListener(this);
        B15.setFocusPainted(false);   B15.addActionListener(this);
        B16.setFocusPainted(false);   B16.addActionListener(this);
        B17.setFocusPainted(false);   B17.addActionListener(this);
        B18.setFocusPainted(false);   B18.addActionListener(this);
        B19.setFocusPainted(false);   B19.addActionListener(this);
        //stop adding here
        setVisible(true);
      }
        
      /* activate buttons and textfields here */
      public void actionPerformed(ActionEvent e)
      {
          
        if (e.getSource() == B1) button_B1();
        
      }
        
      public static void main(String args[]) {
        autoreview prog = new autoreview();
        prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }  
    }
    This isn't a complete code. But I was thinking ahead and ran into a problem. When buttons B20, B21, or B22 are pressed, I will have a new JFrame open up. What shows up in that JFrame is dependent on the buttons that were previously pressed. For example, if button B1 was pressed, then B20, what would appear in the JFrame would be different than what would appear if I pressed B2 then B22. I was wondering if there was a way to see which button (B1-B19) was pressed and make that be a factor in what shows up in the JFrame when button (B20-B22) is pressed.

    Any thoughts?


    Ohh, and I know I will be frequently posting new topics for new problems that arise. I am still in the process of beginner's steps.

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Is it possible to detect buttons pressed?

    Of course!!! you can use this way in the actionPerformed(ActionEvent e) method :

    String button = e.getActionCommand();
    so in button you will have the String of the button for example : "Battlefield Bad Company 2"
    and then you can make an if statement for each button or you can create some boolean variables to check if one button has pressed or not.. here an example :
    Java Code:
    public void actionPerformed(ActionEvent e ){
    	String button = e.getActionCommand();
    	if(button.equalsIgnoreCase("CallOfDuty 2")){
    		YourFrameClass newFrame = YourFrameClass("CallOfDuty 2",600,200);
    		newFrame.setVisible(true);
    	}else if(button.equalsIgnoreCase("Lineage 2")){
    		....
    	}
    	.....
    }
    and check here for more help : How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Last edited by ShadowWalker; 07-17-2013 at 01:11 AM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Is it possible to detect buttons pressed?

    Anytime an event is fired and handled you may save whatever state you want. You may use that state information to then make future decisions when other events are fired. And as previously mentioned, assigning an action command or using the button name is an example of such state information.

    I am not certain what you are doing but it appears a JComboBox could be useful. Have you considered using a JComboxBox instead of buttons 1 thru 19? And then use Buttons 20-22 once a selection has been made.

    Regards,
    Jim
    Last edited by jim829; 07-17-2013 at 01:35 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Is it possible to detect buttons pressed?

    I also suggest you consider using arrays or preferably Lists for storing your buttons and other objects. It makes for a smaller and clearer application. In some cases, you may not even need to refer to the object anymore via its assigned reference so why keep it around in your app?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Is it possible to detect buttons pressed?

    Quote Originally Posted by Lets Assume View Post
    When buttons B20, B21, or B22 are pressed, I will have a new JFrame open up.
    A well designed GUI usually has just one JFrame. Subordinate windows are usually modal JDialogs, often obtained via the static methods of JOptionPane.

    Oh, and in future please don't make your post all-bold as that makes it difficult to read. Formatting like bold and italic are meant for emphasis, not for body text.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Array of buttons, which one is pressed?
    By ea1 in forum New To Java
    Replies: 5
    Last Post: 03-17-2012, 07:41 PM
  2. Kep Pressed
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 09:07 PM
  3. Detect the ENTER key being pressed
    By aaronfsimons in forum New To Java
    Replies: 12
    Last Post: 05-16-2009, 09:48 PM
  4. get key pressed
    By prashant in forum Networking
    Replies: 1
    Last Post: 03-26-2009, 10:10 PM
  5. key pressed event
    By kavithas in forum New To Java
    Replies: 7
    Last Post: 12-10-2007, 03:01 PM

Posting Permissions

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