473,666 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

combo boxes and actionlisteners

53 New Member
this is pretty basic I'm sure for most of you, but im having alot of trouble with the 2. Here is my code its BlueJ. Im sure this is simple for most of you, but im totally lost. Please try to explain to me what you did. here the code below. i know its messy but watever lol. Thanks for your help!


import java.awt.*;
import java.awt.event. *;
import javax.swing.*;

public class Main
{
JFrame f1;
JPanel p1,p2;

GP g1;
JButton Bok,Bcc;
JTextField first,last,age, street,city,gen der;

public Main()
{


String[] year = { "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" , "2009", "2010"};

JComboBox yearcombo = new JComboBox(year) ;
yearcombo.setSe lectedIndex(29) ;
yearcombo.addAc tionListener(ac tionEvent());
String[] month = { "January", "Febuary", "March", "April", "May", "June", "July", "October", "November", "December"} ;
JComboBox monthcombo = new JComboBox(month );
monthcombo.setS electedIndex(9) ;
monthcombo.addA ctionListener(t his);
String[] state = { "Alabama AL", "Alaska AK" ,"Arizona AZ", "Arkansas AR", "California CA" ,"Colorado CO", "Connecticu t CT" ,"Delaware DE", "Florida FL","Georgia GA", "Hawaii HI"," Idaho ID","Illinois IL" ,"Indiana IN", "Iowa IA", "Kansas KS" ,"Kentucky KY" ,"Louisiana LA", "Maine ME", "Maryland MD" ,"Massachuse tts MA", "Michigan MI" ,"Minnesota MN" ,"Mississipp i MS" ,"Missouri MO", "Montana MT" ,"Nebraska NE", "Nevada NV" ,"New Hampshire NH" ,"New Jersey NJ", "New Mexico NM", "New York NY", "North Carolina NC", "North Dakota ND" ,"Ohio OH" ,"Oklahoma OK", "Oregon OR" ,"Pennsylvan ia PA" ,"Rhode Island RI", "South Carolina SC", "South Dakota SD" ,"Tennessee TN", "Texas TX" ,"Utah UT", "Vermont VT" ,"Virginia VA" ,"Washington WA", "West Virginia WV", "Wisconsin WI" ,"Wyoming WY"};
JComboBox statecombo = new JComboBox(state );
statecombo.setS electedIndex(49 );
statecombo.addA ctionListener(t his);
String[] gender = {"M","F"};
JComboBox gendercombo = new JComboBox(gende r);
gendercombo.set SelectedIndex(1 );
gendercombo.add ActionListener( this);
f1 = new JFrame("fafafff das");
f1.setSize(832, 669);
Container c1 = f1.getContentPa ne();


p1 = new JPanel();
p1.setSize(1000 ,730);
p1.setLayout(ne w BorderLayout()) ;

first = new JTextField("Fir st Name");
last = new JTextField("Las t Name");
age = new JTextField("Age ");
street = new JTextField("Str eet Address");
city = new JTextField("Cit y");
clear = new JButton("Clear" );
clear.addAction Listener(new ButtonListener( ));
submit = new JButton("Submit ");
submit.addActio nListener(new ButtonListener( ));
Bok = new JButton("Save") ;
Bok.addActionLi stener(new ButtonListener( ));
Bcc = new JButton("Quit") ;
Bcc.addActionLi stener(new ButtonListener( ));

p2 = new JPanel(); //panel to add JLabel and TextField

g1 = new GP();
p1.add(g1,Borde rLayout.CENTER) ;


g1.add(first);
g1.add(last);
g1.add(age);
g1.add(year);
g1.add(gender);
g1.add(street);
g1.add(city);
g1.add(state);
p2.add(submit);
p2.add(Bok);
p2.add(clear);
p2.add(Bcc);

p1.add(p2,Borde rLayout.SOUTH); //adds p2 to south (Bottom) Graphics Panel will be added to Center
c1.add(p1);

f1.show();

}





public void actionPerformed (ActionEvent e)
{
JComboBox yearcombo = (JComboBox)e.ge tSource();
String year = (String)cb.getS electedItem();

}





private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


if (event.getSourc e() == clear)
{


}

if (event.getSourc e() == Bok)
{
g1.requestFocus ();

}

if (event.getSourc e() == Bcc)
{

f1.dispose();
}
if (event.getSourc e() == submit)
{


}

g1.repaint();
}
}


}
Sep 23 '07 #1
4 1536
JosAH
11,448 Recognized Expert MVP
this is pretty basic I'm sure for most of you, but im having alot of trouble with the 2. Here is my code its BlueJ. Im sure this is simple for most of you, but im totally lost. Please try to explain to me what you did. here the code below. i know its messy but watever lol. Thanks for your help!
Please do ask a question that's on target: what doesn't work? What doesn't it do
what you were expecting it to do? What does your code do that you don't want
it to do? Please don't just dump your code here hoping that some kind soul
will repair it without knowing what you want it to do. Programming and design
of code doesn't work like that and this forum doesn't work like that.

