473,507 Members | 2,379 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,gender;

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.setSelectedIndex(29);
yearcombo.addActionListener(actionEvent());
String[] month = { "January", "Febuary", "March", "April", "May", "June", "July", "October", "November", "December"};
JComboBox monthcombo = new JComboBox(month);
monthcombo.setSelectedIndex(9);
monthcombo.addActionListener(this);
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"};
JComboBox statecombo = new JComboBox(state);
statecombo.setSelectedIndex(49);
statecombo.addActionListener(this);
String[] gender = {"M","F"};
JComboBox gendercombo = new JComboBox(gender);
gendercombo.setSelectedIndex(1);
gendercombo.addActionListener(this);
f1 = new JFrame("fafafffdas");
f1.setSize(832,669);
Container c1 = f1.getContentPane();


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

first = new JTextField("First Name");
last = new JTextField("Last Name");
age = new JTextField("Age");
street = new JTextField("Street Address");
city = new JTextField("City");
clear = new JButton("Clear");
clear.addActionListener(new ButtonListener());
submit = new JButton("Submit");
submit.addActionListener(new ButtonListener());
Bok = new JButton("Save");
Bok.addActionListener(new ButtonListener());
Bcc = new JButton("Quit");
Bcc.addActionListener(new ButtonListener());

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

g1 = new GP();
p1.add(g1,BorderLayout.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,BorderLayout.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.getSource();
String year = (String)cb.getSelectedItem();

}





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


if (event.getSource() == clear)
{


}

if (event.getSource() == Bok)
{
g1.requestFocus();

}

if (event.getSource() == Bcc)
{

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


}

g1.repaint();
}
}


}
Sep 23 '07 #1
4 1532
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.addActionListener(actionEvent());) 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
3497
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...
0
1584
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...
6
3667
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
2900
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...
2
3259
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...
0
7223
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,...
0
7111
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...
0
7319
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,...
0
7376
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...
0
7485
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...
0
5623
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,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
760
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.