My JTextArea cant append..

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.LineBorder;

import java.io.*;

import java.net.*;

import java.util.Date;

// author : Chen Bao Xiong

// This is a beta version .

public class ClientView extends JApplet{

private JTextField jtfEnterName,jtfPlayer1,jtfPlayer2;

private JLabel jlblBS,jlblEnterName,jlblPlayer1,jlblPlayer2,jlblDate,jlblEmpty,jlblEmpty2,jlblStatus,jlblWelcome;

private JTextArea jtaMsg;

//private Cell[][] cell = new Cell[5][4];

private JPanel jpFull,jpTop,jpBottom,jpLabel,jpMsg,jpDetails,jpDateSend,jpDate,jpSend;

private JButton jbtnSend;

private Cell[][] cell = new Cell[5][4];

private BufferedReader fromServer;

private PrintWriter toServer;

private String wait = "Status : waiting. ";

private String blank = "";

private JTextArea jtaLog;

private JScrollPane scrollpane;

private ClientModel cm;

private Thread battleship;

public void init()

{

jpFull = new JPanel();

jpFull.setLayout(new GridLayout(2,1));//int rows, int cols Overall

jpTop = new JPanel();

jpTop.setLayout(new GridLayout(3,1)); //3 Rows , 1 Column

jpLabel = new JPanel();

jpLabel.setLayout(new GridLayout(2,1));

jlblBS = new JLabel(" ~* BattleShip *~ ");

jlblBS.setFont(new Font("SansSerif", Font.BOLD, 28));

jlblBS.setBorder(new LineBorder(Color.blue, 3));

jlblWelcome = new JLabel(" Welcome to the game ! ");

jpLabel.add(jlblBS);

jpLabel.add(jlblWelcome);

jpMsg = new JPanel();

jpMsg.setLayout(new BorderLayout(1,1));

JTextArea jtaLog = new JTextArea();

JScrollPane scrollPane = new JScrollPane(jtaLog);

jtaLog.append("Welcome to BattleShip ." +'\n');

jpMsg.add(scrollPane, BorderLayout.CENTER);

jpDateSend = new JPanel();

jpDateSend.setLayout(new GridLayout(2,1));

jpDate = new JPanel();

jpDate.setLayout(new GridLayout(1,1));

jlblDate = new JLabel("Game Started at : " +new Date());

jpDate.add(jlblDate);

jpDateSend.add(jpDate);

jpSend = new JPanel();

jpSend.setLayout(new GridLayout(1,2));

jbtnSend = new JButton("Connect");

jbtnSend.setForeground(Color.white);

jbtnSend.setBackground(Color.black);

jlblStatus = new JLabel(wait);

jlblStatus.setBorder(new LineBorder(Color.black, 2));

jpSend.add(jlblStatus);

//adding Send() to jbtnSend...

jbtnSend.addActionListener(new Connect());

jpSend.add(jbtnSend);

jpDateSend.add(jpSend);

jpTop.add(jpLabel);

jpTop.add(jpMsg);

jpTop.add(jpDateSend);

jpFull.add(jpTop);

jpBottom = new JPanel();

jpBottom.setLayout(new GridLayout(5,4));

for (int i=0; i<5; i++)

for (int j=0; j<4; j++)

jpBottom.add(cell[j] = new Cell(i, j));

//Formating jpBottom//

jpBottom.setBorder(new LineBorder(Color.pink, 1));

jpFull.add(jpBottom);

getContentPane().add(jpFull);

}// ####### GUI PART ####### //

class Cell extends JButton

{

// Indicate the row and column of this cell in the board

private int row;

private int column;

public Cell(int r, int c)

{

row = r;

column = c;

setBorder(new LineBorder(Color.black, 1)); // Set cell's border

setText("Map ("+row+", "+column+")");

setBackground(Color.green);

setForeground(Color.black);

//addActionListener(new CellListener());

}//end public cell

}//end inner class cell

public class Connect implements ActionListener{

public void actionPerformed(ActionEvent e)

{

if(jlblStatus.getText().equals(wait))

{

cm = new ClientModel("localhost",8000);

jlblStatus.setText("Status : waiting for server.");

Battleship thread = new Battleship();

thread.start();

}

}//end class send

}

class Battleship extends Thread{

public void run()

{

String playerStatus = cm.readFromServer();

jlblStatus.setText(playerStatus);

jtaLog.append("Testing 1 2 3");

}

}

}//end ClientView class

My jtaLog.append("Testing 1 2 3"); cant append to the jtaLog , how should i change the code to access the void init jtaLog to do a append at void run()?

[4461 byte] By [baokya] at [2007-11-27 11:17:33]
# 1

If you post your code, here, you are going to want someone to be able to read it easily. This will greatly improve your chances that someone smart will take the time to read it and comment on it. So to make your code readable, please surround your code with code tags when posting. You can find out about them here:

http://forum.java.sun.com/help.jspa?sec=formatting

petes1234a at 2007-7-29 14:25:39 > top of Java-index,Java Essentials,Java Programming...
# 2

jpMsg = new JPanel();

jpMsg.setLayout(new BorderLayout(1,1));

JTextArea jtaLog = new JTextArea();

JScrollPane scrollPane = new JScrollPane(jtaLog);

jtaLog.append("Welcome to BattleShip ." +'\n');

jpMsg.add(scrollPane, BorderLayout.CENTER);

AT THIS PART , the append to jtaLog is sucessful * it is under init()

jtaLog.append("Welcome to BattleShip ." +'\n');

But when i try to append at public void run section , i cant append sucessfully.

Theres no Compilation problem , but the problem is that the append is not working...

I tested the readFromServer and i can capture the string , just that i cant append to my jta on init

public void run()

{

String playerStatus = cm.readFromServer();

jlblStatus.setText(playerStatus);

jtaLog.append("Testing 1 2 3");

}

Message was edited by:

baoky

Message was edited by:

baoky

baokya at 2007-7-29 14:25:39 > top of Java-index,Java Essentials,Java Programming...
# 3

You forgot to initialize your jtextarea

private JTextArea jtaLog = new JTextArea(); //*** need this!!!

It remains null until it is constructed.

petes1234a at 2007-7-29 14:25:39 > top of Java-index,Java Essentials,Java Programming...
# 4

private JTextArea jtaLog;

i got declare this under my constructor.. was it wrongly declare?

I can append at the init part

but not the void run part , i try append at eg my connect actionlistener also cant work ..

but at init it can append ?

thanks for your reply :)

baokya at 2007-7-29 14:25:39 > top of Java-index,Java Essentials,Java Programming...
# 5

you forgot to instanciate the JTextArea jtaMsg component and add it to your view

take a look at this code and add the missing component

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.LineBorder;

import java.io.*;

import java.net.*;

import java.util.Date;

// author : Chen Bao Xiong

// This is a beta version .

