473,811 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help GUI select and keystroke

nomad
664 Recognized Expert Contributor
I need help with JcomboBox
When and User select the Exit in the File box it will the
//inner class
class exitListener implements ActionListener {
...
}
which it open a window asking them if they want to close the program.

I have a statement
filemenu.setMne monic(KeyEvent. VK_X);
But the setMnemonic(Key Event .VK_X) is wrong.
Also I want to be able to drag down and select Exit.
Can someone please help me on this.



Here is my code...

Expand|Select|Wrap|Line Numbers
  1. class MyFrame extends JFrame {
  2.     String[] file = { "New", "Open", "Exit" };//items for file
  3.     String[] edit = { "Cut", "Copy", "Paste" };//items for edit
  4.     JComboBox filemenu = new JComboBox();
  5.     JComboBox editmenu = new JComboBox();
  6.  
  7.  
  8.     public MyFrame(String title) {
  9.         super(title);
  10.         this.setSize(250, 250); //sets the size for the frame
  11.         this.setLocation(200, 200);//location where frame is at
  12.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.         // setup contents
  14.         makeComponents();
  15.         initComponents();
  16.         buildGUI();
  17.  
  18.         // display
  19.         this.setVisible(true);
  20.     }
  21.  
  22.     private void makeComponents() {
  23.  
  24.         JPanel pane = new JPanel();
  25. //        file menu section
  26.         filemenu = new JComboBox();
  27.         JLabel fileLabel = new JLabel();
  28.         pane.add(fileLabel);
  29.         for (int i = 0; i < file.length; i++)
  30.             filemenu.addItem(file[i]);
  31.         pane.add(filemenu);
  32.         add(pane);
  33.         setVisible(true);
  34.  
  35. //edit menu section
  36.         editmenu = new JComboBox();
  37.         JLabel editLabel = new JLabel();
  38.         pane.add(editLabel);
  39.         for (int i = 0; i < edit.length; i++)
  40.             editmenu.addItem(edit[i]);
  41.         pane.add(editmenu);
  42.         add(pane);
  43.         setVisible(true);
  44.     }
  45.  
  46.     private void initComponents() {
  47.  
  48.         filemenu.addActionListener(new exitListener());
  49.     }
  50.  
  51.  
  52.     //inner class
  53.     class exitListener implements ActionListener {
  54.         public void actionPerformed(ActionEvent arg0) {
  55.  
  56.                 int x = JOptionPane.showOptionDialog(MyFrame.this, "Exit Program?",
  57.                                 "Exit Request", JOptionPane.YES_NO_OPTION,
  58.                                 JOptionPane.QUESTION_MESSAGE, null, null,
  59.                                 JOptionPane.NO_OPTION);
  60.                 if (x == JOptionPane.YES_OPTION) {
  61.                         MyFrame.this.dispose();
  62.                 }
  63.  
  64.         }
  65. }
  66.  
  67.  
  68.  
  69.  
  70.     private void buildGUI() {
  71.         Container cont = this.getContentPane();// set gui components into the frame
  72.         this.setLayout(new FlowLayout(FlowLayout.LEFT));// Comp are added to the frame
  73.         cont.add(filemenu);
  74.         cont.add(editmenu);
  75.     }
  76.  
  77.     // / inner classes
  78.  
  79. }
  80.  
  81.  
  82.  
  83. public class ButtonFrame {
  84.  
  85.     public static void main(String[] args) {
  86.  
  87.         MyFrame f1 = new MyFrame("This is my Project for GUI");
  88.     }
  89.  
  90. }


Thanks
nomad
May 3 '07 #1
1 1608
JosAH
11,448 Recognized Expert MVP
Your filemenu isn't a menu at all, it's a JCombobox. Have a look at the JMenu
and [b/JMenuItem[/b] classes; they make up the parts of your drop down menus.
Those classes implement the mnemonics methods and a bit more.

kind regards,

Jos
May 3 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2719
by: Ste | last post by:
Hello, my goal would be implementing some keystroke analysis using php. I'm a novice in php, so i ask your advice: according to you, is it possible to log each keystroke client-side even if php is a server-side language, or shall i use JavaScript and make the client run the program locally? And, if i can use php to do that, how can i implement a feature logging keystrokes on the client system? Thanks,
7
6225
by: hokieghal99 | last post by:
Does anyone know of a keystroke logger that has been written in Python for Windows machines? I'd like to see such a script and use it as a point of reference for a real-time backup project that I'm working on. Basically, I'd like to be able to capture all keystrokes from the keyboard buffer and write them to a text file so I could reporduce emails, documents, etc. in the event of file loss that occurs between nightly backups. For example,...
2
3248
by: Wayne Wengert | last post by:
I want to get the decimal (integer) value of user keystrokes. I am using the following code. chrInput is the character entered (e.g. "M") and "keystroke is DIMed as Short keystroke = Microsoft.VisualBasic.Val(chrInput) but it always yields a value of 0 How should I be doing this?
8
7724
by: Lucy | last post by:
help! javascript inline of an HTML to open an HTML in a target window. Something like... <html> <head> <title>test</title> </head> <body>
15
5773
by: Frank Bormann | last post by:
Hi, probably a stupid question, but I haven't been able to find anything. Is there a istream related function that let me read exactly one keystroke from the keyboard through cin? What I need it to do is this: - remove all characters currently in the input buffer - block until the user presses a key
12
4040
by: BC | last post by:
I have read the FAQ and the discussions but I am not a Javascript programmer and do not know how to make toFixed (and other techniques) work, after several attempts. The following calculations work when the form is submitted (and maybe they should not...), but I do not know how to add a Rounding function to the existing code, so that each Result is Rounded to 2 decimal places. toFixed sounds like it would be ideal...
10
2820
by: Martin Holm Pedersen | last post by:
Hey All.. Im having a bit of a problem with my program that i wrote for linux in c. I use select() to monitor if the user has pressed a key and reads the key with read(). It works fine om my IBM laptop but once i move the program to my dell laptop it seems like it doesn't even recognize the select-function. That is, it doesn't use the timeout assigned at all. I don't get any errors when i compile on either computer. I run debian/testing on...
1
5294
by: Joseph Geretz | last post by:
I'm noticing that when my DataGrid has focus, it handles the Enter key by advancing to the next row in the grid. This neutralizes Forms's current defined AcceptButton. My primary question: is there any quick way to configure the Grid not to handle the Enter key? I do not see any property control which might configure this behavior. My secondary question: If there's no quick solution via Grid configuration, then how do I get the...
4
2701
by: Cron | last post by:
Hi can someone give me a hand with this please? I'm trying to build a search filter that scans through a list of client names in a database as you type into a text box and filters the form records accordingly. The problem is my database structure is making it difficult: I need the filter to first lookup the partially inputted name in the clients table and then filter my invoices according to the clientID from that table. Here's what I have...
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10652
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10395
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10408
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9211
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.