1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import java.awt.BorderLayout;
import javax.swing.JButton;
class Test extends JFrame {
// Instance attributes used in this example
private JPanel topPanel;
private JTree tree;
private JPanel panelButton = null;
private JButton enable = null;
private JButton refresh;
private JCompCheckBox jcompCheckBox1 = new JCompCheckBox();
private JCompCheckBox jcompCheckBox2 = new JCompCheckBox();
private JCompCheckBox jcompCheckBox3 = new JCompCheckBox();
private JCompCheckBox jcompCheckBox4 = new JCompCheckBox();
// Constructor of main frame
public Test()
{
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
// Set the frame characteristics
setTitle( "My Tree Application" );
setSize( 300, 200);
//setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
setContentPane(topPanel);
//Create Jtree
DefaultMutableTreeNode racine = new DefaultMutableTreeNode("Racine");
DefaultMutableTreeNode rep1 = new DefaultMutableTreeNode(jcompCheckBox1);
DefaultMutableTreeNode rep2 = new DefaultMutableTreeNode(jcompCheckBox2);
DefaultMutableTreeNode rep3 = new DefaultMutableTreeNode(jcompCheckBox3);
DefaultMutableTreeNode rep4 = new DefaultMutableTreeNode(jcompCheckBox4);
racine.add(rep1);
racine.add(rep2);
racine.add(rep3);
racine.add(rep4);
DefaultTreeModel treeModel = new DefaultTreeModel(racine);
tree = new JTree(treeModel);
tree.setShowsRootHandles( true);
tree.setEditable(true);
((DefaultTreeModel)tree.getModel()).nodeChanged(rep3);
// Add a cell editor to the tree
tree.setCellEditor(new EditCellPanel() );
//Add a cell renderer to the tree
tree.setCellRenderer(new CellPanelRender());
//Add Elements to panel
topPanel.add(tree, BorderLayout.NORTH);
topPanel.add(getPanelButton(), BorderLayout.SOUTH);
}
//Panel containing enable/disable and refresh JButtons
private JPanel getPanelButton() {
if (panelButton == null) {
panelButton = new JPanel();
panelButton.setLayout( new BorderLayout() );
panelButton.add(getEnable(), BorderLayout.NORTH);
panelButton.add(getRefresh(), BorderLayout.SOUTH);
}
return panelButton;
}
//Enable/Disable JButton
private JButton getEnable() {
if (enable == null) {
enable = new JButton();
enable.setText("Disable");
//Add Action listener that disable and enable CompCheckBox
enable.addActionListener ( new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
if (enable.getText().equals("Disable")){
jcompCheckBox1.getM_checkBox().setEnabled(false);
jcompCheckBox2.getM_checkBox().setEnabled(false);
jcompCheckBox3.getM_checkBox().setEnabled(false);
jcompCheckBox4.getM_checkBox().setEnabled(false);
enable.setText("Enable");
}else{
jcompCheckBox1.getM_checkBox().setEnabled(true);
jcompCheckBox2.getM_checkBox().setEnabled(true);
jcompCheckBox3.getM_checkBox().setEnabled(true);
jcompCheckBox4.getM_checkBox().setEnabled(true);
enable.setText("Disable");
}
}
});
}
return enable;
}
//Refresh JButton
private JButton getRefresh() {
if (refresh == null) {
refresh = new JButton();
refresh.setText("Refresh");
//Add a listener to refresh the JTree
refresh.addActionListener ( new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
tree.updateUI();
}
});
}
return refresh;
}
// Main entry point for this example
public static void main( String args[] )
{
// Create an instance of the test application
Test mainFrame = new Test();
mainFrame.setVisible( true );
}
}
//The Cell Renderer for the JTree
//allow to put a JCompCheckBox instead of a Label
class CellPanelRender implements TreeCellRenderer{
public Component getTreeCellRendererComponent(JTree tree, Object obj, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
DefaultMutableTreeNode dmtcr = (DefaultMutableTreeNode)obj;
if(dmtcr.getUserObject() instanceof JCompCheckBox){
JCompCheckBox compCheckBox = (JCompCheckBox)dmtcr.getUserObject();
return compCheckBox;
} else {
JLabel compCheckBox = new JLabel((String)dmtcr.getUserObject());
return compCheckBox;
}
}
}
//the Cell Eitor for the JTree
//allox to edit node of a JTree
class EditCellPanel implements TreeCellEditor{
public void addCellEditorListener(CellEditorListener l){
}
public void cancelCellEditing() {
}
public Object getCellEditorValue(){
return this;
}
public boolean isCellEditable(EventObject evt){
if(evt instanceof MouseEvent){
MouseEvent mevt = (MouseEvent) evt;
if (mevt.getClickCount() == 1){
return true;
}
}
return false;
}
public void removeCellEditorListener(CellEditorListener l){
}
public boolean shouldSelectCell(EventObject anEvent){
return true;
}
public boolean stopCellEditing(){
return false;
}
public Component getTreeCellEditorComponent(JTree tree, Object obj, boolean isSelected, boolean expanded, boolean leaf, int row){
DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)obj;
JCompCheckBox compCheckBox =(JCompCheckBox)dmtn.getUserObject();
compCheckBox.setEnabled(true);
return compCheckBox;
}
}
//class to create a new JCompCheckBox that contains 3 Label and a checkBox
class JCompCheckBox extends JPanel
{
private JLabel m_buildStatus;
private JLabel m_GDAStatus;
private JLabel m_PDAStatus;
private JCheckBox m_checkBox;
/**
* This is the constructor for compCheckBox
*/
public JCompCheckBox()
{
setOpaque( false );
setAlignmentX(0);
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
m_buildStatus = new JLabel("X");
add(m_buildStatus);
m_GDAStatus = new JLabel("X");
add(m_GDAStatus);
m_PDAStatus = new JLabel("X");
add(m_PDAStatus);
m_checkBox = new JCheckBox("CheckBox");
getM_checkBox().setOpaque( false );
add(m_checkBox);
}
public void addItemListener ( ItemListener listener )
{
getM_checkBox().addItemListener ( listener );
}
public void addActionListener ( ActionListener listener )
{
getM_checkBox().addActionListener ( listener );
}
/**
* Returns the checkBox
*/
JCheckBox getM_checkBox()
{
return m_checkBox;
}
/**
* Returns the m_buildStatus
*/
JLabel getM_buildStatus()
{
return m_buildStatus;
}
/**
* Returns the m_GDAStatus
*/
JLabel getM_GDAStatus()
{
return m_GDAStatus;
}
/**
* Returns the m_PDAStatus
*/
JLabel getM_PDAStatus()
{
return m_PDAStatus;
}
} |
Partager