Displaying data from an object to jTextArea of another class

hi i,m new to java and i have this problem

i have 3 public classes: Klient,ClassDataArray and NewJframe(Gui)

the client class defines the klient object() the ClassDataArray makes an array of objects(klient) and the gui gets data and displays them

My problem is : I don't know how to display inserted Klient object

in class Klient i have this method that displays data as u see but only in System.out.print..ect..

publicvoid dispKlient(){

System.out.println(" Imię : " + imie);

System.out.println(" Nazwisko : " + nazwisko);

System.out.println(" Adres : " + adres);

System.out.println(" Kod Pocztowy : " + kodPocztowy+"\n");

}

but i want this data to be displayed in jTextArea1 of NewJFrame

how do i implement this?

i really have no clue:(

here is the source code.

publicclass Klient{

public String imie;

public String nazwisko;

public String adres;

public String kodPocztowy;

public Klient(String i, String n, String a, String kodp)

{

nazwisko=n;

imie=i;

adres=a;

kodPocztowy=kodp;

}

publicvoid dispKlient(){

System.out.println(" Imię : " + imie);

System.out.println(" Nazwisko : " + nazwisko);

System.out.println(" Adres : " + adres);

System.out.println(" Kod Pocztowy : " + kodPocztowy+"\n");

}

public String pobierzImie()

{

return imie;

}

public String pobierzNazwisko()

{

return nazwisko;

}

public String pobierzAdres()

{

return adres;

}

public String pobierzkodP()

{

return kodPocztowy;

}

publicstaticvoid main(String args[]){}

}

-

this class is ok i think it only insetrs, deletes and search objects

publicclass ClassDataArray{

private Klient[] a;

privateint nElems;

/** Creates a new instance of ClassDataArray */

public ClassDataArray(int max ){

a=new Klient[max];

nElems=0;

}

public Klient find(String searchName){

int j;

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

if(a[j].pobierzImie().equals(searchName))

break;

if(j==nElems)

returnnull;

else{

return a[j];

}}

publicvoid insert(String imie, String nazwisko, String adres, String kodPocztowy){

a[nElems] =new Klient(imie, nazwisko, adres, kodPocztowy);

nElems++;

}

publicboolean delete(String searchName){

int j;

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

if(a[j].pobierzImie().equals(searchName))

break;

if(j==nElems)

returnfalse;

else{

for(int k=j;k<nElems;k++)

a[k] = a[k+1];

nElems--;

returntrue;

}

}

publicvoid displayA()

{

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

a[j].dispKlient();

}

}

this class ia almost all generated with NetBeans 5.5

==========================================================

publicclass NewJFrameextends javax.swing.JFrame{

publicstatic String imie;

publicstatic String nazwisko;

publicstatic String adres;

publicstatic String kod;

publicstatic ClassDataArray arr;

publicstatic String text;

/** Creates new form NewJFrame */

public NewJFrame(){

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// ><editor-fold defaultstate="collapsed" desc=" Generated Code ">

privatevoid initComponents(){

jPanel1 =new javax.swing.JPanel();

jLabel1 =new javax.swing.JLabel();

jTextField1 =new javax.swing.JTextField();

jLabel2 =new javax.swing.JLabel();

jTextField2 =new javax.swing.JTextField();

jLabel3 =new javax.swing.JLabel();

jLabel4 =new javax.swing.JLabel();

jTextField3 =new javax.swing.JTextField();

jTextField4 =new javax.swing.JTextField();

jScrollPane1 =new javax.swing.JScrollPane();

jTextArea1 =new javax.swing.JTextArea();

jButton2 =new javax.swing.JButton();

jButton1 =new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Imi\u0119");

jTextField1.setText("jTextField1");

jLabel2.setText("Nazwisko");

jTextField2.setText("jTextField2");

jLabel3.setText("Adres");

jLabel4.setText("Kod Pocztowy");

jTextField3.setText("jTextField3");

jTextField4.setText("jTextField4");

jTextArea1.setColumns(20);

jTextArea1.setFont(new java.awt.Font("Arial", 1, 12));

jTextArea1.setRows(5);

jScrollPane1.setViewportView(jTextArea1);

javax.swing.GroupLayout jPanel1Layout =new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)

.addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)

.addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)

.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3)

.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4)

.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))

.addContainerGap())

);

jButton2.setText("Wyswietl Dane klienta");

jButton2.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

jButton2ActionPerformed(evt);

}

});

jButton1.setText("Dodaj Klienta");

jButton1.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

jButton1ActionPerformed(evt);

}

});

javax.swing.GroupLayout layout =new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addComponent(jButton1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 185, Short.MAX_VALUE)

.addComponent(jButton2)))

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton2)

.addComponent(jButton1))

.addContainerGap())

);

pack();

}// </editor-fold>

privatevoid jButton2ActionPerformed(java.awt.event.ActionEvent evt){

arr.displayA();

// TODO add your handling code here:

}

privatevoid jButton1ActionPerformed(java.awt.event.ActionEvent evt){

String imie=jTextField1.getText();

String nazwisko=jTextField2.getText();

String adres=jTextField3.getText();

String kodp=jTextField4.getText();

arr.insert(imie,nazwisko,adres,kodp);

// TODO add your handling code here:

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new NewJFrame().setVisible(true);

int maxSize=100;

arr =new ClassDataArray(maxSize);

}

});

