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

In summary, the code doesn't seem to allow you to display anything into the applet itself. You might need to include code in the paint() method to write text to the label.
  • #1
FritoTaco
132
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" );
        }
    }
}
 
Technology news on Phys.org
  • #2
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 FritoTaco
  • #3
Thanks for the help. I'll work on it.
 

1. What is a String Array in a JApplet?

A String Array in a JApplet is a data structure that stores a collection of strings in a specific order. It is used to hold multiple strings and can be accessed and manipulated by a JApplet program.

2. How do I declare a String Array in a JApplet?

To declare a String Array in a JApplet, you can use the syntax: String[] arrayName = new String[size]. This will create an empty array with the specified size.

3. How do I add elements to a String Array in a JApplet?

You can add elements to a String Array in a JApplet by using the array index. For example, to add a string "Hello" at index 0, you can use the syntax: arrayName[0] = "Hello".

4. How do I access elements from a String Array in a JApplet?

To access elements from a String Array in a JApplet, you can use the array index again. For example, to retrieve the string at index 2, you can use the syntax: arrayName[2]. This will return the element at that specific index.

5. How do I loop through a String Array in a JApplet?

You can use a for loop to iterate through a String Array in a JApplet. For example, to print out all the elements in the array, you can use the following code: for(int i=0; i

Similar threads

  • Programming and Computer Science
Replies
1
Views
881
  • Programming and Computer Science
Replies
4
Views
946
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top