[Java] – Menggunakan Getter-Setter untuk memanipulasi class lain

Memanfaatkan getter-setter yang disediakan java untuk memanipulasi class lain ?
It’s easy to use.
Try it…
Disini saya mencontohkan dengan kasus bahwa anda mempunyai sebuah popUp window atau JDialog dimana JDialog ini harus bisa memanipulasi class yang jadi parent-nya dalam hal ini JPanel yang jadi container bagi JDialog.
untuk preview langsung saja bisa dilihat di gambar dibawah.


download source code disini

langkah-langkah nya :
1. Buat JFrame 

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.border.EmptyBorder;
public class MainFrame extends JFrame {
private Panel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setTitle(“Getter-Setter by Mazipan”);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
// set panel to frame
panel = new Panel();
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
panel.setLayout(new BorderLayout(0, 0));
setContentPane(panel);
}
}

2. Buat JPanel beserta Getter-Setternya

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class Panel extends JPanel {
private JTextField textField;

/**
* Create the panel.
*/
private Panel panel;
public Panel() {
panel = this;
setLayout(null);

textField = new JTextField();
textField.setBounds(30, 11, 261, 32);
add(textField);
textField.setColumns(10);

JButton btnCallDialog = new JButton(“Call Dialog”);
btnCallDialog.setBounds(30, 58, 81, 23);
add(btnCallDialog);

}

// this called Getter
public JTextField getTextField() {
return textField;
}

// this called Setter
public void setTextField(JTextField textField) {
this.textField = textField;
}

}

3. Buat JDialognya dengan constructor yang reference ke JPanel

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;

public class JDialogPanel extends JDialog {

private final JPanel contentPanel = new JPanel();
private Panel panel;
private JDialogPanel dia;
private JTextField txtThisJtextfieldWill;

/**
* Create the dialog.
*/
public JDialogPanel(final Panel panel) {
dia = this;
this.panel = panel;
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);

txtThisJtextfieldWill = new JTextField();
txtThisJtextfieldWill.setText(“edit this JTextField will modify JTextField on JPanel”);
txtThisJtextfieldWill.setBounds(13, 11, 325, 30);
contentPanel.add(txtThisJtextfieldWill);
txtThisJtextfieldWill.setColumns(10);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton(“OK”);
okButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// this is call getter from panel
// must be same Class
// in sample is JTextField
JTextField txtFromPanel = panel.getTextField();

// this is getting text from JDialog
String Str = txtThisJtextfieldWill.getText();
// this is setText to JTextFied in Panel
txtFromPanel.setText(Str);
// this is close dialog without close applicatiion
dia.dispose();
}
});
okButton.setActionCommand(“OK”);
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton(“Cancel”);
// action for button cancel
cancelButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
dia.dispose();
}
});
cancelButton.setActionCommand(“Cancel”);
buttonPane.add(cancelButton);
}
}
}
}

4. Tinggal buat action di JButton di Panel untuk memanggil JDialog

// this action listener for button
btnCallDialog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// this to call JDialog
JDialogPanel dia = new JDialogPanel(panel);
dia.show();
}
});

mazipan-signature

bahan bacaan lain :
http://stackoverflow.com/questions/1568091/why-use-getters-and-setters
http://stackoverflow.com/questions/17942580/java-setter-getter-and-constructor

Be a good reader, leave your comment please.

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