kind regards,

Jos
Sep 23 '07 #2
carlos123
53 New Member
i just want the combo boxes to compile and just add a basic actionlistener to them
Sep 24 '07 #3
r035198x
13,262 MVP
i just want the combo boxes to compile and just add a basic actionlistener to them
So the code does not compile?
What error message do you get then.

P.S Do not double post.
Sep 24 '07 #4
Nepomuk
3,112 Recognized Expert Specialist
QUOTE from carlos123
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Main
  6. {
  7.         JFrame f1;
  8.         JPanel p1,p2;
  9.  
  10.         GP g1;
  11.         JButton Bok,Bcc;
  12.         JTextField first,last,age,street,city,gender;
  13.  
  14.     public Main()
  15.     {  
  16.  
  17.  
  18.         String[] year = { "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" , "2009", "2010"};
  19.  
  20.         JComboBox yearcombo = new JComboBox(year);
  21.         yearcombo.setSelectedIndex(29);
  22.         yearcombo.addActionListener(actionEvent());
  23.         String[] month = { "January", "Febuary", "March", "April", "May", "June", "July", "October", "November", "December"};
  24.         JComboBox monthcombo = new JComboBox(month);
  25.         monthcombo.setSelectedIndex(9);
  26.         monthcombo.addActionListener(this);
  27.         String[] state = { "Alabama AL", "Alaska AK" ,"Arizona AZ", "Arkansas AR", "California CA" ,"Colorado CO", "Connecticut CT" ,"Delaware DE", "Florida FL","Georgia GA", "Hawaii HI"," Idaho ID","Illinois IL" ,"Indiana IN", "Iowa IA", "Kansas KS" ,"Kentucky KY" ,"Louisiana LA", "Maine ME", "Maryland MD" ,"Massachusetts MA", "Michigan MI" ,"Minnesota MN" ,"Mississippi MS" ,"Missouri MO", "Montana MT" ,"Nebraska NE", "Nevada NV" ,"New Hampshire NH" ,"New Jersey NJ", "New Mexico NM", "New York NY", "North Carolina NC", "North Dakota ND" ,"Ohio OH" ,"Oklahoma OK", "Oregon OR" ,"Pennsylvania PA" ,"Rhode Island RI", "South Carolina SC", "South Dakota SD" ,"Tennessee TN", "Texas TX" ,"Utah UT", "Vermont VT" ,"Virginia VA" ,"Washington WA", "West Virginia WV", "Wisconsin WI" ,"Wyoming WY"};
  28.         JComboBox statecombo = new JComboBox(state);
  29.         statecombo.setSelectedIndex(49);
  30.         statecombo.addActionListener(this); 
  31.         String[] gender = {"M","F"};
  32.         JComboBox gendercombo = new JComboBox(gender);
  33.         gendercombo.setSelectedIndex(1);
  34.         gendercombo.addActionListener(this);
  35.         f1 = new JFrame("fafafffdas");
  36.           f1.setSize(832,669);        
  37.         Container c1 = f1.getContentPane();
  38.  
  39.  
  40.         p1 = new JPanel();
  41.         p1.setSize(1000,730);
  42.         p1.setLayout(new BorderLayout());
  43.  
  44.         first = new JTextField("First Name");
  45.         last = new JTextField("Last Name");
  46.         age = new JTextField("Age");
  47.         street = new JTextField("Street Address");
  48.         city = new JTextField("City");
  49.         clear = new JButton("Clear");
  50.         clear.addActionListener(new ButtonListener());
  51.         submit = new JButton("Submit");
  52.         submit.addActionListener(new ButtonListener());
  53.         Bok = new JButton("Save");
  54.          Bok.addActionListener(new ButtonListener());
  55.         Bcc = new JButton("Quit");
  56.          Bcc.addActionListener(new ButtonListener());
  57.  
  58.         p2 = new JPanel();  //panel to add JLabel and TextField
  59.  
  60.         g1 = new GP();    
  61.          p1.add(g1,BorderLayout.CENTER);
  62.  
  63.  
  64.         g1.add(first);
  65.         g1.add(last);
  66.         g1.add(age);
  67.         g1.add(year);
  68.         g1.add(gender);
  69.         g1.add(street);
  70.         g1.add(city);
  71.         g1.add(state);
  72.         p2.add(submit);
  73.         p2.add(Bok);
  74.         p2.add(clear);
  75.         p2.add(Bcc);
  76.  
  77.         p1.add(p2,BorderLayout.SOUTH);      //adds p2 to south (Bottom) Graphics Panel will be added to Center    
  78.         c1.add(p1);
  79.  
  80.         f1.show();
  81.  
  82.     }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.     public void actionPerformed(ActionEvent e) 
  89.     {
  90.         JComboBox yearcombo = (JComboBox)e.getSource();
  91.         String year = (String)cb.getSelectedItem();
  92.  
  93.     }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.     private class ButtonListener implements ActionListener
  100.     {
  101.       public void actionPerformed (ActionEvent event)
  102.       {
  103.  
  104.  
  105.         if (event.getSource() == clear)
  106.         {
  107.  
  108.  
  109.         }    
  110.  
  111.         if (event.getSource() == Bok)
  112.            {
  113.                g1.requestFocus();
  114.  
  115.            }
  116.  
  117.         if (event.getSource() == Bcc)
  118.            {
  119.  
  120.                f1.dispose();
  121.            }
  122.         if (event.getSource() == submit)
  123.         {
  124.  
  125.  
  126.         }
  127.  
  128.           g1.repaint();  
  129.         }
  130.     }
  131.  
  132.  
  133. }
