java Swing 编写需要用的一些常用组件,求代码~~
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Swing 编写需要用的一些常用组件,求代码~~相关的知识,希望对你有一定的参考价值。
自定义jtextfield,jcombobox,jprogressbar,jbutton,jdialog,jtreemodel等。
这个看你设计成什么样子,都有不同的需求,代码只能参考一下。package com.hengjun.hjropcs.commons;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.UIManager;
import com.hengjun.commons.lang.Func1;
import com.hengjun.commons.lang.HJCollectionUtils;
import com.hengjun.commons.swing.DimensionUtility;
import com.hengjun.commons.swing.HJComboBoxUtils;
import com.hengjun.commons.swing.combobox.decorators.SpellComboBoxDecorator;
import com.hengjun.commons.ui.DisplayData;
import com.hengjun.framework.enums.EDataStatus;
import com.hengjun.framework.framework_core.model.BaseModel;
import com.hengjun.framework.framework_core.support.constant.MessageConstants;
import com.hengjun.hjrop.model.dbmodel.RopRegion;
import com.hengjun.hjrop.model.dbmodel.RopStation;
import com.hengjun.hjropcs.cache.CacheRegion;
import com.hengjun.hjropcs.commons.hjcombobox.Builder;
import com.hengjun.hjropcs.commons.hjcombobox.RegionComboBox.CBuilder;
import com.hengjun.hjropcs.core.ui.FullSizeComboBoxUI;
public class HJCommonCombox extends JComboBox
//批量修改用,=1 带拼音搜索,批量修改时第一项插入一个new 对象,而不是CommonObject中的对象;
//因为点击+-号时可以直接更改index=0项的text即可,效率高
public final static int MULTI_MODIFY_WITH_SPELL=1;
//批量修改用,=2 不带拼音搜索
public final static int MULTI_MODIFY_WITHOUT_SPELL=2;
//active数据,不带有第一行的empty
public final static int ACTIVE_RECORDS_WITH_SPELL_WITHOUT_EMPTY=3;
//默认的下拉框长度
// public static final int COMBOBOX_MAX_WIDTH = 170;
private boolean isSupportSpell=false;
//产生一个新的数据model,第一条数据为new DisplayData(null,"","")
protected List produceNewModelData(List activeViewForMulti)
if(activeViewForMulti==null || activeViewForMulti.size()==0)
return activeViewForMulti;
List ret=new ArrayList(activeViewForMulti);
ret.set(0, new DisplayData(null,"",""));
return ret;
public boolean isSupportSpell()
return isSupportSpell;
public void setSRTDimension()
this.setMaximumSize(DimensionUtility.SRTDimension);
this.setMinimumSize(DimensionUtility.SRTDimension);
public void setSRTDimension(Dimension dimension)
this.setMaximumSize(dimension);
this.setMinimumSize(dimension);
public void setSupportSpell(boolean isSupportSpell)
this.isSupportSpell = isSupportSpell;
/**
* 默认创建的是含有所有active数据的下拉框
*/
public HJCommonCombox()
this.setMaximumSize(DimensionUtility.componentDimension);
this.setMinimumSize(DimensionUtility.componentDimension);
public HJCommonCombox(Builder builder)
//默认是allActive数据
List model=getActiveModelData();
//如果是批量修改用,基于active数据
if(builder.isMultiModify())
model=getActiveMultiModelData();
model= produceNewModelData(model);
//如果需要所有数据
if(builder.isAll())
model=getAllModelData();//getActiveModelData;
//如果不需要第一项empty值
if(!builder.isWithEmpty())
HJComboBoxUtils.showList(this, model,1);
if(builder.isWithEmpty())
HJComboBoxUtils.showList(this, model);
//如果需要拼音搜索
if(builder.isSupportSpell())
SpellComboBoxDecorator scb = new SpellComboBoxDecorator(this);
scb.initialize();
this.setMaximumSize(DimensionUtility.componentDimension);
this.setMinimumSize(DimensionUtility.componentDimension);
//根据filter名称和builder,返回List
public List<DisplayData> getListData(String filter,Builder builder)
List model=getFilterActiveModelData(filter);
//如果是批量修改用,基于active数据
if(builder.isMultiModify())
model=getFilterActiveMultiModelData(filter);
model= produceNewModelData(model);
//如果需要所有数据
if(builder.isAll())
model=getFilterAllModelData(filter);//getActiveModelData;
//如果不需要第一项empty值
if(!builder.isWithEmpty())
if (!HJCollectionUtils.isEmpty(model))
model=model.subList(1,model.size());
else//需要则直接返回
// HJComboBoxUtils.showList(this, model);
return model;
public HJCommonCombox(String filter,Builder builder)
//默认是allActive数据
List model=getFilterActiveModelData(filter);
//如果是批量修改用,基于active数据
if(builder.isMultiModify())
model=getFilterActiveMultiModelData(filter);
model= produceNewModelData(model);
//如果需要所有数据
if(builder.isAll())
model=getFilterAllModelData(filter);//getActiveModelData;
//如果不需要第一项empty值
if(!builder.isWithEmpty())
HJComboBoxUtils.showList(this, model,1);
else
HJComboBoxUtils.showList(this, model);
//如果需要拼音搜索
if(builder.isSupportSpell())
SpellComboBoxDecorator scb = new SpellComboBoxDecorator(this);
scb.initialize();
this.setMaximumSize(DimensionUtility.componentDimension);
this.setMinimumSize(DimensionUtility.componentDimension);
/**
* 当内容比较长的时候,做得处理
*/
public void updateUI()
setUI(new FullSizeComboBoxUI());
/**
* @param isSupportSpell is true;表示支持拼音搜索
*/
public HJCommonCombox(boolean isSupportSpell)
this();
if(isSupportSpell)
this.isSupportSpell=isSupportSpell;
SpellComboBoxDecorator scb = new SpellComboBoxDecorator(this);
scb.initialize();
/**@deprecated 请使用 HJCommonCombox(Builder builder)模式
* @param isSupportSpell is true;表示支持拼音搜索
*/
public HJCommonCombox(boolean isSupportSpell,boolean isAll)
this(isSupportSpell);
public void setSelectedItem(Object o)
ComboBoxModel cm=getModel();
HJComboBoxUtils.setSelectedItem(o, cm);
/**
* 全部置空,在批量修改中使用
*/
public static void allSetEmpty(HJCommonCombox cmbBox)
if(cmbBox.getModel().getSize()>0)
cmbBox.setSelectedIndex(0);
cmbBox.setEnabled(false);
DisplayData dd=(DisplayData) cmbBox.getItemAt(0);
dd.setText(MessageConstants.ALL_SET_EMPTY);
JTextField editor = (JTextField)cmbBox.getEditor().getEditorComponent();
editor.setText(MessageConstants.ALL_SET_EMPTY);
public void setSelectedIndex(int anIndex)
if(getModel().getSize()>0)
super.setSelectedIndex(anIndex);
/**
* 全部置空复位
* @param cmbBox
*/
public static void allSetEmptyReset(HJCommonCombox cmbBox)
if(cmbBox.getModel().getSize()>0)
cmbBox.setSelectedIndex(0);
cmbBox.setEnabled(true);
DisplayData dd=(DisplayData) cmbBox.getItemAt(0);
dd.setText("");
// cmbBox.rep
JTextField editor = (JTextField)cmbBox.getEditor().getEditorComponent();
editor.setText("");
/**
*
* @param collection
* @param converter
* @param allRecods 返回所有的对象
* @param allActiveRecords 返回所有状态为active的对象
* @return 不需要返回值,修改传入的2个list即可
*/
public static <T, TResult> void select(Collection<T> collection, Func1<T, TResult> converter,List allRecods,List allActiveRecords)
BaseModel temp=null;
for (T t : collection)
temp=(BaseModel)t;
if(temp!=null)
if(EDataStatus.VALID.getKey()==temp.getDataStatus())
allActiveRecords.add(converter.func(t));
allRecods.add(converter.func(t));
public static <T, TResult> void select(Collection<T> collection, Func1<T, TResult> converter,List inputRecods)
BaseModel temp=null;
for (T t : collection)
temp=(BaseModel)t;
if(temp!=null)
inputRecods.add(converter.func(t));
public static <T, TResult> void select(Collection<T> collection, Func1<T, TResult> converter,List allRecods,List allActiveRecords,List allActiveRecordsWithMulti)
BaseModel temp=null;
Object a=null;
for (T t : collection)
temp=(BaseModel)t;
if(temp!=null)
a=converter.func(t);
if(EDataStatus.VALID.getKey()==temp.getDataStatus())
allActiveRecords.add(a);
allActiveRecordsWithMulti.add(a);
allRecods.add(a);
protected List<DisplayData> getActiveModelData()
return null;
protected List<DisplayData> getAllModelData()
return null;
protected List<DisplayData> getActiveMultiModelData()
return null;
protected List<DisplayData> getFilterActiveModelData(String filter)
return null;
protected List<DisplayData> getFilterAllModelData(String filter)
return null;
protected List<DisplayData> getFilterActiveMultiModelData(String filter)
return null;
参考技术A package zhidao;
import javax.accessibility.Accessible;
import javax.swing.Action;
import javax.swing.DefaultButtonModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.ButtonUI;
public class MyButton extends JButton implements Accessible
private static final long serialVersionUID = 1L;
private static final String uiClassID = "MyButtonUI";
public MyButton ()
this (null, null);
public MyButton ( Icon icon )
this (null, icon);
public MyButton ( String text )
this (text, null);
/**
* Creates a button where properties are taken from the <code>Action</code>
* supplied.
*
* @param a
* the <code>Action</code> used to specify the new button
* @since 1.3
*/
public MyButton ( Action a )
this ();
setAction (a);
public MyButton ( String text, Icon icon )
setModel (new DefaultButtonModel ());
init (text, icon);
public void updateUI ()
setUI ((ButtonUI) UIManager.getUI (this));
public String getUIClassID ()
return uiClassID;
public boolean isDefaultButton ()
JRootPane root = SwingUtilities.getRootPane (this);
if (root != null)
return root.getDefaultButton () == this;
return false;
public void removeNotify ()
JRootPane root = SwingUtilities.getRootPane (this);
if (root != null && root.getDefaultButton () == this)
root.setDefaultButton (null);
super.removeNotify ();
参考技术B Java GUI编程似乎不是重点 参考技术C 自定义组建?什么需要还要自定义?
以上是关于java Swing 编写需要用的一些常用组件,求代码~~的主要内容,如果未能解决你的问题,请参考以下文章
Java学习笔记7.3.1 用户界面 - Swing常用组件