Java GUI Swing Tutorial Part 19.4 | Menu Bar,Menu,Popup Menu
Java GUI Swing Tutorial Part 19.4 | Menu Bar,Menu,Popup Menu
Tutorial Cover:
1.How to Create Menu Bar in Java Swing
2.Menu Bar Java Swing
3.Menu Item in Java Swing
4.Sub Menu in Java Swing
5.How to Create Sub Menu in Java Swing
6.Java Swing Popup Menu Example
7.Java Menu Bar and Popup Menu Bar Example Tutorial
8.Cut Copy Paste Functionality in Input Box in Java
9.Mouse Event Handle Java Swing
Tutorial Link : https://youtu.be/mhPGxzY8zW0
Join Our Telegram Channel to Stay Updated : https://t.me/supercoders
Source Code : https://github.com/hackstarsj/JavaGUISwingTutorial
Menu Bar Source Code :
import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SwingMenuBarExample { public static void main(String args[]){ System.out.println("Menu Bar Example"); JFrame jFrame=new JFrame("Menu Bar Example"); JPanel jPanel=new JPanel(); BoxLayout boxLayout=new BoxLayout(jPanel,BoxLayout.Y_AXIS); EmptyBorder emptyBorder=new EmptyBorder(20,20,20,20); jPanel.setBorder(emptyBorder); jPanel.setLayout(boxLayout); jFrame.add(jPanel); JLabel simpleLabel=new JLabel("Menu Bar Example"); jPanel.add(simpleLabel); //Menu Bar Code JMenuBar jMenuBar=new JMenuBar(); //Menu JMenu menu1=new JMenu("File"); //Menu Item JMenuItem open=new JMenuItem("Open"); JMenuItem save=new JMenuItem("Save"); JMenuItem close=new JMenuItem("Close"); open.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Open"); } }); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Close"); } }); save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Save"); } }); menu1.add(open); menu1.add(close); menu1.add(save); //Menu JMenu editment=new JMenu("Edit"); //Menu Item JMenuItem cut=new JMenuItem("Cut"); JMenuItem copy=new JMenuItem("Copy"); JMenuItem paste=new JMenuItem("Paste"); //Submenu JMenu more=new JMenu("More"); JMenuItem select=new JMenuItem("Select"); JMenuItem select_all=new JMenuItem("Select All"); more.add(select); more.add(select_all); editment.add(cut); editment.add(copy); editment.add(paste); editment.add(more); jMenuBar.add(menu1); jMenuBar.add(editment); //Setting into jframe jFrame.setJMenuBar(jMenuBar); //End menu Bar code jFrame.setSize(300,300); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setVisible(true); } }
Popup Menu Source Code :
import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class SwingPopupMenuExample { String temp_data=""; public static void main(String args[]){ SwingPopupMenuExample swingPopupMenuExample=new SwingPopupMenuExample(); System.out.println("Popup Menu Example"); JFrame jFrame=new JFrame("Popup Menu Example"); JPanel jPanel=new JPanel(); BoxLayout boxLayout=new BoxLayout(jPanel,BoxLayout.Y_AXIS); EmptyBorder emptyBorder=new EmptyBorder(20,20,20,20); jPanel.setBorder(emptyBorder); jPanel.setLayout(boxLayout); jFrame.add(jPanel); JTextField simpleinput=new JTextField(); jPanel.add(simpleinput); //Popup menu Code JPopupMenu jPopupMenu=new JPopupMenu("Edit"); JMenuItem cut=new JMenuItem("Cut"); JMenuItem copy=new JMenuItem("Copy"); JMenuItem paste=new JMenuItem("Paste"); jPopupMenu.add(cut); jPopupMenu.add(copy); jPopupMenu.add(paste); //End Popup Menu Code jPanel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent mouseEvent) { if(SwingUtilities.isRightMouseButton(mouseEvent)){ jPopupMenu.show(jPanel,mouseEvent.getX(),mouseEvent.getY()); } } }); //End Popup Code //Example Popup menu //Popup menu Code JPopupMenu jPopupMenu2=new JPopupMenu("Edit"); JMenuItem cut2=new JMenuItem("Cut"); JMenuItem copy2=new JMenuItem("Copy"); JMenuItem paste2=new JMenuItem("Paste"); jPopupMenu2.add(cut2); jPopupMenu2.add(copy2); jPopupMenu2.add(paste2); //End Popup Menu Code simpleinput.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent mouseEvent) { if(SwingUtilities.isRightMouseButton(mouseEvent)){ jPopupMenu2.show(simpleinput,mouseEvent.getX(),mouseEvent.getY()); } } }); cut2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { swingPopupMenuExample.temp_data=simpleinput.getText(); simpleinput.setText(""); } }); copy2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { swingPopupMenuExample.temp_data=simpleinput.getText(); } }); paste2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { String current_data=simpleinput.getText(); simpleinput.setText(current_data+""+swingPopupMenuExample.temp_data); } }); jFrame.setSize(300,300); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setVisible(true); } }
No comments:
Post a Comment