Link to home
Start Free TrialLog in
Avatar of jillette
jilletteFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Java multiple buttons

Hi, how so i trim this code?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Components
{
    // instance variables - replace the example below with your own
    private JButton one,two,three,four,five,six,seven,eight,nine,ten,
    eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen,twenty,
    twentyOne,twentyTwo,twentyThree,twentyFour,twentyFive,twentySix,twentySeven,twentyEight,twentyNine,thirty,
    thirtyOne,thirtyTwo,thirtyThree,thirtyFour,thirtyFive,thirtySix,thirtySeven,thirtyEight,thirtyNine;;
    private JPanel betField;
    private RouletteGUI roulette;

    public void displayFieldButtons(){
        roulette = new RouletteGUI();
        betField.setLayout(new GridLayout(3, 12));
            betField = new JPanel();

        three = new JButton("3");
        three.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(three.getText());
                }
            });
        

        six = new JButton("6");
        six.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(six.getText());
                }
            });
        betField.add(six);

        nine = new JButton("9");
        nine.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(nine.getText());
                }
            });
        betField.add(nine);

        twelve = new JButton("12");
        twelve.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twelve.getText());
                }
            });
        betField.add(twelve);

        fifteen = new JButton("15");
        fifteen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(fifteen.getText());
                }
            });			
        betField.add(fifteen);    

        eighteen = new JButton("18");
        eighteen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(eighteen.getText());
                }
            });
        betField.add(eighteen);

        twentyOne = new JButton("21");
        twentyOne.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twentyOne.getText());
                }
            });
        betField.add(twentyOne);

        twentyFour = new JButton("24");
        twentyFour.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twentyFour.getText());
                }
            });
        betField.add(twentyFour);

        thirtySeven = new JButton("27");
        thirtySeven.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirtySeven.getText());
                }
            });
        betField.add(thirtySeven);

        thirty = new JButton("30");
        thirty.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirty.getText());
                }
            });
        betField.add(thirty);

        thirtyThree = new JButton("33");
        thirtyThree.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirtyThree.getText());
                }
            });

        betField.add(thirtyThree);

        thirtySix = new JButton("36");
        thirtySix .addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirtySix.getText());
                }
            });
        betField.add(thirtySix);

        two = new JButton("2");
        two.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(two.getText());
                }
            });
        betField.add(two);

        five = new JButton("5");
        five.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(five.getText());
                }
            });
        betField.add(five);

        eight = new JButton("8");
        eight.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(eight.getText());
                }
            });
        betField.add(eight);

        eleven = new JButton("11");
        eleven.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(eleven.getText());
                }
            });
        betField.add(eleven);

        fourteen = new JButton("14");
        fourteen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(fourteen.getText());
                }
            });			
        betField.add(fourteen);    

        seventeen = new JButton("17");
        seventeen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(seventeen.getText());
                }
            });
        betField.add(seventeen);

        twenty = new JButton("20");
        twenty.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twenty.getText());
                }
            });
        betField.add(twenty);

        twentyThree = new JButton("23");
        twentyThree.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twentyThree.getText());
                }
            });
        betField.add(twentyThree);

        twentySix = new JButton("26");
        twentySix.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twentySix.getText());
                }
            });
        betField.add(twentySix);

        twentyNine = new JButton("29");
        twentyNine.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twentyNine.getText());
                }
            });
        betField.add(twentyNine);

        thirtyTwo = new JButton("32");
        thirtyTwo.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirtyTwo.getText());
                }
            });

        betField.add(thirtyTwo);

        thirtyFive = new JButton("35");
        thirtyFive .addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirtyFive.getText());
                }
            });
        betField.add(thirtyFive);

        one = new JButton("1");
        one.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(one.getText());
                }
            });
        betField.add(one);

        four = new JButton("4");
        four.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(four.getText());
                }
            });
        betField.add(four);

        seven = new JButton("7");
        seven.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(seven.getText());
                }
            });
        betField.add(seven);

        ten = new JButton("10");
        ten.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(ten.getText());
                }
            });
        betField.add(ten);

        thirteen = new JButton("13");
        thirteen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirteen.getText());
                }
            });			
        betField.add(thirteen);    

        sixteen = new JButton("16");
        sixteen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(sixteen.getText());
                }
            });
        betField.add(sixteen);

        nineteen = new JButton("19");
        nineteen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(nineteen.getText());
                }
            });
        betField.add(nineteen);

        twentyTwo = new JButton("22");
        twentyTwo.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twentyTwo.getText());
                }
            });
        betField.add(twentyTwo);

        twentyFive = new JButton("25");
        twentyFive.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twentyFive.getText());
                }
            });
        betField.add(twentyFive);

        twentyEight = new JButton("28");
        twentyEight.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(twentyEight.getText());
                }
            });
        betField.add(twentyEight);

        thirtyOne = new JButton("31");
        thirtyOne.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirtyOne.getText());
                }
            });
        betField.add(thirtyOne);   

        thirtyFour = new JButton("34");
        thirtyFour .addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {     
                    roulette.setnumber(thirtyFour.getText());
                }
            });
        betField.add(thirtyFour);



    }

}

Open in new window


there must be an easyer way than this.

Thanks
Avatar of for_yan
for_yan
Flag of United States of America image

You can make array of JButton's