// int maxSize=100;

// ClassDataArray arr;

// arr = new ClassDataArray(maxSize);

// arr.insert("ttttt","ttttt","ttttt","tttt");

// arr.displayA();

}

// Variables declaration - do not modify

javax.swing.JButton jButton1;

javax.swing.JButton jButton2;

javax.swing.JLabel jLabel1;

javax.swing.JLabel jLabel2;

javax.swing.JLabel jLabel3;

javax.swing.JLabel jLabel4;

javax.swing.JPanel jPanel1;

javax.swing.JScrollPane jScrollPane1;

javax.swing.JTextArea jTextArea1;

javax.swing.JTextField jTextField1;

javax.swing.JTextField jTextField2;

javax.swing.JTextField jTextField3;

javax.swing.JTextField jTextField4;

// End of variables declaration

}

Thank you

[18107 byte] By [Tottigola] at [2007-11-27 4:13:49]
# 1

I can't run the program because I don't use netbeans, but this should work

//arr = new ClassDataArray(maxSize);

arr = new ClassDataArray(maxSize,jTextArea1);//<-1

public class ClassDataArray {

private Klient[] a;

private int nElems;

JTextArea textArea;//<-2

//public ClassDataArray(int max ){

public ClassDataArray(int max,JTextArea ta ) {//<-3

textArea = ta;//<-4

a= new Klient[max];

nElems=0;

}

public void displayA()

{

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

//a[j].dispKlient();

a[j].dispKlient(textArea);//><-5

}

//public void dispKlient(){

public void dispKlient(JTextArea textArea){//<6

//System.out.println(" Imie; : " + imie);

textArea.append(" Imie; : " + imie);//<--7

textArea.append("\n Nazwisko : " + nazwisko);

textArea.append("\n Adres : " + adres);

textArea.append("\n Kod Pocztowy : " + kodPocztowy+"\n");

}

Michael_Dunna at 2007-7-12 9:20:07 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks:) i fell like i'm closer to the point but:

i have some litle errors

arr = new ClassDataArray(maxSize,jTextArea1);//<-1 error "non static variable jTextArea1 cannot be referenced from a static context

public class ClassDataArray {

private Klient[] a;

private int nElems;

JTextArea textArea;//<-2 error cannot find symbol : class JTextArea location: class javaapplication4.ClassDataArray

//public ClassDataArray(int max ){

public ClassDataArray(int max,JTextArea ta ) {//<-3 the same error as at point "2"

textArea = ta;//<-4

a= new Klient[max];

nElems=0;

}

public void displayA()

{

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

//a[j].dispKlient();

a[j].dispKlient(textArea);//><-5

}

//public void dispKlient(){

public void dispKlient(JTextArea textArea){//<6 the same error as in piont 2, 3

//System.out.println(" Imie; : " + imie);

textArea.append(" Imie; : " + imie);//<--7

textArea.append("\n Nazwisko : " + nazwisko);

textArea.append("\n Adres : " + adres);

textArea.append("\n Kod Pocztowy : " + kodPocztowy+"\n");

}

Tottigola at 2007-7-12 9:20:07 > top of Java-index,Desktop,Core GUI APIs...
# 3

welcome the world of netbeans :-)

most of the errors look like they can be fixed by using fully qualified names e.g.

JTextArea textArea

becomes

javax.swing.JTextArea textArea

the first error probably would be fixed by changing

javax.swing.JTextArea jTextArea1;

to

static javax.swing.JTextArea jTextArea1;

but it might be better to move both these lines

int maxSize=100;

arr = new ClassDataArray(maxSize,jTextArea1);

to the bottom of initComponents()

Michael_Dunna at 2007-7-12 9:20:07 > top of Java-index,Desktop,Core GUI APIs...
# 4

thank you very much.

i've updated my code

i had to insert import javax.swing.*;

to my Klient class and ClassDataArray and it worked:) thank very much very very much

i have one more little question:) is the "world of netbeans" bad for a beginner?:) should i use another IDE?

Tottigola at 2007-7-12 9:20:07 > top of Java-index,Desktop,Core GUI APIs...
# 5

> is the "world of netbeans" bad for a beginner?:)

> should i use another IDE?

Beginners shouldn't use any IDE until they're comfortable with the basics of the language.

Notepad is fine.

Netbeans produces terrible, long-winded code.

The code is very difficult to read/maintain/modify, as evidenced by the many posts in this forum.

All Netbeans does is give some a false idea that they're a programmer, when

all they can do is click-drag-drop.

Netbeans is fine for those who can whip up a program in say, Notepad, but

want to do it very quickly, or get an idea of what a proposed gui might look like.

Which IDE is best - I'd go with one with a good link to the api docs. Search

the forums/google for 'best ide', there are countless posts.

Michael_Dunna at 2007-7-12 9:20:07 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thank you. You helped e a lot:)
Tottigola at 2007-7-12 9:20:07 > top of Java-index,Desktop,Core GUI APIs...