Solved

Java multiple buttons

Posted on 2012-03-29
14
271 Views
Last Modified: 2012-03-29
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
0
Comment
Question by:jillette
  • 11
  • 3
14 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37784672
You can make array of JButton's

and have arrays of strings corresponding to the texts on each JButton
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37784693
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37784704
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 47

Expert Comment

by:for_yan
ID: 37784723
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37784777
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37784901
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37784916
when you click on each button - it would print the corresponding number on the system.out

buttons
0
 

Author Comment

by:jillette
ID: 37785081
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37785101
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37785111
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
 

Author Comment

by:jillette
ID: 37785130
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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37785138
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
 

Author Closing Comment

by:jillette
ID: 37785178
great guy, thanks for being patient with me :)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37785181
Good, I'm happy it worked for you.
You are always welcome.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
factorial example 4 39
hibernate insert example 13 28
by zero exception 10 40
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 33
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question