タイトル
 メニューにないコーナーはTopからいけます
TOPJavaSwingJTree → This Page

JTree@Swing サンプル04

概要

Java - Swing - JTree のサンプルです。
・子ノードの追加
・子ノードの挿入
・子ノードの削除
・子ノードの全削除
・選択された子ノードの削除

解説

各種ボタンを押すと子ノードの追加、子ノードの挿入、子ノードの削除、
子ノードの全削除、選択された子ノードの削除を実行します。

サンプルイメージ

サンプル画像


サンプルソース

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 * JTree サンプル04
 * ・子ノードの追加
 * ・子ノードの挿入
 * ・子ノードの削除
 * ・子ノードの全削除
 * ・選択された子ノードの削除
 * 
 * @author みっちー
 */
public class JTree04 extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	/** ツリー */
	JTree tree = null;
	
	/** ルート */
	DefaultMutableTreeNode rootNode = null;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JTree04 frame = new JTree04();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JTree サンプル04");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 700, 200);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JTree04() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// ルートを作成
		rootNode = new DefaultMutableTreeNode("root");
		
		// ツリーを作成
		tree = new JTree(rootNode);
		tree.setPreferredSize(new Dimension(150, 100));
		tree.setShowsRootHandles(true);
		
		// ボタンを作成
		JButton button1 = new JButton("Add child");
		JButton button2 = new JButton("Insert child");
		JButton button3 = new JButton("Remove child");
		JButton button4 = new JButton("Remove all children");
		JButton button5 = new JButton("Remove selected child");
		
		// アクションコマンドを設定
		button1.setActionCommand("Add");
		button2.setActionCommand("Insert");
		button3.setActionCommand("Remove");
		button4.setActionCommand("RemoveAll");
		button5.setActionCommand("RemoveSel");
		
		// アクションリスナー追加
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		button5.addActionListener(this);
		
		// ツリー・ボタンを追加
		panelBase.add(tree);
		panelBase.add(button1);
		panelBase.add(button2);
		panelBase.add(button3);
		panelBase.add(button4);
		panelBase.add(button5);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
	
	/**
	 * アクション
	 */
	public void actionPerformed(ActionEvent e) {
		DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
		if ("Add".equals(e.getActionCommand())) {
			// 子ノードの追加
			DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("addChild");
			rootNode.add(childNode);
			model.reload();
		} else if ("Insert".equals(e.getActionCommand())) {
			// 子ノードの挿入
			DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("insChild");
			rootNode.insert(childNode, 0);
			model.reload();
		} else if ("Remove".equals(e.getActionCommand())) {
			// 子ノードの削除
			rootNode.remove(0);
			model.reload();
		} else if ("RemoveAll".equals(e.getActionCommand())) {
			// 子ノードの全削除
			rootNode.removeAllChildren();
			model.reload();
		} else if ("RemoveSel".equals(e.getActionCommand())) {
			// 選択された子ノードの削除
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
			node.removeFromParent();
			model.reload();
		}
	}
}

サンプルソースのダウンロード

ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)

更新履歴

2016/05/13 新規作成


TOPJavaSwingJTree → This Page
Valid CSS!