JTreeComboBox--Swing树形下拉框

最近公司用SWing开发时,需要实现网友们称为的“树形下拉框的”效果。关于此效果B/S模式很容易实现,放到Swing上面事先就有点困难。这个东西弄了将近一下午,终于出效果了。先分享出去。希望共同提高!
package com.pos.utils.tree;

import java.awt.*;

/**
* @author wanghaoqian
* Description: 树形下拉列表框
*/
public class JTreeComboBox extends JComboBox {
/**
* 显示用的树
*/
private JTree tree;

public JTreeComboBox() {
this(new JTree());
}

public JTreeComboBox(JTree tree) {
this.setTree(tree);
}

/**
* 设置树
*
* @param tree
* JTree
*/
public void setTree(JTree tree) {
this.tree = tree;
if (tree != null) {
this.setSelectedItem(tree.getSelectionPath());
this.setRenderer(new JTreeComboBoxRenderer());
}
this.updateUI();
}

/**
* 取得树
*
* @return JTree
*/
public JTree getTree() {
return tree;
}

/**
* 设置当前选择的树路径
*
* @param o
* Object
*/
public void setSelectedItem(Object o) {
tree.setSelectionPath((TreePath) o);
getModel().setSelectedItem(o);
}

public void updateUI() {
ComboBoxUI cui = (ComboBoxUI) UIManager.getUI(this);
if (cui instanceof MetalComboBoxUI) {
cui = new MetalJTreeComboBoxUI();
} else if (cui instanceof MotifComboBoxUI) {
cui = new MotifJTreeComboBoxUI();
} else {
cui = new WindowsJTreeComboBoxUI();
}
setUI(cui);
}

// UI Inner classes -- one for each supported Look and Feel
class MetalJTreeComboBoxUI extends MetalComboBoxUI {
protected ComboPopup createPopup() {
return new TreePopup(comboBox);
}
}

class WindowsJTreeComboBoxUI extends WindowsComboBoxUI {
protected ComboPopup createPopup() {
return new TreePopup(comboBox);
}
}

class MotifJTreeComboBoxUI extends MotifComboBoxUI {
protected ComboPopup createPopup() {
return new TreePopup(comboBox);
}
}

/**
*
* Description: 树形结构而来的DefaultListCellRenderer
*/
class JTreeComboBoxRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value != null) {
TreePath path = (TreePath) value;
TreeNode node = (TreeNode) path.getLastPathComponent();
value = node;
TreeCellRenderer r = tree.getCellRenderer();
JLabel lb = (JLabel) r.getTreeCellRendererComponent(tree,
value, isSelected, false, node.isLeaf(), index,
cellHasFocus);
return lb;
}
return super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
}
}

/**
* 测试
*/
public static void main(String args[]) {
JFrame frame = new JFrame("JTreeComboBox 例子");
frame.setSize(400, 320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.getContentPane().setLayout(new FlowLayout());
final JTreeComboBox box = new JTreeComboBox(new JTree());
box.setPreferredSize(new Dimension(300, 21));
frame.getContentPane().add(box);
final JButton btt = new JButton("保存");
btt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
box.setTree(new JTree());
btt.setEnabled(false);
}
});
frame.getContentPane().add(btt);
frame.setVisible(true);
}
}

/**
* <p>
* Description: TreePopup
* </p>
*/
class TreePopup extends JPopupMenu implements ComboPopup {
protected JTreeComboBox comboBox;
protected JScrollPane scrollPane;

protected MouseMotionListener mouseMotionListener;
protected MouseListener mouseListener;
private MouseListener treeSelectListener = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
JTree tree = (JTree) e.getSource();
TreePath tp = tree.getPathForLocation(e.getPoint().x,
e.getPoint().y);
if (tp == null) {
return;
}
comboBox.setSelectedItem(tp);
togglePopup();
MenuSelectionManager.defaultManager().clearSelectedPath();
}
};

public TreePopup(JComboBox comboBox) {
this.comboBox = (JTreeComboBox) comboBox;
setBorder(BorderFactory.createLineBorder(Color.black));
setLayout(new BorderLayout());
setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled());
JTree tree = this.comboBox.getTree();
if (tree != null) {
scrollPane = new JScrollPane(tree);
scrollPane.setBorder(null);
add(scrollPane, BorderLayout.CENTER);
tree.addMouseListener(treeSelectListener);
}
}

public void show() {
updatePopup();
show(comboBox, 0, comboBox.getHeight());
comboBox.getTree().requestFocus();
}

public void hide() {
setVisible(false);
comboBox.firePropertyChange("popupVisible", true, false);
}

protected JList list = new JList();

public JList getList() {
return list;
}

public MouseMotionListener getMouseMotionListener() {
if (mouseMotionListener == null) {
mouseMotionListener = new MouseMotionAdapter() {
};
}
return mouseMotionListener;
}

public KeyListener getKeyListener() {
return null;
}

public void uninstallingUI() {
}

/**
* Implementation of ComboPopup.getMouseListener().
*
* @return a <code>MouseListener</code> or null
* @see ComboPopup#getMouseListener
*/
public MouseListener getMouseListener() {
if (mouseListener == null) {
mouseListener = new InvocationMouseHandler();
}
return mouseListener;
}

protected void togglePopup() {
if (isVisible()) {
hide();
} else {
show();
}
}

protected void updatePopup() {
setPreferredSize(new Dimension(comboBox.getSize().width, 200));
Object selectedObj = comboBox.getSelectedItem();
if (selectedObj != null) {
TreePath tp = (TreePath) selectedObj;
((JTreeComboBox) comboBox).getTree().setSelectionPath(tp);
}
}

protected class InvocationMouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if (!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) {
return;
}
if (comboBox.isEditable()) {
Component comp = comboBox.getEditor().getEditorComponent();
if ((!(comp instanceof JComponent))
|| ((JComponent) comp).isRequestFocusEnabled()) {
comp.requestFocus();
}
} else if (comboBox.isRequestFocusEnabled()) {
comboBox.requestFocus();
}
togglePopup();
}
}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值