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
AI Thread Summary
To display strings from an array in a Java applet, the paint method needs to be utilized effectively. The original code uses a two-dimensional array for strings, which is unnecessary; a one-dimensional array is more appropriate. The correct declaration should be `String[] name = {"dog", "cat", "bridge", "lake"};`. In the paint method, the Graphics object can be used to draw the strings onto the applet. This involves calling methods like `g.drawString(name[index], x, y)` to render each string at specified coordinates. The actionPerformed method currently updates a JLabel based on button clicks but does not handle displaying the strings from the array. Adjusting the code to incorporate these elements will allow the strings to be displayed correctly in the applet.
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 FritoTaco
Thanks for the help. I'll work on it.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
1
Views
1K
Replies
2
Views
3K
Replies
4
Views
5K
Replies
2
Views
2K
Replies
9
Views
4K
Replies
6
Views
3K
Replies
6
Views
2K
Back
Top