How can I display string arrays in a JApplet without using integers?

  • Thread starter Thread starter FritoTaco
  • Start date Start date
  • Tags Tags
    Array String
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
FritoTaco
Messages
132
Reaction score
23
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:
 String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};

Code:
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);
        add(tf);
       
        String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};
        for( int x=0; x < mybuttons.length; x++ )
        {
            mybuttons[x].addActionListener(this);
            add( mybuttons[x] );
        }
    }
    public void paint(Graphics g){
            super.paint(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" );
        }
    }
}
 
on Phys.org
FritoTaco said:
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:
 String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};

Code:
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);
        add(tf);
     
        String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};
        for( int x=0; x < mybuttons.length; x++ )
        {
            mybuttons[x].addActionListener(this);
            add( mybuttons[x] );
        }
    }
    public void paint(Graphics g){
            super.paint(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" );
        }
    }
}
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:
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.
 
  • Like
Likes   Reactions: FritoTaco
Thanks for the help. I'll work on it.