Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Two dimensional array to create JButtons

807603Dec 5 2004 — edited Dec 19 2007
Problem: I am not sure how use the array when to build JButtons.

Goal: An example of what I want to do would be to use the first array to input the name of the JButtons and the second array to input the text onto the button.

This is what I have so far
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

class array extends JFrame
{
    

    String [][] foodButton = {{"tofuBurgerButton", "Tofu Burger"}, 
                             {"cajunChickenButton", "Cajun Chicken"},
                             {"buffaloWingsButton", "Buffalo Wings"},
                             {"rainbowFilletButton", "Rainbow Fillet"},
                             {"riceCrackerButton", "Rice Cracker"},
                             {"noSaltFriesButton", "No Salt Fries"},
                             {"zucchiniButton", "Zucchini"},
                             {"brownRiceButton", "Brown Rice"},
                             {"cafeMochaButton", "Cafe Mocha"},
                             {"cafeLatteButton", "Cafe Latte"},
                             {"espressoButton", "Espresso"},
                             {"oolongTeaButton", "Oolong Tea"}};     

    JFrame arrayTestFrame;                            
                            

    public static void main(String [] args)
    {
        new array();
    }
    
    public void array()
    {
        arrayTestMain();
        setVisible(true);
    }

    
    public void arrayTestMain()
    {
        arrayTestFrame();
        
        arrayTestButton();
        
        arrayTestPanel();
    }
    
    public void arrayTestFrame()
    {
        setSize(500, 500);
        setTitle("Welcome to array test");
        setLocation(150, 250);
    }
    
    
    //Help
    public void arrayTestButton()
    {    
        for (int i = 0; i < foodButton.length; i++)
            for (int j = 0; j < foodButton.length; j++)
                {
                    foodButton[i][j] = new JButton (foodButton[i][j]);
                }
    }
    //Help
    
    
    public void arrayTestPanel()
    {
        JPanel arrayPanel;
        
        arrayPanel = new JPanel();
        
        arrayPanel.add(foodButton[i][j]);
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 16 2008
Added on Dec 5 2004
11 comments
3,851 views