Created
February 25, 2013 06:25
-
-
Save resarahadian/5028118 to your computer and use it in GitHub Desktop.
ComboBox Dinamis Java Swing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.EventQueue; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.UnsupportedLookAndFeelException; | |
import javax.swing.border.EmptyBorder; | |
import javax.swing.ImageIcon; | |
import javax.swing.JComboBox; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JTextField; | |
import javax.swing.JButton; | |
import java.awt.event.ActionListener; | |
import java.awt.event.ActionEvent; | |
import java.awt.Color; | |
@SuppressWarnings("serial") | |
public class FrameComboDinamis extends JFrame | |
{ | |
private JPanel contentPane; | |
@SuppressWarnings("rawtypes") | |
private JComboBox cbJudulBuku; | |
String judul[] = {"Java Programming with GUI Make Over","Do'a Membawa Berkah","Jaga Mata, Jaga Telinga, Jaga Mulut"}; | |
private JTextField txtId; | |
private JLabel lblIdBuku; | |
private JLabel lblJudulbuku; | |
private JLabel lblPenerbit; | |
private JTextField txtPenerbit; | |
String id,judulBuku,penerbit; | |
private JLabel lblIcon; | |
private JLabel lblIcon_2; | |
/** | |
* Create the frame. | |
*/ | |
public FrameComboDinamis() | |
{ | |
setResizable(false); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setBounds(100, 100, 610, 341); | |
contentPane = new JPanel(); | |
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | |
setContentPane(contentPane); | |
contentPane.setLayout(null); | |
cbJudulBuku = new ModifyComboBox(judul); | |
cbJudulBuku.setBounds(116, 55, 155, 24); | |
contentPane.add(cbJudulBuku); | |
lblIdBuku = new JLabel("ID Buku : "); | |
lblIdBuku.setForeground(new Color(255, 255, 255)); | |
lblIdBuku.setBounds(12, 12, 70, 18); | |
contentPane.add(lblIdBuku); | |
lblJudulbuku = new JLabel("JudulBuku : "); | |
lblJudulbuku.setForeground(new Color(255, 255, 255)); | |
lblJudulbuku.setBounds(12, 58, 93, 18); | |
contentPane.add(lblJudulbuku); | |
txtId = new JTextField(); | |
txtId.setBounds(116, 8, 149, 27); | |
contentPane.add(txtId); | |
txtId.setColumns(10); | |
lblPenerbit = new JLabel("Penerbit : "); | |
lblPenerbit.setForeground(new Color(255, 255, 255)); | |
lblPenerbit.setBounds(12, 101, 80, 15); | |
contentPane.add(lblPenerbit); | |
txtPenerbit = new JTextField(); | |
txtPenerbit.setBounds(116, 95, 301, 27); | |
contentPane.add(txtPenerbit); | |
txtPenerbit.setColumns(10); | |
JButton btnTampilkan = new JButton("Tampilkan"); | |
btnTampilkan.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/tampilData.png")); | |
btnTampilkan.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
id = txtId.getText(); | |
if(cbJudulBuku.getSelectedIndex() == 0) | |
{ | |
judulBuku = "Java Programming with GUI Make Over"; | |
} | |
else if(cbJudulBuku.getSelectedIndex() == 1) | |
{ | |
judulBuku = "Do'a Membawa Berkah"; | |
} | |
if(cbJudulBuku.getSelectedIndex() == 2) | |
{ | |
judulBuku = "Jaga Mata, Jaga Telinga, Jaga Mulut"; | |
} | |
penerbit = txtPenerbit.getText(); | |
JOptionPane.showMessageDialog(null,"ID Buku : " + id + "\n" + "Judul Buku : " + judulBuku + "\n" + "Penerbit : " + penerbit,"Pesan",JOptionPane.INFORMATION_MESSAGE,new ImageIcon("src/Gambar/success.png")); | |
} | |
}); | |
btnTampilkan.setBounds(149, 196, 125, 38); | |
contentPane.add(btnTampilkan); | |
JButton btnNewButton = new JButton("Refresh"); | |
btnNewButton.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/refresh.png")); | |
btnNewButton.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
txtId.setText(""); | |
cbJudulBuku.setSelectedIndex(0); | |
txtPenerbit.setText(""); | |
txtId.requestFocus(); | |
} | |
}); | |
btnNewButton.setBounds(286, 196, 125, 38); | |
contentPane.add(btnNewButton); | |
lblIcon = new JLabel(""); | |
lblIcon.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/buku.png")); | |
lblIcon.setBounds(454, 41, 136, 142); | |
contentPane.add(lblIcon); | |
lblIcon_2 = new JLabel(""); | |
lblIcon_2.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/pencil.png")); | |
lblIcon_2.setBounds(12, 197, 125, 98); | |
contentPane.add(lblIcon_2); | |
JLabel lblBg = new JLabel(""); | |
lblBg.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/bgBlur.png")); | |
lblBg.setBounds(0, 0, 608, 314); | |
contentPane.add(lblBg); | |
setLocationRelativeTo(null); | |
} | |
/** | |
* Launch the application. | |
*/ | |
public static void main(String[] args) | |
{ | |
EventQueue.invokeLater(new Runnable() | |
{ | |
public void run() | |
{ | |
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
if ("com.sun.java.swing.plaf.gtk.GTKLookAndFeel".equals(info.getClassName())) { | |
try | |
{ | |
javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
FrameComboDinamis frame = new FrameComboDinamis(); | |
frame.setVisible(true); | |
} | |
catch (ClassNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (InstantiationException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (UnsupportedLookAndFeelException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
break; | |
} | |
} | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Component; | |
import java.awt.Dimension; | |
import javax.swing.JComboBox; | |
import javax.swing.JList; | |
@SuppressWarnings({ "serial", "rawtypes" }) | |
public class ModifyComboBox extends JComboBox | |
{ | |
@SuppressWarnings("unchecked") | |
public ModifyComboBox(String[] param) | |
{ | |
super(param); | |
} | |
public Dimension getSize() | |
{ | |
Dimension dimensi = super.getSize(); | |
int wide = getLebarCombo(); | |
dimensi.width = Math.max(wide, dimensi.width); | |
return dimensi; | |
} | |
public int getLebarCombo() | |
{ | |
int l = 0; | |
int a; | |
for(a=0;a<this.getItemCount();a++) | |
{ | |
@SuppressWarnings("unchecked") | |
Component komponen = this.getRenderer().getListCellRendererComponent(new JList(this.getModel()), this.getItemAt(a), a, false, false); | |
//(JList<? extends E> list, E value,int index,boolean isSelected,boolean cellHasFocus) | |
l = Math.max(l, komponen.getPreferredSize().width); | |
} | |
return l + 10; //Margin lebar combo ditambah 10 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment