Search code examples
javaswingjtextarea

adding a double to a JTextArea?


im trying to add a double value that represents the stamina of my player into a jTextArea after i click my north button cant seem to do it heres my code:

private void northButtonActionPerformed(java.awt.event.ActionEvent evt) 
{                                            
    game.playerMove(MoveDirection.NORTH);
    update();
    double playerStamina = player.getStaminaLevel();

    //tried this
    String staminaLevel = Double.toString(playerStamina);
    jTextArea1.setText("Stamina: " + staminaLevel);
}                

im new here sorry if this is not right

heres the main method

public class Main 
{
/**
 * Main method of Lemur Island.
 * 
 * @param args the command line arguments
 */
public static void main(String[] args) 
{
    // create the game object
    final Game game = new Game();
    // create the GUI for the game
    final LemurIslandUI  gui  = new LemurIslandUI(game);
    // make the GUI visible
    java.awt.EventQueue.invokeLater(new Runnable() 
    {
        @Override
        public void run() 
        {
            gui.setVisible(true);
        }
    });
}

and heres the class

public class LemurIslandUI extends javax.swing.JFrame
{
private Game game;
private Player player;
/** 
 * Creates a new JFrame for Lemur Island.
 * 
 * @param game the game object to display in this frame
 */
public LemurIslandUI(final Game game) 
{
    this.game = game;     
    initComponents();
    createGridSquarePanels();
    update();      
}

private void createGridSquarePanels() {

    int rows = game.getIsland().getNumRows();
    int columns = game.getIsland().getNumColumns();
    LemurIsland.removeAll();
    LemurIsland.setLayout(new GridLayout(rows, columns));

    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < columns; col++)
        {
            GridSquarePanel panel = new GridSquarePanel(game, row, col);
            LemurIsland.add(panel);
        }
    }  
}

/**
 * Updates the state of the UI based on the state of the game.
 */
private void update()
{ 
    for(Component component : LemurIsland.getComponents()) 
    {
        GridSquarePanel gsp = (GridSquarePanel) component;
        gsp.update();
}
    game.drawIsland();

}

Solution

  • Your class doesn't seem to be implmeneting ActionListener, therefore the action on your button will not be triggered.

    Your class declaration should be:

    public class LemurIslandUI extends javax.swing.JFrame implements ActionListener 
    

    And put the code for your button action inside:

    public void actionPerformed(ActionEvent e) {}
    

    Alternatively, you can use an anonymous class to implement the code for your button, instead of making your class implement the ActionListener. Something like:

    final JButton button = new JButton();
    
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent)
            {
                //code
            }
        });