473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Help GUI select and keystroke

nomad
664 Expert 512MB
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.setMnemonic(KeyEvent.VK_X);
But the setMnemonic(KeyEvent .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 1585
JosAH
11,448 Expert 8TB
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
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...
7
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...
2
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 =...
8
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
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...
12
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...
10
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...
1
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...
4
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.