Results 1 to 6 of 6

Thread: Help with adding working buttons and lists with java

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Help with adding working buttons and lists with java

    since there are no useful tutorials on the internet on how to use buttons I will now ask here how do I use buttons on java? I got one button to work to output "hello" but when I have muiltiple how do I make them work. the code I have is:



    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class MaxHit extends JPanel {
    	private static JButton Button;
        private JButton IncreaseStr,Increasebon,DecreaseStr,Decreasebon,Maxhitbut;
        private JComboBox Prayop,Potop;
       
        public MaxHit() {
            //construct preComponents
            String[] PrayopItems = {"None", "Burst of Strength", "Super Human Strength", "Ultimate Strength", "Chivarly", "Piety", "Turmoil"};
            String[] PotopItems = {"None", "Strength potion", "Super Strength potions", "Extreme Strength potion"};
    
            //construct components
            IncreaseStr = new JButton ("Increase Strength");
            Increasebon = new JButton ("Increase Bonus");
            DecreaseStr = new JButton ("Descrease Strength");
            Decreasebon = new JButton ("Decrease Bonus");
            Maxhitbut = new JButton ("Max hit");
            Prayop = new JComboBox (PrayopItems);
            Potop = new JComboBox (PotopItems);
    
            //adjust size and set layout
            setPreferredSize (new Dimension (432, 362));
            setLayout (null);
    
            //add components
            add (IncreaseStr);
            add (Increasebon);
            add (DecreaseStr);
            add (Decreasebon);
            add (Maxhitbut);
            add (Prayop);
            add (Potop);
    
            //set component bounds (only needed by Absolute Positioning)
            IncreaseStr.setBounds (115, 10, 190, 30);
            Increasebon.setBounds (115, 90, 190, 30);
            DecreaseStr.setBounds (115, 50, 190, 30);
            Decreasebon.setBounds (115, 130, 190, 30);
            Maxhitbut.setBounds (85, 270, 245, 65);
            Prayop.setBounds (105, 175, 210, 25);
            Potop.setBounds (105, 210, 210, 25);
        }
    
    
        public static void main (String[] args) {
            JFrame frame = new JFrame ("zzzzz");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add (new MaxHit());
            frame.pack();
            frame.setVisible (true);
            
            Button.addActionListener(new ActionListener()  {
             
               public void actionPerformed(ActionEvent e)  
               {  
                	System.out.println("test");
                }
                
    
            });
            }
        }

    When I run it I get:
    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at MaxHit.main(MaxHit.java:60)
    But the GUI still pops up

    How Do i make buttons and lists work properly?

  2. #2
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    1,631
    Mentioned
    47 Post(s)
    Quoted
    254 Post(s)

    Default

    You can either write your own ActionListener or implement one. The easiest way is just to implement it.

    public class MaxHit extends JPanel implements ActionListener {

    You will now be forced to make the methods that you implement from ActionListener.
    Example:
    Code:
    @Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == about){
    			HelpWindow hw = new HelpWindow(MainWindow.this);
    			hw.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    			hw.setSize(300, 100);
    			hw.setLocationRelativeTo(this);
    			hw.setTitle("Help Window");
    			hw.setVisible(true);
    			return;
    		}
    Code:
    if(e.getSource() == IncreaseStr){
      System.out.println("IncreaseStr button clicked");
    }else{
    etc.
    }
    For each button you need to add an actionlistener which is "this" because you implement it.
    IncreaseStr.addActionListener(this);

    Example GUI where you grab from:
    http://pastebin.com/UuHrc1Pv

    Main class: http://pastebin.com/33DxcU4j

    Script source code available here: Github

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by J J View Post
    You can either write your own ActionListener or implement one. The easiest way is just to implement it.

    public class MaxHit extends JPanel implements ActionListener {

    You will now be forced to make the methods that you implement from ActionListener.
    Example:
    Code:
    @Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == about){
    			HelpWindow hw = new HelpWindow(MainWindow.this);
    			hw.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    			hw.setSize(300, 100);
    			hw.setLocationRelativeTo(this);
    			hw.setTitle("Help Window");
    			hw.setVisible(true);
    			return;
    		}
    Code:
    if(e.getSource() == IncreaseStr){
      System.out.println("IncreaseStr button clicked");
    }else{
    etc.
    }
    For each button you need to add an actionlistener which is "this" because you implement it.
    IncreaseStr.addActionListener(this);

    Example GUI where you grab from:
    http://pastebin.com/UuHrc1Pv

    Main class: http://pastebin.com/33DxcU4j

    It's still not working.. and I can't learning anything from a random botting script that does not apply with what I am trying to make

    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class MaxHit extends JPanel {
    	private static JButton Button;
        private JButton IncreaseStr,Increasebon,DecreaseStr,Decreasebon,Maxhitbut;
        private JComboBox Prayop,Potop;
       
        public MaxHit() {
            //construct preComponents
            String[] PrayopItems = {"None", "Burst of Strength", "Super Human Strength", "Ultimate Strength", "Chivarly", "Piety", "Turmoil"};
            String[] PotopItems = {"None", "Strength potion", "Super Strength potions", "Extreme Strength potion"};
    
            //construct components
            IncreaseStr = new JButton ("Increase Strength");
            Increasebon = new JButton ("Increase Bonus");
            DecreaseStr = new JButton ("Descrease Strength");
            Decreasebon = new JButton ("Decrease Bonus");
            Maxhitbut = new JButton ("Max hit");
            Prayop = new JComboBox (PrayopItems);
            Potop = new JComboBox (PotopItems);
    
            //adjust size and set layout
            setPreferredSize (new Dimension (432, 362));
            setLayout (null);
    
            //add components
            add (IncreaseStr);
            add (Increasebon);
            add (DecreaseStr);
            add (Decreasebon);
            add (Maxhitbut);
            add (Prayop);
            add (Potop);
    
            //set component bounds (only needed by Absolute Positioning)
            IncreaseStr.setBounds (115, 10, 190, 30);
            Increasebon.setBounds (115, 90, 190, 30);
            DecreaseStr.setBounds (115, 50, 190, 30);
            Decreasebon.setBounds (115, 130, 190, 30);
            Maxhitbut.setBounds (85, 270, 245, 65);
            Prayop.setBounds (105, 175, 210, 25);
            Potop.setBounds (105, 210, 210, 25);
        }
    
    
        public static void main (String[] args) {
            JFrame frame = new JFrame ("MyPanel");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add (new MaxHit());
            frame.pack();
            frame.setVisible (true);
            
            Button.addActionListener(new ActionListener()  {
            	@Override
            	public void actionPerformed(ActionEvent e) {
            		System.out.println("this still does not work and gives me the same error.");
            			
            			
            			
            			
            			return;
            		}
        });
        }
    }

  4. #4
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    1,631
    Mentioned
    47 Post(s)
    Quoted
    254 Post(s)

    Default

    It's not a random botting script, I made it and it constructs a GUI and passes the data to another class.

    Code:
    package org.obduro.java;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class MaxHit extends JPanel implements ActionListener {
    	private static JButton Button;
    	
        private JButton IncreaseStr,Increasebon,DecreaseStr,Decreasebon,Maxhitbut;
        private JComboBox Prayop,Potop;
       
        public MaxHit() {
            //construct preComponents
            String[] PrayopItems = {"None", "Burst of Strength", "Super Human Strength", "Ultimate Strength", "Chivarly", "Piety", "Turmoil"};
            String[] PotopItems = {"None", "Strength potion", "Super Strength potions", "Extreme Strength potion"};
    
            //construct components
            IncreaseStr = new JButton ("Increase Strength");
            IncreaseStr.addActionListener(this);
            
            Increasebon = new JButton ("Increase Bonus");
            Increasebon.addActionListener(this);
            
            DecreaseStr = new JButton ("Descrease Strength");
            DecreaseStr.addActionListener(this);
            
            Decreasebon = new JButton ("Decrease Bonus");
            Decreasebon.addActionListener(this);
            
            Maxhitbut = new JButton ("Max hit");
            Maxhitbut.addActionListener(this);
            
            Prayop = new JComboBox<String>(PrayopItems);
            Potop = new JComboBox<String>(PotopItems);
    
            //adjust size and set layout
            setPreferredSize (new Dimension (432, 362));
            setLayout (null);
    
            //add components
            add(IncreaseStr);
            add(Increasebon);
            add(DecreaseStr);
            add(Decreasebon);
            add(Maxhitbut);
            add(Prayop);
            add(Potop);
    
            //set component bounds (only needed by Absolute Positioning)
            IncreaseStr.setBounds (115, 10, 190, 30);
            Increasebon.setBounds (115, 90, 190, 30);
            DecreaseStr.setBounds (115, 50, 190, 30);
            Decreasebon.setBounds (115, 130, 190, 30);
            Maxhitbut.setBounds (85, 270, 245, 65);
            Prayop.setBounds (105, 175, 210, 25);
            Potop.setBounds (105, 210, 210, 25);
        }
    
    
        public static void main (String[] args) {
            JFrame frame = new JFrame ("MyPanel");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MaxHit());
            frame.pack();
            frame.setVisible (true);
        }
    
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == IncreaseStr){
    			System.out.println("IncreaseStr has been clicked");
    		}
    		
    		if(e.getSource() == Increasebon){
    			System.out.println("Increasebon has been clicked");
    		}
    		
    		if(e.getSource() == Decreasebon){
    			System.out.println("Increasebon has been clicked");
    		}
    		
    		if(e.getSource() == DecreaseStr){
    			System.out.println("DecreaseStr has been clicked");
    		}
    		
    		if(e.getSource() == Maxhitbut){
    			System.out.println("Maxhitbut has been clicked");
    		}
    		
    		if(e.getSource() == Prayop){
    			System.out.println("Prayop has been clicked");
    		}
    		
    		if(e.getSource() == Potop){
    			System.out.println("Potop has been clicked");
    		}
    		
    	}
    }


    Still major logical mistakes / syntax mistakes in your code but atleast it works. I suggest working on those before continueing.

    Script source code available here: Github

  5. #5
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by J J View Post
    It's not a random botting script, I made it and it constructs a GUI and passes the data to another class.

    Code:
    package org.obduro.java;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class MaxHit extends JPanel implements ActionListener {
    	private static JButton Button;
    	
        private JButton IncreaseStr,Increasebon,DecreaseStr,Decreasebon,Maxhitbut;
        private JComboBox Prayop,Potop;
       
        public MaxHit() {
            //construct preComponents
            String[] PrayopItems = {"None", "Burst of Strength", "Super Human Strength", "Ultimate Strength", "Chivarly", "Piety", "Turmoil"};
            String[] PotopItems = {"None", "Strength potion", "Super Strength potions", "Extreme Strength potion"};
    
            //construct components
            IncreaseStr = new JButton ("Increase Strength");
            IncreaseStr.addActionListener(this);
            
            Increasebon = new JButton ("Increase Bonus");
            Increasebon.addActionListener(this);
            
            DecreaseStr = new JButton ("Descrease Strength");
            DecreaseStr.addActionListener(this);
            
            Decreasebon = new JButton ("Decrease Bonus");
            Decreasebon.addActionListener(this);
            
            Maxhitbut = new JButton ("Max hit");
            Maxhitbut.addActionListener(this);
            
            Prayop = new JComboBox<String>(PrayopItems);
            Potop = new JComboBox<String>(PotopItems);
    
            //adjust size and set layout
            setPreferredSize (new Dimension (432, 362));
            setLayout (null);
    
            //add components
            add(IncreaseStr);
            add(Increasebon);
            add(DecreaseStr);
            add(Decreasebon);
            add(Maxhitbut);
            add(Prayop);
            add(Potop);
    
            //set component bounds (only needed by Absolute Positioning)
            IncreaseStr.setBounds (115, 10, 190, 30);
            Increasebon.setBounds (115, 90, 190, 30);
            DecreaseStr.setBounds (115, 50, 190, 30);
            Decreasebon.setBounds (115, 130, 190, 30);
            Maxhitbut.setBounds (85, 270, 245, 65);
            Prayop.setBounds (105, 175, 210, 25);
            Potop.setBounds (105, 210, 210, 25);
        }
    
    
        public static void main (String[] args) {
            JFrame frame = new JFrame ("MyPanel");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MaxHit());
            frame.pack();
            frame.setVisible (true);
        }
    
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == IncreaseStr){
    			System.out.println("IncreaseStr has been clicked");
    		}
    		
    		if(e.getSource() == Increasebon){
    			System.out.println("Increasebon has been clicked");
    		}
    		
    		if(e.getSource() == Decreasebon){
    			System.out.println("Increasebon has been clicked");
    		}
    		
    		if(e.getSource() == DecreaseStr){
    			System.out.println("DecreaseStr has been clicked");
    		}
    		
    		if(e.getSource() == Maxhitbut){
    			System.out.println("Maxhitbut has been clicked");
    		}
    		
    		if(e.getSource() == Prayop){
    			System.out.println("Prayop has been clicked");
    		}
    		
    		if(e.getSource() == Potop){
    			System.out.println("Potop has been clicked");
    		}
    		
    	}
    }


    Still major logical mistakes / syntax mistakes in your code but atleast it works. I suggest working on those before continueing.


    Thanks works great

  6. #6
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    someone want to help me with lists? :d

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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