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
Click For Summary
SUMMARY

This discussion focuses on displaying string arrays in a JApplet without using integers. The user initially attempts to use a two-dimensional array, String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};, which is not optimal for this purpose. The correct approach is to utilize a one-dimensional array, String [] name = {"dog", "cat", "bridge", "lake"};, and implement the paint(Graphics g) method to render the strings on the applet. The user is advised to use the Graphics.drawString() method to display the strings effectively.

PREREQUISITES
  • Understanding of Java Applet programming
  • Familiarity with the AWT and Swing libraries
  • Knowledge of event handling in Java
  • Basic understanding of arrays in Java
NEXT STEPS
  • Implement the Graphics.drawString() method in the paint(Graphics g) method to display strings.
  • Learn about the differences between one-dimensional and two-dimensional arrays in Java.
  • Explore Java AWT and Swing components for better UI design.
  • Study Java event handling to enhance interactivity in applets.
USEFUL FOR

Java developers, particularly those working with applets and GUI applications, will benefit from this discussion. It is also valuable for beginners seeking to understand string manipulation and display in Java.

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" );
        }
    }
}
 
Technology news 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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K