SRL Archive - News and General

Help with adding working buttons and lists with java


rj

2013-01-16 16:24:12

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:



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:
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?

J J

2013-01-16 16:31:30

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:
@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;
}

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

rj

2013-01-16 16:36:50

From: J J
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:
@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;
}

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

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;
}
});
}
}

J J

2013-01-16 17:05:38

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

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.

rj

2013-01-16 18:06:57

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

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

rj

2013-01-16 22:07:16

someone want to help me with lists? :d