Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding Elements to Array or ArrayList?
#1
Hi guys, I need some help.

Im trying to create a comment system in an applet.
When a user enters a new comment all previous comments are shown below.

The comments are taken from two text fields.

At the moment im just storing the latest 2 comments in variables, I need to find a way of adding them to an array.
All my efforts have failed as I cant seem to find a way to add an element to the very end of an array.

Ive pasted both the comment system and a stripped down version of the applet, just so you can replicate what im looking at right now.

My current comment system is below.
Code:
if(comment1 == null)
{    
    comment1=commentField.getText() + ":" + searchField.getText();
    resultMessage = comment1;
}
else
{
    comment2 = comment1;
    comment1=commentField.getText() + ":" + searchField.getText();
    resultMessage = comment1 + "     |     " + comment2;
}

This is a stripped down version of my applet
Code:
import java.util.*;
import java.applet.Applet ;
import java.awt.*; // import the java . awt package
import java.awt.event.*; // import the java . awt . e v ent package
import java.awt.*;

public class applet extends Applet implements ActionListener, MouseListener
{
    int ovalxcord,ovalycord,ovalsize=20;
    int x, y, width, height;
    String resultMessage = "";
    int messageX = 115, messageY = 50;
    String comment[] = new String [10];
    String location[] = new String [10];
    int commentno;
    
    
    public void paint(Graphics g)
    //pre: g is valid graphics object
    //post: draws to Applet window
    {
        setSize(800,300);
        
        // draw search
        Font standardBold = new Font("Arial", Font.BOLD, 12);
        g.setFont(standardBold);
        g.drawString ("Search:", 70,20);
        Font standard = new Font("Arial", Font.PLAIN, 12);
        g.setFont(standard);
        g.drawString(resultMessage,messageX,messageY);
        
        // draw black circle
        g.setColor(Color.black);
        g.fillOval(ovalxcord-(ovalsize/2),ovalycord-(ovalsize/2),ovalsize,ovalsize);
        
    }
    
    public void mouseClicked( MouseEvent e)
    {
       ovalxcord=e.getX();
       ovalycord=e.getY();
       repaint();
    }
  
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}

    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
       
       Button searchButton;
    Button infoButton;
    TextField searchField;
    TextField commentField;
    Button commentButton;
       
       public void init()
    {
           searchButton = new Button("Search");
           infoButton = new Button("Information");
           commentButton = new Button("Add Comment");
        searchField = new TextField(20);
        commentField = new TextField(20);
        add(searchField);
        add(searchButton);
        add(infoButton);
        add(commentField);
        add(commentButton);
    
        searchButton.addActionListener(this);
        infoButton.addActionListener(this);
        commentButton.addActionListener(this);
    }
       
       public void start( )
       {
           addMouseListener(this);
       }
    
       String comment1, comment2;
       
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == searchButton)
        {
            resultMessage = searchField.getText();
            messageX = 115; messageY = 50;
        }
        else
        if(e.getSource() == infoButton)
        {
            resultMessage = "Information " + ovalxcord + "," + ovalycord;
            messageX = ovalxcord+ovalsize;
            messageY = ovalycord+ovalsize/4;
        }
        
        if(e.getSource() == commentButton)
        {
            if(comment1 == null)
            {    
                comment1=commentField.getText() + ":" + searchField.getText();
                resultMessage = comment1;
            }
            else
            {
                comment2 = comment1;
                comment1=commentField.getText() + ":" + searchField.getText();
                resultMessage = comment1 + "     |     " + comment2;
            }
            
        }
        repaint();
    }
        
}

Any help is appreciated.

Thanks
[Image: banner2.gif]
Reply
#2
the moment im just storing the latest 2 comments in variables, I need to find a way of adding them to an array
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you create an array in C sharp? wholegrain 3 1,541 05-19-2017, 03:51 AM
Last Post: Clarencetew24
  My first GUI: Adding Calculator nevets04 25 5,394 01-02-2012, 06:38 PM
Last Post: MineCrack
  ArrayList Tip Project Evolution 2 1,211 03-13-2010, 10:44 PM
Last Post: Ⱳąŗɗ

Forum Jump:


Users browsing this thread: 2 Guest(s)