and have arrays of strings corresponding to the texts on each JButton
something like that - do not know aht ii RoulletteGUI, but it is probably not important:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonsGUI extends JFrame {


    String [] btnNames = {"one", "two","three"};

    JButton [] buttons;

    public ButtonsGUI() {
        buttons = new JButton[btnNames.length];
        
            JPanel  betField = new JPanel();
          betField.setLayout(new GridLayout(3, 1));

        for(int j=0; j<buttons.length; j++){
            buttons[j] = new JButton(btnNames[j]);
              buttons[j].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    roulette.setnumber(buttons[j].getText());
                }
            });
            
            betField.add(buttons[j]);

        }

    

        
        



    }



}

Open in new window

This would be better, as it cannot acces non-instance variable (j)  inside anonyomouys class

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonsGUI extends JFrame {


    String [] btnNames = {"one", "two","three"};

    JButton [] buttons;
    
    int count;

    public ButtonsGUI() {
        buttons = new JButton[btnNames.length];

            JPanel  betField = new JPanel();
          betField.setLayout(new GridLayout(3, 1));

        for( count=0;count<buttons.length; count++){
            buttons[count] = new JButton(btnNames[count]);
              buttons[count].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    roulette.setnumber(buttons[count].getText());
                }
            });
            
            betField.add(buttons[count]);

        }



        
        



    }



}

Open in new window

You can make it this way with "1","2""3" instead of thext on the buttons
as it is in roulette I gues and you can easily convert them into  numbers if necessary

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonsGUI extends JFrame {


    String [] btnNames = {"1", "2","3"};

    JButton [] buttons;

    int count;

    public ButtonsGUI() {
        buttons = new JButton[btnNames.length];

            JPanel  betField = new JPanel();
          betField.setLayout(new GridLayout(3, 1));

        for( count=0;count<buttons.length; count++){
            buttons[count] = new JButton(btnNames[count]);
              buttons[count].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    roulette.setnumber(buttons[count].getText());
                }
            });
            
            betField.add(buttons[count]);

        }



        
        



    }



}

Open in new window

with this code - one you jsut add one more string to btnNames array in its declartion at top
(and modify new  GridLayout(..)  accordingly) - you'll accommodates as many buttons as you want
ACtually this is corrected - and I tested it - it executes fine - use this code
with  
final String text =   buttons[count].getText();
before anonyomous class

and
roulette.setnumber(text);
inside  anlonymous class

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonsGUI extends JFrame {


    String [] btnNames = {"1", "2","3","4","5","6"};

    JButton [] buttons;

    int count;

    public ButtonsGUI() {
        buttons = new JButton[btnNames.length];

            JPanel  betField = new JPanel();
          betField.setLayout(new GridLayout(3, 2));



        for( count=0;count<buttons.length; count++){
            buttons[count] = new JButton(btnNames[count]);
            final String text =   buttons[count].getText();
              buttons[count].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                   // roulette.setnumber(text);
                    System.out.println(text);
                }
            });
            
            betField.add(buttons[count]);

        }

        Container c = this.getContentPane();
        c.add(betField);

         this.setSize(200,200);
        this.setVisible(true);


        
        



    }


    public static void main(String[] args) {

        new ButtonsGUI();
    }
}

Open in new window

when you click on each button - it would print the corresponding number on the system.out

User generated image
Avatar of jillette

ASKER

Hi, sorry im new to guis, its working great atm, but how do i add it to my main application?
below is what i tried:
    private void makeFrame()
    {
        frame = new JFrame("My Application");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        contentPane.add(label);

        fieldButtons = new ButtonsGUI();

        

        frame.pack();
        frame.setVisible(true);
    }

Open in new window

This part jsut creates the JPanel betField which is then added to JFrame - in my case
it was ButtonGUI,
you can add this JPanel to any of your frames



            JPanel  betField = new JPanel();
          betField.setLayout(new GridLayout(3, 2));



        for( count=0;count<buttons.length; count++){
            buttons[count] = new JButton(btnNames[count]);
            final String text =   buttons[count].getText();
              buttons[count].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                   // roulette.setnumber(text);
                    System.out.println(text);
                }
            });
            
            betField.add(buttons[count]);

        }

Open in new window




You can't add JFrame to Jframe , but you can add JPanle to JFrame

so it should be simething like that should work



I usually don't use box layout - so changed to BorderLayout, but it cdpends on your preferences

  String [] btnNames = {"1", "2","3","4","5","6"};

    JButton [] buttons;

    int count;

    private void makeFrame()
    {
        frame = new JFrame("My Application");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout());

        contentPane.add(label, BorderLayout.NORTH);

    //    fieldButtons = new ButtonsGUI();

       JPanel  betField = new JPanel();
          betField.setLayout(new GridLayout(3, 2));



        for( count=0;count<buttons.length; count++){
            buttons[count] = new JButton(btnNames[count]);
            final String text =   buttons[count].getText();
              buttons[count].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                   // roulette.setnumber(text);
                    System.out.println(text);
                }
            });
            
            betField.add(buttons[count]);

        }

contentPane.add(betField);





        

        frame.pack();
        frame.setVisible(true);
    }
                                            

Open in new window

Your originall method which you poseted was egenerating JPanel betField
In that sens my code is very simlira it generates the same panel - so use it the same way
you used your betField panel in your code; there may be necessaryy some minor adjustments
count < buttons.length;  is throwing a null pointer error.
java.lang.NullPointerException

java.lang.NullPointerException
	at RouletteGUI.makeFrame(RouletteGUI.java:65)
	at RouletteGUI.<init>(RouletteGUI.java:45)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at bluej.runtime.ExecServer$3.run(ExecServer.java:790)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
great guy, thanks for being patient with me :)
Good, I'm happy it worked for you.
You are always welcome.