Cara Melewatkan Data, Array, Variabel dari Kelas A ke Kelas B

cara untuk melewatkan data dari class A ke kelas B, adalah dengan membuat method getter dan setternya..
dari kelas A, kita buat button actionnya, yang isinya kurang lebih seperti berikut :

int jumcent = tblCent.getRowCount();
Float[][] cent2 = new Float[jumcent][7];
System.arraycopy(cent, 0, cent2, 0, cent.length);
for(Frame frame: Frame.getFrames()){
if(frame.getTitle().equals(“Form Pengujian”)){
Pengujian uji = (Pengujian) frame;
uji.setJumCent(jumcent);
uji.setCent(cent);
}
}

lalu dari kelas B yang akan menerima data, kita buat method getter and setter nya, seperti berikut :
public class Pengujian extends javax.swing.JFrame {
int jumCent;
Float[][] cent = new Float[jumCent][7];

public int getJumCent(){
return jumCent;
}

public void setJumCent(int jumCent){
this.jumCent = jumCent;
}

public Float[][] getCent(){
FUtama a = new FUtama();
a.getCentTerakhir();
return cent;
}
public void setCent(Float[][] cent){
this.cent = cent;
tampilkan();
}
public void tampilkan(){
String[] kolom = {“id”,”POR”,”JK”,”JB”,”V”,”H”,”M”,”KELAS”};
DefaultTableModel model = new DefaultTableModel(kolom, 0);

for (int i = 0; i < cent.length; i++) {

area2.append(“no. “+i+”\t”+cent[i][1]+”\t”+cent[i][2]+”\t”+cent[i][3]+”\t”+cent[i][4]+”\t”+cent[i][5]+”\t”+cent[i][6]+”\n”);
model.addRow(new Object[] {i+1,cent[i][1],cent[i][2],cent[i][3],cent[i][4],cent[i][5],cent[i][6]});
tblCent.setModel(model);

}
}

Tinggal atur sesuai kebutuhan. Dapat juga seperti berikut implementasinya :
dari kelas A, kita buat button actionnya :
/* Get text from TextField */
String message = textField.getText();
String setNewMessage = “”;
boolean isFrameOpen = false;

if (message != null && message.length() > 0) {
/**
* Get list of opened JFrames
* Read more about “How to get list of opened JFrame in Java Swing?”
* http://www.javaquery.com/2014/06/how-to-get-list-of-opened-jframe-in.html
*/
for (Frame frame : Frame.getFrames()) {
/* Match title of JFrame */
if (frame.getTitle().equals(“Child JFrame”)) {
/* Cast to ChildJFrame */
ChildJFrame childJFrame = (ChildJFrame) frame;
/**
* Access TextArea of ChildJFrame using getter method.
* You can so same for other component like JLabel, Buttons,
* Combobox, List by creating getter and setter method.
*/
String getPreviousMessages = childJFrame.getTextArea().getText();
if (getPreviousMessages.length() > 0) {
/* Append new message to String */
setNewMessage = getPreviousMessages + “\n[” + new Date().toString() + “]: ” + message;
} else {
/* Create new message String */
setNewMessage = “[” + new Date().toString() + “]: ” + message;
}

/* Set value of TextArea */
childJFrame.getTextArea().setText(setNewMessage);
isFrameOpen = true;
}
}

/* If ChildJFrame is not opened yet */
if (!isFrameOpen) {
/* Create new message String */
setNewMessage = “[” + new Date().toString() + “]: ” + message;
/* Create an object of ChildJFrame */
ChildJFrame childJFrame = new ChildJFrame();
/* Set message in TextArea */
childJFrame.getTextArea().setText(setNewMessage);
/* Make ChildJFrame visible */
childJFrame.setVisible(true);
}
} else {
/* Alert box when user try to send blank message */
JOptionPane.showMessageDialog(null, “Type something to send”);
}
}
lalu dari kelas b, kita buat method getter dan setter nya :
public javax.swing.JTextArea getTextArea() {
return textArea;
}

public void setTextArea(javax.swing.JTextArea textArea) {
this.textArea = textArea;
}

sekian semoga bermanfaat….

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s