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
jilletteAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
You need to have this at the top of your class:

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

    JButton [] buttons;


and this:

 buttons = new JButton[btnNames.length];

before you start using buttons array

You need firts to create array, only then you can populate it or use sych fields as its buttons.length
0
 
for_yanCommented:
You can make array of JButton's

and have arrays of strings corresponding to the texts on each JButton
0
 
for_yanCommented:
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

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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

0
 
for_yanCommented:
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

0
 
for_yanCommented:
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
0
 
for_yanCommented:
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

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

buttons
0
 
jilletteAuthor Commented:
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

0
 
for_yanCommented:
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

0
 
for_yanCommented:
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
0
 
jilletteAuthor Commented:
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

0
 
jilletteAuthor Commented:
great guy, thanks for being patient with me :)
0
 
for_yanCommented:
Good, I'm happy it worked for you.
You are always welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.