End of Quote

Please use CODE tags when posting. Now, what is line 22 (the line yearcombo.addAc tionListener(ac tionEvent());) supposed to do? Shouldn't it look a bit more like this:
Expand|Select|Wrap|Line Numbers
  1. yearcombo.addActionListener(new ActionListener()
  2. {
  3.     public void actionPerformed(ActionEvent e)
  4.     {
  5.         // your code here
  6.     }
  7. });
  8.  
Greetings,
Nepomuk
Sep 24 '07 #5

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

Similar topics

0
3522
by: Krisa | last post by:
Hello all, I just discovered something (stop me if you've heard this before....) that was causing me a significant performance hit when opening a form with subforms. To speed up loading the form, I set the rowsources of its, and its subforms', combo boxes in the "Enter" events of the combo boxes. That's the standard trick for not loading the rowsource unless/until the user actually needs the combo box. For example:
0
1596
by: Tom | last post by:
I have some very strange issues with combo boxes on a tab control. Here's the scenario: I have a Windows Forms form that has a tab control on it, with two (2) tabs. Tab 2 happens to have a number of text and combo boxes on it (in panels on the tab). These combos were originally simple drop down lists - i.e. you had to select from the list and couldn't enter anything of your own. At that time everything worked fine. Now, the users...
6
3674
by: Dave | last post by:
I want to put the information that the user selects in my combo boxes into a subform that lies on the same form as the combo boxes. Thanks for your help already, Dave
1
2905
by: Dave | last post by:
Hello all, First I'd like to apologize...This post was meant to be put in my previous post, but I tried many times without success to reply within my previous post. Now here goes... I have a main form (RD Form) with 4 combo boxes (i.e. cbo1, cbo2, etc) and a subdatasheet (the subform...let's call it subInfo) below the combo boxes on the RD Form. I hope this eliminates any confusion of the
2
3267
by: Dave | last post by:
I have 3 tables of information feeding into 4 combo boxes on my main form (DR Form). I have as many list boxes (acting as text boxes) as there are fields in each one of the 3 tables. Once selecting from the combo box, I have all the combo boxes, using afterupdate, populating their respective list boxes. These text boxes are directly correlated to the combo box selection using SQL. Here is an example from the afterupdate event in the...
0
8440
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
8352
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
8863
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...
1
8549
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
7378
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...
1
6189
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2005
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.