Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Action Listeners
Ollie Dee
Greenhorn
Posts: 10
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Could someone help me write a action or event listener for one of these text fields, either for the Register, Login or Help field as I am having real trouble doing it. Thanks you.
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; // Class implements three different types of listener. /** * @author * @Version 1 28/03/2013 * */ public class MenuBarExample { //ImageIcon icon = new ImageIcon("..\\Example\\images\\imdb.gif"); //JMenuItem item = new JMenuItem(icon); public JPanel createContentPane() { // JPanel panel = new JPanel(); ImagePanel panel1 = new ImagePanel("Images/IMDB1.png"); //ImageIcon icon = new ImageIcon("Images/imdb.gif"); //JPanel totalGUI = new JPanel(); panel1.setBackground(Color.black); panel1.setMinimumSize(new Dimension(720, 200)); panel1.setPreferredSize(new Dimension(720, 200)); panel1.setMaximumSize(new Dimension(720, 200)); panel1.setOpaque(true); return panel1; } public JMenuBar createMenuBar() { //Create the menu bar. JMenuBar menuBar = new JMenuBar(); menuBar.setBackground(Color.WHITE); //Adds a JMenu JMenu movies = new JMenu("Movies"); JMenu tv = new JMenu("TV"); JMenu news = new JMenu("News"); JMenu showTimes = new JMenu("Showtimes"); JMenu community = new JMenu("Community"); JMenu imdbPro = new JMenu("IMDbPro"); JMenu apps = new JMenu("Apps"); menuBar.add(movies); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(tv); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(news); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(showTimes); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(community); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(imdbPro); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(apps); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(new JTextField(" ")); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(new JMenuItem("Register")); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(new JMenuItem("Login")); menuBar.add(new JSeparator(SwingConstants.VERTICAL)); menuBar.add(new JMenuItem("Help")); JMenu inTheaters = new JMenu("In Theaters"); JCheckBoxMenuItem yourTown = new JCheckBoxMenuItem("Your Town"); movies.add(inTheaters); inTheaters.add(yourTown); JMenu top250 = new JMenu("Top 250"); JCheckBoxMenuItem aToZ = new JCheckBoxMenuItem("A to Z"); movies.add(top250); top250.add(aToZ); JMenu usBoxOffice = new JMenu("US Box Office"); JMenuItem blockBusters = new JMenuItem("Block Busters"); JMenuItem highestEarnings = new JMenuItem("Highest Earnings"); movies.add(usBoxOffice); usBoxOffice.add("Block Busters"); usBoxOffice.add("Highest Earnings"); //menuBar.add(new JButton("Search IMDb.com")); //Fill each of the menus. //with nine MenuItems. // JMenuItem inTheaters = new JMenuItem("In Theaters"); // JMenuItem top250 = new JMenuItem("Top 250"); // JMenuItem usBoxOffice = new JMenuItem("US Box Office"); JMenuItem comingSoon = new JMenuItem("Coming Soon"); JMenuItem trailerGallery = new JMenuItem("Trailer Gallery"); JMenuItem watchNowOnAiv = new JMenuItem("Watch now on Aiv"); JMenuItem onDvdAndBlueRay = new JMenuItem("On Dvd & Blue-Ray"); JMenuItem xRayForMovies = new JMenuItem("X-Ray for Movies"); JMenuItem roadToTheOscars = new JMenuItem("Road to the Oscars"); // movies.add(inTheaters); // movies.add(top250); // movies.add(usBoxOffice); movies.add(comingSoon); movies.add(trailerGallery); movies.add(watchNowOnAiv); movies.add(onDvdAndBlueRay); movies.add(xRayForMovies); movies.add(roadToTheOscars); //New menu with three items JMenuItem tvHome = new JMenuItem("TV Home"); //JMenuItem tvListings = new JMenuItem("TV Listings"); // JMenuItem tvEpisodes = new JMenuItem("TV episodes"); tv.add(tvHome); //tv.add(tvListings); // tv.add(tvEpisodes); JMenu tvListings = new JMenu("Tv Listings"); JCheckBoxMenuItem whatsOnTonight = new JCheckBoxMenuItem("Whats on Tonight"); tv.add(tvListings); tvListings.add(whatsOnTonight); JMenu topTvSeries = new JMenu("Top TV Series"); JCheckBoxMenuItem gameOfThrones = new JCheckBoxMenuItem("Game of Thrones"); JCheckBoxMenuItem dexter = new JCheckBoxMenuItem("Dexter"); tv.add(topTvSeries); topTvSeries.add(gameOfThrones); topTvSeries.add(dexter); JMenu tvEpisodes = new JMenu("TV Episodes"); JCheckBoxMenuItem revolution = new JCheckBoxMenuItem("Revolution"); JCheckBoxMenuItem familyGuy = new JCheckBoxMenuItem("Family Guy"); tv.add(tvEpisodes); tvEpisodes.add(revolution); tvEpisodes.add(familyGuy); JMenu cinemas = new JMenu("Cinemas"); JMenuItem odeanCinemas = new JMenuItem("Odean Cinemas"); JMenuItem stormCinemas = new JMenuItem("Storm Cinemas"); showTimes.add(cinemas); cinemas.add(stormCinemas); cinemas.add(odeanCinemas); JMenu weekEndShows = new JMenu("Weekend Shows"); JCheckBoxMenuItem friSatSun = new JCheckBoxMenuItem("Fri Sat Sun"); showTimes.add(weekEndShows); weekEndShows.add(friSatSun); final JPopupMenu popup; popup = new JPopupMenu(); JMenuItem menuItem = new JMenuItem("A popup menu item"); JMenuItem menuItem2 = new JMenuItem("A popup menu item"); JMenuItem menuItem3 = new JMenuItem("A popup menu item"); popup.add(menuItem); popup.add(menuItem2); popup.add(menuItem3); popup.setLocation(100,40); JButton button = new JButton("Show"); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { if(popup.isVisible()) { popup.setVisible(false); } else { popup.setVisible(true); } } }; button.addActionListener(al); button.setBounds(0,0,100,40); createContentPane().add(button); //New menu with six menu items // JMenuItem messageBoards = new JMenuItem("Message Boards"); // JMenuItem newestLists = new JMenuItem("Newest Lists"); // JMenuItem yourLists = new JMenuItem("Your Lists"); // JMenuItem yourRatings = new JMenuItem("Your Ratings"); // JMenuItem contributorZone = new JMenuItem("Contributor Zone"); // JMenuItem quizGame = new JMenuItem("Quiz Game"); // community.add(messageBoards); // community.add(newestLists); // community.add(yourLists); // community.add(yourRatings); // community.add(contributorZone); // community.add(quizGame); JMenu addPhotos = new JMenu ("Add Photos"); JCheckBoxMenuItem yourAlbums = new JCheckBoxMenuItem("Your Albums"); imdbPro.add(addPhotos); addPhotos.add(yourAlbums); JMenu contactInfo = new JMenu ("Contact Info"); JCheckBoxMenuItem registerHere = new JCheckBoxMenuItem("Register Here"); imdbPro.add(contactInfo); contactInfo.add(registerHere); JMenu messageBoards = new JMenu ("Message Boards"); JCheckBoxMenuItem ireland = new JCheckBoxMenuItem("Ireland"); JCheckBoxMenuItem us = new JCheckBoxMenuItem("U.S"); JCheckBoxMenuItem uk = new JCheckBoxMenuItem("U.K"); community.add(messageBoards); messageBoards.add(ireland); messageBoards.add(us); messageBoards.add(uk); JMenu newestLists = new JMenu ("Newest Lists"); JCheckBoxMenuItem usa = new JCheckBoxMenuItem("USA"); JCheckBoxMenuItem europe = new JCheckBoxMenuItem("Europe"); community.add(newestLists); newestLists.add(usa); newestLists.add(europe); JMenu yourLists = new JMenu ("Your Lists"); JCheckBoxMenuItem newMovies = new JCheckBoxMenuItem("New Movies"); JCheckBoxMenuItem tvShows = new JCheckBoxMenuItem("TV Shows"); community.add(yourLists); yourLists.add(newMovies); yourLists.add(tvShows); JMenu contributorZone = new JMenu ("Contributor Zone"); JCheckBoxMenuItem voteOnline = new JCheckBoxMenuItem("Vote Online"); community.add(contributorZone); contributorZone.add(voteOnline); JMenu yourRatings = new JMenu ("Your Ratings"); JCheckBoxMenuItem good = new JCheckBoxMenuItem("Good"); JCheckBoxMenuItem bad = new JCheckBoxMenuItem("Bad"); JCheckBoxMenuItem excelent = new JCheckBoxMenuItem("Excelent"); community.add(yourRatings); yourRatings.add(good); yourRatings.add(bad); yourRatings.add(excelent); JMenu quizGames = new JMenu ("Quiz Games"); JCheckBoxMenuItem yourGames = new JCheckBoxMenuItem("Your Games"); JCheckBoxMenuItem favouriteGames = new JCheckBoxMenuItem("Favourite Games"); JCheckBoxMenuItem playOnline = new JCheckBoxMenuItem("Play Online"); community.add(quizGames); quizGames.add(yourGames); quizGames.add(favouriteGames); quizGames.add(playOnline); //New menu with two menu items // JMenuItem contactInfo = new JMenuItem("Contact Info"); //JMenuItem addPhotos = new JMenuItem("Add Photos"); // imdbPro.add(contactInfo); // imdbPro.add(addPhotos); //New menu with four menu items JMenuItem appsHome = new JMenuItem("Apps Home"); JMenuItem iPhonePlusIPadApps = new JMenuItem("I Phone + I Pad Apps"); JMenuItem androidApps = new JMenuItem("Android Apps"); JMenuItem kindleFireApp = new JMenuItem("Kindle Fire App"); apps.add(appsHome); apps.add(iPhonePlusIPadApps); apps.add(androidApps); apps.add(kindleFireApp); // This is to display the CheckBoxMenuItem // and another SubMenu with more CheckBoxes in it. JCheckBoxMenuItem actors = new JCheckBoxMenuItem("Actors"); JCheckBoxMenuItem actresses = new JCheckBoxMenuItem("Actresses"); JMenu shows = new JMenu("Shows"); JCheckBoxMenuItem breakingBad = new JCheckBoxMenuItem("Breaking Bad"); JCheckBoxMenuItem theWalkingDead = new JCheckBoxMenuItem("The Walking Dead"); shows.add(shows); shows.add(theWalkingDead); shows.add(breakingBad); news.add(actors); news.add(actresses); news.add(shows); return menuBar; } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame(" ************************************************ INTERNET MOVIE DATA BASE *************************************************** "); //Create and set up the content pane. MenuBarExample ex = new MenuBarExample(); frame.setContentPane(ex.createContentPane()); //Set the MenuBar of the Frame to the MenuBar frame.setJMenuBar(ex.createMenuBar()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } }
Rob Camick
Rancher
Posts: 3324
32
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
See:
How to Write an Action Listener
. Don't forget to look at the examples found under the heading
Examples that Use Action Listeners
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
enabling & disabling menuitems
JCheckboxMenuItem won't change selection color
Background Image not Showing
Scrolling JMenu
JMenuBar in an Applet : Good vs Bad Code Practice?
More...