public class ClientView extends JApplet {

private JTextField jtfEnterName, jtfPlayer1, jtfPlayer2;

private JLabel jlblBS, jlblEnterName, jlblPlayer1, jlblPlayer2, jlblDate, jlblEmpty, jlblEmpty2, jlblStatus,

jlblWelcome;

private JTextArea jtaMsg;

// private Cell[][] cell = new Cell[5][4];

private JPanel jpFull, jpTop, jpBottom, jpLabel, jpMsg, jpDetails, jpDateSend, jpDate, jpSend;

private JButton jbtnSend;

private Cell[][] cell = new Cell[5][4];

private BufferedReader fromServer;

private PrintWriter toServer;

private String wait = "Status : waiting. ";

private String blank = "";

private JTextArea jtaLog;

private JScrollPane scrollpane;

private ClientModel cm;

private Thread battleship;

public void init() {

jpFull = new JPanel();

jpFull.setLayout(new GridLayout(2, 1));// int rows, int cols Overall

jpTop = new JPanel();

jpTop.setLayout(new GridLayout(3, 1)); // 3 Rows , 1 Column

jpLabel = new JPanel();

jpLabel.setLayout(new GridLayout(2, 1));

jlblBS = new JLabel(" ~* BattleShip *~ ");

jlblBS.setFont(new Font("SansSerif", Font.BOLD, 28));

jlblBS.setBorder(new LineBorder(Color.blue, 3));

jlblWelcome = new JLabel(" Welcome to the game ! ");

jpLabel.add(jlblBS);

jpLabel.add(jlblWelcome);

jpMsg = new JPanel();

jpMsg.setLayout(new BorderLayout(1, 1));

jtaLog = new JTextArea();

JScrollPane scrollPane = new JScrollPane(jtaLog);

jtaLog.append("Welcome to BattleShip ." + '\n');

jpMsg.add(scrollPane, BorderLayout.CENTER);

jpDateSend = new JPanel();

jpDateSend.setLayout(new GridLayout(2, 1));

jpDate = new JPanel();

jpDate.setLayout(new GridLayout(1, 1));

jlblDate = new JLabel("Game Started at : " + new Date());

jpDate.add(jlblDate);

jpDateSend.add(jpDate);

jpSend = new JPanel();

jpSend.setLayout(new GridLayout(1, 2));

jbtnSend = new JButton("Connect");

jbtnSend.setForeground(Color.white);

jbtnSend.setBackground(Color.black);

jlblStatus = new JLabel(wait);

jlblStatus.setBorder(new LineBorder(Color.black, 2));

jpSend.add(jlblStatus);

// adding Send() to jbtnSend...

jbtnSend.addActionListener(new Connect());

jpSend.add(jbtnSend);

jpDateSend.add(jpSend);

jpTop.add(jpLabel);

jpTop.add(jpMsg);

jpTop.add(jpDateSend);

jpFull.add(jpTop);

jpBottom = new JPanel();

jpBottom.setLayout(new GridLayout(5, 4));

int i=0;////////////////////CHANGES HERE

int j=0;

for (i = 0; i < 5; i++)

for (j = 0; j < 4; j++)

cell[i][j] = new Cell(i, j);

jpBottom.add(cell[i][j]);

// Formating jpBottom//

jpBottom.setBorder(new LineBorder(Color.pink, 1));

jpFull.add(jpBottom);

getContentPane().add(jpFull);

}// ####### GUI PART ####### //

class Cell extends JButton {

// Indicate the row and column of this cell in the board

private int row;

private int column;

public Cell(int r, int c) {

row = r;

column = c;

setBorder(new LineBorder(Color.black, 1)); // Set cell's border

setText("Map (" + row + ", " + column + ")");

setBackground(Color.green);

setForeground(Color.black);

// addActionListener(new CellListener());

}// end public cell

}// end inner class cell

public class Connect implements ActionListener {

public void actionPerformed(ActionEvent e)

{

if (jlblStatus.getText().equals(wait)) {

cm = new ClientModel("localhost", 8000);

jlblStatus.setText("Status : waiting for server.");

Battleship thread = new Battleship();

thread.start();

}

}// end class send

}

class Battleship extends Thread {

public void run() {

String playerStatus = cm.readFromServer();

jlblStatus.setText(playerStatus);

jtaLog.append("Testing 1 2 3");

}

}

}// end ClientView class

java_2006a at 2007-7-29 14:25:39 > top of Java-index,Java Essentials,Java Programming...
# 6

***

Message was edited by:

petes1234

petes1234a at 2007-7-29 14:25:39 > top of Java-index,Java Essentials,Java Programming...
# 7

> private JTextArea jtaLog;

>

> i got declare this under my constructor.. was it

> wrongly declare?

>

> I can append at the init part

>

> but not the void run part , i try append at eg my

> connect actionlistener also cant work ..

>

> but at init it can append ?

>

> thanks for your reply :)

what constructor? You have to do it in init like I said, or put it in a constructor and call that constructor

petes1234a at 2007-7-29 14:25:39 > top of Java-index,Java Essentials,Java Programming...