📄 swingutils.java
字号:
/* ===========================================================
* JDBMonitor : a flexiable JDBC Monitor for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2006-2006, by yang zhongke
*
* Project Info: http://www.cownew.com
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* SwingUtils.java
* ---------------
* (C) Copyright 2006-2006, by yang zhongke
*
* Original Author: yang zhongke;
*
* Changes
* -------
*
*/
package com.cownew.ctk.ui.swing;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;
import org.apache.commons.lang.enums.EnumUtils;
import com.cownew.ctk.common.ExceptionUtils;
import com.cownew.ctk.common.StringEnum;
public class SwingUtils
{
/**
* make Container frame in the center of the owner
*
* @param owner
* @param frame
*/
public static void centerInParent(Container owner, Container frame)
{
Rectangle parentRect = owner.getBounds();
Point pos = new Point();
pos.setLocation(parentRect.x + parentRect.width / 4, parentRect.y
+ parentRect.height / 4);
frame.setLocation(pos);
}
public static void centerAtScreen(Window window)
{
if (window != null)
{
Dimension scrDim = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowDim = window.getSize();
window.setLocation((scrDim.width - windowDim.width) / 2,
(scrDim.height - windowDim.height) / 2);
}
}
/**
* init the JFrame size
*
* @param frame
*/
public static void initWindowSize(JFrame frame)
{
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds((int) dim.getWidth() / 4, (int) dim.getHeight() / 4,
(int) dim.getWidth() / 2, (int) dim.getHeight() / 2);
}
/**
* int the submenu of the LookAndFeel Menu(menuItemSkin)
*
* @param owner
* @param menuItemSkin
*/
public static void initLookAndFeelMenu(final Component owner,
JMenu menuItemSkin)
{
LookAndFeelInfo[] skins = UIManager.getInstalledLookAndFeels();
ButtonGroup btnGroupSkins = new ButtonGroup();
for (int i = 0, n = skins.length; i < n; i++)
{
final LookAndFeelInfo skinInfo = skins[i];
JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(skinInfo
.getName());
if (i == 0)
{
menuItem.setSelected(true);
}
btnGroupSkins.add(menuItem);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try
{
UIManager.setLookAndFeel(skinInfo.getClassName());
SwingUtilities.updateComponentTreeUI(owner);
} catch (ClassNotFoundException e1)
{
throw ExceptionUtils.toRuntimeException(e1);
} catch (InstantiationException e1)
{
throw ExceptionUtils.toRuntimeException(e1);
} catch (IllegalAccessException e1)
{
throw ExceptionUtils.toRuntimeException(e1);
} catch (UnsupportedLookAndFeelException e1)
{
throw ExceptionUtils.toRuntimeException(e1);
}
}
});
menuItemSkin.add(menuItem);
}
}
/**
* return all the elements in JList as Vector
*
* @param list
* @return
*/
public static Vector getItemVector(JList list)
{
ListModel model = list.getModel();
Vector vector = new Vector(model.getSize());
for (int i = 0, n = model.getSize(); i < n; i++)
{
vector.add(model.getElementAt(i));
}
return vector;
}
public static void setChildEnabled(Container parent, boolean editable)
{
for (int i = 0, n = parent.getComponentCount(); i < n; i++)
{
Component comp = parent.getComponent(i);
if(comp instanceof Container)
{
setChildEnabled((Container) comp,editable);
}
comp.setEnabled(editable);
}
}
/**
* 得到当前激活的窗口
* @return
*/
public static Window getActiveWindow()
{
return KeyboardFocusManager.getCurrentKeyboardFocusManager()
.getActiveWindow();
}
/**
* 选择JComboBox中等于(==或者equals)obj的项
* @param box
* @param obj
*/
public static void setSelectedItem(JComboBox box,Object obj)
{
if(obj==null)
{
box.setSelectedIndex(-1);
return;
}
for(int i=0,n=box.getItemCount();i<n;i++)
{
Object item = box.getItemAt(i);
if(obj==item||obj.equals(item))
{
box.setSelectedIndex(i);
return;
}
}
}
/**
* 用枚举类型填充combobox,填充之前删除原有数据
* @param box
* @param enumClass 枚举类型名,必须是StringEnum的子类
*/
public static void fillWithEnum(JComboBox box, Class enumClass)
{
fillWithEnum(box,enumClass,true);
}
/**
* 用枚举类型填充combobox
* @param box
* @param removeOthers 是否填充之前删除原有数据
* @param enumClass 枚举类型名,必须是StringEnum的子类
*/
public static void fillWithEnum(JComboBox box, Class enumClass,
boolean removeOthers)
{
if (removeOthers)
{
box.removeAllItems();
}
if (!StringEnum.class.isAssignableFrom(enumClass))
{
throw new IllegalArgumentException(
"enum Class must inherited from Stringenum!");
}
List list = EnumUtils.getEnumList(enumClass);
for (int i = 0, n = list.size(); i < n; i++)
{
box.addItem(list.get(i));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -