java 下拉框级联及相关(转)

ActionLintsner是所有监听器的父类,其它监听器可以监听的事件都可以被它捕获
ItemListener用于捕获带有item的组件产生的事件,如ComboBox,CheckBox,RadioButton,ToggleButton,接口中定义的itemStateChanged(ItemEvent e)将执行需要在已选定(或已取消选定)项时发生的操作

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class YearMonthDay extends JPanel {

 private int year;
 private int month;
 private int day;
 private final int STARTYEAR = 2000;// 年份的开始值
 private final int ENDYEAR = 2020;// 年份的结束值
 JPanel contentPane;
 // 年月日的选择框
 JComboBox cboYear = new JComboBox();
 JComboBox cboMonth = new JComboBox();
 JComboBox cboDay = new JComboBox();
 // 年月日标签
 JLabel jLabel1 = new JLabel();
 JLabel jLabel2 = new JLabel();
 JLabel jLabel3 = new JLabel();
 public YearMonthDay() {
  jbInit();
 }
 private void jbInit() {
  this.setLayout(null);
  setSize(new Dimension(200, 80));
  // 年的下拉选择框
  cboYear.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
  cboYear.setBounds(new Rectangle(0, 0, 55, 18));
  // cboYear.addItemListener(new DateItemAdapter(this));
  // 月的下拉选择框
  cboMonth.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
  cboMonth.setBounds(new Rectangle(80, 0, 45, 18));
  cboMonth.addItemListener(new DateItemAdapter(this));
  // 日的下拉选择框
  cboDay.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
  cboDay.setBounds(new Rectangle(150, 0, 45, 18));
  // cboDay.setEditable(true);
  // 年的label
  jLabel3.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
  jLabel3.setText("年");
  jLabel3.setBounds(new Rectangle(60, 0, 20, 20));
  // 月的label
  jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
  jLabel2.setText("月");
  jLabel2.setBounds(new Rectangle(130, 0, 20, 20));
  // 日的label
  jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
  jLabel1.setText("日");
  jLabel1.setBounds(new Rectangle(200, 0, 20, 20));
  this.add(cboYear);
  this.add(cboMonth);
  this.add(cboDay);
  this.add(jLabel3);
  this.add(jLabel2);
  this.add(jLabel1);
  // 添加初始值
  AddInfo();
  //设置默认的时间
  setYear(Integer.valueOf(cboYear.getSelectedItem().toString().trim()));
  setMonth(Integer.valueOf(cboMonth.getSelectedItem().toString().trim()));
  setDay(Integer.valueOf(cboDay.getSelectedItem().toString().trim()));

 }

 private void AddInfo() {
  // 年下拉选择框
  for (int i = STARTYEAR; i < ENDYEAR; i++) {
   cboYear.addItem("" + i);
  }
  //年的默认时间是2009年
  cboYear.setSelectedIndex(9);
  // 月下拉选择框
  for (int i = 0; i < 12; i++) {
   cboMonth.addItem("" + (i + 1));
  }
  // 日下拉选择框
  for (int j = 0; j < 31; j++) {
   cboDay.addItem("" + (j + 1));
  }
 }
 private int getYear() {
  return year;
 }
 private void setYear(int year) {
  this.year = year;
 }
 private int getMonth() {
  return month;
 }
 private void setMonth(int month) {
  this.month = month;
 }
 private int getDay() {
  return day;
 }
 private void setDay(int day) {
  this.day = day;
 }
 public String getDate(){
  return year+"-"+month+"-"+day;
 }
 @Override
 public String toString() {
  return this.getDate();
 }
 public void cboMonth_itemStateChanged(ItemEvent e) {
  Object yearObject = cboYear.getSelectedItem();
  Object monthObject = cboMonth.getSelectedItem();
  Object dayObject = cboDay.getSelectedItem();
  if (yearObject != null && monthObject != null && dayObject != null) {
   this.setYear(Integer.valueOf(yearObject.toString().trim()));
   int month = Integer.valueOf(cboMonth.getSelectedItem().toString());// 取得选
中月份
   this.setDay(Integer.valueOf(cboDay.getSelectedItem().toString()
     .trim()));
   cboDay.removeAllItems();// 清空日的下拉列表框
   int days = 31;
   if (month == 4 || month == 6 || month == 9 || month == 11) {
    days = 30;
   } else if (month == 2) {
    // 取得选中年份
    int year = Integer.parseInt(cboYear.getSelectedItem()
      .toString());
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
     // 是闰年
     days = 29;
    } else {
     // 不是闰年
     days = 28;
    }
   }// if
   for (int j = 0; j < days; j++) {
    cboDay.addItem("" + (j + 1));
   }// for
  }// if
 }
}// end class
// 事件监听器
class DateItemAdapter implements ItemListener {
 private YearMonthDay adaptee;
 DateItemAdapter(YearMonthDay adaptee) {
  this.adaptee = adaptee;
 }
 public void itemStateChanged(ItemEvent e) {
  adaptee.cboMonth_itemStateChanged(e);
 }
}

http://blog.sina.com.cn/s/blog_5f20c4740100e47e.html

JComboBox ItemListener监听器

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;

class itemListener implements ItemListener {

public void itemStateChanged(ItemEvent e) {

if (e.getStateChange() == ItemEvent.SELECTED) {
    JComboBox jcb = (JComboBox) e.getSource();

System.out.println((String) (jcb.getSelectedItem()));
    System.out.println(jcb.getSelectedIndex());
   } else {

}

}

}

今天用到了JComboBox的事件监听类 ItemListener,程序在执行时发现事件总是被触发两次,上网看了些资料,原来和ItemListener类中的方法itemStateChanged()事件的itemState有关,itemState在这里的状态有两个,Selected 和 deSelected(即选中和未被选中),所以,当改变下拉列表中被选中的项的时候,其实是触发了两次事件,第一次是上次被选中的项的 State 由 Selected 变为 deSelected ,即取消选择, 第二次是本次被选中的项的 State 由 deSelected 变为 Selected ,即新选中,所以,必然的 ItemStateChanged 事件中的代码要被执行两次了。

解决方法 就是:
  if(e.getStateChange() == ItemEvent.SELECTED){
  //要执行的代码
  }
  即为当事件是选中某一项的时候,才执行该代码。

http://ribbonchen.blog.163.com/blog/static/118316505200988105942563/

(总结)Swing组件的使用---下拉式菜单(menu),弹出式菜单(JPopupMenu),选项卡窗体(JTabbedPane)

http://blog.csdn.net/lyxaiclr/article/details/7391318

时间: 2024-10-31 23:42:07

java 下拉框级联及相关(转)的相关文章

Jquery+Ajax下拉框级联查询

Jquery代码 <script type="text/javascript">          $(function(){ //提交事件           $("#ImageButton1").click(function(){ //省Id        var provinceId=$("#selProvince").val(); //市Id        var cityId=$("#selCity").

java下拉框,滚动条

1 package com.soft.test; 2 3 /** 4 * 下拉列表.下拉框.滚动条的使用 5 */ 6 7 import javax.swing.*; 8 import java.awt.*; 9 public class Dem5 extends JFrame { 10 11 12 private JPanel jp1, jp2; 13 private JLabel jlb1, jlb2; 14 private JComboBox jb1; 15 private JList j

ASP.NET MVC 下拉框级联

这个是效果图 首先分析下,我们需要两个下拉框 首先要动态绑定班级的,这个很好实现,怎么让学生下拉框也出来,并显示请选择学生呢? public ActionResult Index() { ViewData["class"] = getByItem(); ViewData["stu"] = new List<SelectListItem>() { new SelectListItem {Text = "请选则学生", Value = &

[转]html 下拉框级联

<html> <head> <title>$Title$</title> <meta charset="UTF-8"/> <style type="text/css"> * { margin: 0px; padding: 0px; } .change { width: 800px; border: 1px solid red; margin: 50px auto; text-align: cen

MVC+knocKout.js 实现下拉框级联

数据库:部门表和员工表 在控制器里面的操作: 1 public ActionResult Index3() 2 { 3 ViewBag.departments = new SelectList(getDepartments(),"d_id","d_name");//部门数据 4 return View(); 5 } 6 7 private List<department> getDepartments() 8 { 9 List<department

NG循环结束后执行函数:(用于瀑布流,下拉框,及相关需要插座dom插件之类的场景)

先定义指令: app.directive('onFinishRender',function ($timeout) { return { restrict: 'A', link: function (scope, element, attr) { if (scope.$last === true) { $timeout(function () { scope.$emit('ngRepeatFinished'); // scope.isSelectShow = false; }); } } } }

easy ui 下拉框绑定事件

---------------选择改变------------------------------ <input id='ServiceStatus' field-type='combo' type='text' class='easyui-combobox' style='width: 100px' value='' data-options=" valueField: 'id', textField: 'text',url: '../../Ajax/GetListData.ashx?D

Struts2 &lt;s:doubleselect&gt;级联下拉框 详解析

运行环境:myeclipse8.6+jboss5.1+jvm1.6 先看最后目录结构: 直接上源码: complexFormTag.jsp: <%@ page language="java" contentType="text/html; charset=gb2312"pageEncoding="gb2312"%> <%@ taglib prefix="s" uri="/struts-tags&qu

jquery级联下拉框

$(document).ready(function(){     //找到三个下拉框     var carnameSelect = $(".carname").children("select");     var cartypeSelect = $(".cartype").children("select");     var wheeltypeSelect = $(".wheeltype").chi