Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created November 22, 2013 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resarahadian/7592272 to your computer and use it in GitHub Desktop.
Save resarahadian/7592272 to your computer and use it in GitHub Desktop.
PopUp Button Java
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;
@SuppressWarnings("serial")
public class framePopUpButton extends JFrame {
private JPanel contentPane;
private JButton btnAksi;
private JPopupMenu popUp;
private JMenuItem miSimpan, miUbah, miHapus;
private JLabel lblKdBarang;
private JTextField txtKdBarang;
private JLabel lblNama;
private JTextField txtNama;
private JLabel lblSatuan;
private JTextField txtSatuan;
private JTextField txtJumlah;
private JLabel lblWall;
private JLabel lblIcon;
/**
* Create the frame.
*/
public framePopUpButton()
{
setTitle("PopUp Button");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 395);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
popUp = new JPopupMenu();
miSimpan = new JMenuItem("Simpan", new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/popUpButton/Simpan.png"));
miUbah = new JMenuItem("Ubah" , new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/popUpButton/ubah.png"));
miHapus = new JMenuItem("Hapus" , new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/popUpButton/hapus.png"));
popUp.add(miSimpan);
popUp.add(miUbah);
popUp.add(miHapus);
ActionListener aksi = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent act) {
// TODO Auto-generated method stub
tampilPopUp(act);
}
};
btnAksi = new JButton("Aksi");
btnAksi.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/popUpButton/aksi.png"));
btnAksi.addActionListener(aksi);
btnAksi.setBounds(244, 285, 117, 44);
contentPane.add(btnAksi);
lblKdBarang = new JLabel("Kd Barang : ");
lblKdBarang.setForeground(new Color(255, 255, 255));
lblKdBarang.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
lblKdBarang.setBounds(16, 12, 98, 15);
contentPane.add(lblKdBarang);
txtKdBarang = new JTextField();
txtKdBarang.setBounds(110, 6, 251, 27);
contentPane.add(txtKdBarang);
txtKdBarang.setColumns(10);
lblNama = new JLabel("Nama : ");
lblNama.setForeground(new Color(255, 255, 255));
lblNama.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
lblNama.setBounds(16, 52, 60, 15);
contentPane.add(lblNama);
txtNama = new JTextField();
txtNama.setBounds(110, 45, 318, 27);
contentPane.add(txtNama);
txtNama.setColumns(10);
lblSatuan = new JLabel("Satuan : ");
lblSatuan.setForeground(new Color(255, 255, 255));
lblSatuan.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
lblSatuan.setBounds(16, 98, 60, 15);
contentPane.add(lblSatuan);
txtSatuan = new JTextField();
txtSatuan.setBounds(110, 92, 122, 27);
contentPane.add(txtSatuan);
txtSatuan.setColumns(10);
JLabel lblJumlah = new JLabel("Jumlah : ");
lblJumlah.setForeground(new Color(255, 255, 255));
lblJumlah.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
lblJumlah.setBounds(16, 147, 60, 15);
contentPane.add(lblJumlah);
txtJumlah = new JTextField();
txtJumlah.setBounds(110, 141, 122, 27);
contentPane.add(txtJumlah);
txtJumlah.setColumns(10);
lblIcon = new JLabel("");
lblIcon.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/popUpButton/file.png"));
lblIcon.setBounds(389, 127, 152, 135);
contentPane.add(lblIcon);
lblWall = new JLabel("New label");
lblWall.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/popUpButton/wall.png"));
lblWall.setBounds(0, 0, 606, 370);
contentPane.add(lblWall);
setLocationRelativeTo(null);
}
public void tampilPopUp(ActionEvent act)
{
Component comp = (Component) act.getSource();
Point p = comp.getLocationOnScreen();
popUp.show(this,0,0);
popUp.setLocation(p.x, p.y + comp.getHeight());
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
framePopUpButton frame = new framePopUpButton();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment