Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

String Array for JApplet

  1. Feb 27, 2017 #1
    I don't know how to display what the string you see in the array. Do I need to put something in the paint method? It's annoying because my book and everywhere I look only show examples using integers and not strings.

    This code is in the init method. I can't find out where to display the "dog", "cat", etc into the applet itself.

    Code (Text):
     String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};
    Code (Text):
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Arrays;
    import java.applet.*;
    import java.awt.Graphics;

    public class DictionaryTest extends JApplet implements ActionListener

         JLabel label;
         //declaring and creating array of objects/buttons using intiailizer list
        TextField tf;
        JButton[ ] mybuttons =
                new JButton( "one" ),
                new JButton( "two" ),
                new JButton( "three" ),
                new JButton( "four" )
        public void init( )
            setLayout( new FlowLayout( )  );
            tf = new TextField(15);
            String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};
            for( int x=0; x < mybuttons.length; x++ )
                add( mybuttons[x] );
        public void paint(Graphics g){
        public void actionPerformed( ActionEvent ae )
            Object obj = ae.getSource( );
            if( obj == mybuttons[0] )
                label.setText( "first button" );
            else if ( obj == mybuttons[1] )
                label.setText( "second button" );
            else if( obj == mybuttons[2] )
                label.setText( "third button" );
            else if( obj == mybuttons[3] )
                label.setText( "fourth button" );
  2. jcsd
  3. Feb 27, 2017 #2


    Staff: Mentor

    I believe that you need code in your paint() method. You should be able to use the String class's write method to write a string.

    Regarding your array, you shouldn't be using a two-dimensional array for your strings.

    The declaration should look like this:
    Code (Java):
    String [] name = {"dog", "cat", "bridge", "lake"};
    name[0] is "dog", name[1] is "cat" and so on.
    Actually, the array is just a list of addresses, with each address being the location of the first character in one of the strings.
  4. Feb 28, 2017 #3
    Thanks for the help. I'll work on it.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted