Kamis, 01 Desember 2011
Source Code Eclipse : Tabel
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.border.Border;
public class tugas extends JFrame{
JCheckBox jcbChin;
JCheckBox jcbGlasses;
JCheckBox jcbHair;
JCheckBox jcbTeeth;
CheckBoxListener myListener = null;
String data[][] = {{"John", "Sutherland", "Student"},
{"George", "Davies", "Student"},
{"Melissa", "Anderson", "Associate"},
{"Stergios", "Maglaras", "Developer"},
};
String fields[] = {"Name", "Surname", "Status"};
public static void main( String[] argv ) {
tugas myExample = new tugas( "JTable Example" );
JFrame frame = new JFrame("JCheckBox Usage Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setContentPane(new tugas());
frame.pack();
frame.setVisible(true);
}
public tugas( String title ) {
super( title );
setSize( 150, 150 );
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent we ) {
dispose();
System.exit( 0 );
}
} );
init();
pack();
setVisible( true );
}
private void init() {
JTable jt = new JTable( data, fields );
JScrollPane pane = new JScrollPane( jt );
getContentPane().add( pane );
}
public tugas() {
// Add an item listener for each of the check boxes.
// This is the listener class which contains business logic
tugas myListener = new tugas();
// Create check boxes with default selection true
jcbChin = new JCheckBox("Chin");
jcbChin.setMnemonic(KeyEvent.VK_C);
//Alt+C Checks/Unchecks the check Box
jcbChin.setSelected(true);
jcbChin.addItemListener((ItemListener) myListener);
jcbGlasses = new JCheckBox("Glasses");
jcbGlasses.setMnemonic(KeyEvent.VK_G);
//Alt+G Checks/Unchecks the check Box
jcbGlasses.setSelected(true);
jcbGlasses.addItemListener((ItemListener) myListener);
jcbHair = new JCheckBox("Hair");
jcbHair.setMnemonic(KeyEvent.VK_H);
//Alt+H Checks/Unchecks the check Box
jcbHair.setSelected(true);
jcbHair.addItemListener((ItemListener) myListener);
jcbTeeth = new JCheckBox("Teeth");
jcbTeeth.setMnemonic(KeyEvent.VK_T);
//Alt+T Checks/Unchecks the check Box
jcbTeeth.setSelected(true);
jcbTeeth.addItemListener( (ItemListener) myListener);
// Put the check boxes in a column in a panel
JPanel jplCheckBox = new JPanel();
jplCheckBox.setLayout(new GridLayout(0, 1)); //0 rows, 1 Column
jplCheckBox.add(jcbChin);
jplCheckBox.add(jcbGlasses);
jplCheckBox.add(jcbHair);
jplCheckBox.add(jcbTeeth);
setLayout(new BorderLayout());
add(jplCheckBox, BorderLayout.WEST);
setBorder(BorderFactory.createEmptyBorder(20,300,20,20));
}
private void setBorder(Border createEmptyBorder) {
// TODO Auto-generated method stub
}
class CheckBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
int index = 0;
char c = '-';
Object source = e.getSource();
if (source == jcbChin) {
index = 0;
c = 'c';
} else if (source == jcbGlasses) {
index = 1;
c = 'g';
} else if (source == jcbHair) {
index = 2;
c = 'h';
} else if (source == jcbTeeth) {
index = 3;
c = 't';
}
if (e.getStateChange() == ItemEvent.DESELECTED)
c = '-';
}
}
}
Hasilnya :
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar