checkbox ploblem pls help!!

  • Thread starter yew y via JavaKB.com
  • Start date
Y

yew y via JavaKB.com

i was new in java and i now completed add the checkbox in each node but the
ploblem is i cant click the check box..
what should i do in next step... pls help me thank you..
following is my code

fileTree.setCellRenderer(new DefaultTreeCellRenderer()
{
public Component getTreeCellRendererComponent(JTree tree, Object
value,
boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
row, hasFocus);
JCheckBox chk = new JCheckBox();
chk.setContentAreaFilled(false);
chk.setEnabled(true);
//
chk.setBorderPaintedFlat(true);
chk.setOpaque(false);

JPanel jPanel = new JPanel(new BorderLayout());
jPanel.add(chk, BorderLayout.WEST);
jPanel.add(this, BorderLayout.EAST);
return jPanel;
}
 
C

Christian Kaufhold

yew y via JavaKB.com said:
i was new in java and i now completed add the checkbox in each node but the
ploblem is i cant click the check box..
what should i do in next step... pls help me thank you..

* Read the documentation and tutorial.
* Also write a TreeCellEditor.


Christian
 
C

Christian Kaufhold

yew y via JavaKB.com said:
how can i write the tree cell renderer?

Please quote what you are referring to.
Please look up the meaning of "specific".
Please post the code you've written, describing what it should do and
what it does etc.



Christian
 
C

Christian Kaufhold

yew y via JavaKB.com said:
can u giv me ur e-mail so i can send the code for you..

Please quote the text you are referring to.

I see no reason to switch te discussion to mail.



Christian
 
Y

yew y via JavaKB.com

tis is my renderer n editor..
y my checkbox still can't click?

public class TestEditor1 implements TreeCellEditor
{

public TestEditor1()
{ }


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 value,
boolean isSelected,
boolean expanded,
boolean leaf,
int row)
{
DefaultMutableTreeNode temp = (DefaultMutableTreeNode) value;
JCheckBox temp2=(JCheckBox)temp.getUserObject();
return temp2;


}
}



public class CkBoxRenderer extends DefaultTreeCellRenderer
{
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row,
hasFocus);

JCheckBox chk = new JCheckBox();

chk.setContentAreaFilled(true);
chk.setEnabled(true);
chk.setBackground(UIManager.getColor("Tree.textBackground"));
JPanel jPanel = new JPanel(new BorderLayout());
jPanel.add(chk, BorderLayout.WEST);
jPanel.add(this, BorderLayout.EAST);
return jPanel;
}


}
 
C

Christian Kaufhold

yew y via JavaKB.com said:
tis is my renderer n editor..
y my checkbox still can't click?

a b c u i s?

public class TestEditor1 implements TreeCellEditor

Why are you not subclassing AbstractCellEditor? Your implementation for
most methods are wrong.

public Object getCellEditorValue()
{
return this;
}

The value of the cell editor is the value to be stored in the model. It
hardly makes sense that this is the cell editor itself.
public Component getTreeCellEditorComponent(JTree tree,
Object value,
boolean isSelected,
boolean expanded,
boolean leaf,
int row)
{
DefaultMutableTreeNode temp = (DefaultMutableTreeNode) value;
JCheckBox temp2=(JCheckBox)temp.getUserObject();
return temp2;

Neither does it make much sense to use JCheckBox as value. JCheckBox
can be used to present to the value, but the value itself should be
a proper abstraction.
public class CkBoxRenderer extends DefaultTreeCellRenderer
{
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row,
hasFocus);

JCheckBox chk = new JCheckBox();

chk.setContentAreaFilled(true);
chk.setEnabled(true);
chk.setBackground(UIManager.getColor("Tree.textBackground"));
JPanel jPanel = new JPanel(new BorderLayout());
jPanel.add(chk, BorderLayout.WEST);
jPanel.add(this, BorderLayout.EAST);
return jPanel;

This does not make much sense either. Why are you creating a new checkbox
instead of using the "value"?


Christian
 
Y

yew y via JavaKB.com

so wat should i do to make my checkbox can click?
can you teach me??
tis is very urgent!!!
 
Y

yew y via JavaKB.com

i really no idea to do .......
i really not familiar with swing.....
hope you can help me..
thank in advanced...
 
C

Christian Kaufhold

yew y via JavaKB.com said:
i really no idea to do .......
i really not familiar with swing.....
hope you can help me..
thank in advanced...

Try to understand the following code. Also try to figure out its
limitations.



import javax.swing.AbstractCellEditor;

import javax.swing.JCheckBox;

import javax.swing.JTree;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;


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

import java.awt.Component;
import java.awt.Graphics;

import java.awt.Color;

import javax.swing.UIManager;


class CheckBoxEditor
extends AbstractCellEditor
implements TreeCellEditor
{
private JCheckBox box;



public CheckBoxEditor()
{
box = new JCheckBox();


box.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stopCellEditing();
}
});
}



public Component getTreeCellEditorComponent
(JTree t, Object value, boolean selected, boolean expanded, boolean leaf, int row)
{
box.setForeground(t.getForeground());
box.setBackground(t.getBackground());

box.setComponentOrientation(t.getComponentOrientation());
box.setEnabled(t.isEnabled());

// these two lines
box.setText(value.toString().substring(1));
box.setSelected(value.toString().charAt(0) == 'x');

return box;
}


public Object getCellEditorValue()
{
// this line
return (box.isSelected() ? "x" : "/") + box.getText();
}
}


class CheckBoxRenderer
extends JCheckBox
implements TreeCellRenderer
{
private Color selectionForeground, selectionBackground;
private boolean painting, focused;

public CheckBoxRenderer()
{
setHorizontalAlignment(LEADING);
setContentAreaFilled(true);

selectionForeground = UIManager.getColor("Tree.selectionForeground");
selectionBackground = UIManager.getColor("Tree.selectionBackground");
}


public Component getTreeCellRendererComponent
(JTree t, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean focused)
{
if (false && selected)
{
setForeground(selectionForeground);
setBackground(selectionBackground);
}
else
{
setForeground(t.getForeground());
setBackground(t.getBackground());
}

this.focused = focused;

setComponentOrientation(t.getComponentOrientation());
setEnabled(t.isEnabled());

// these two lines
setText(value.toString().substring(1));
setSelected(value.toString().charAt(0) == 'x');

return this;
}


protected void paintComponent(Graphics g)
{
{
painting = true;
}
try
{
super.paintComponent(g);
}
finally
{
painting = false;
}
}

public boolean hasFocus()
{
return painting ? focused : super.hasFocus();
}
}



class CheckBoxTest
{

public static void main(String[] args)
{
JTree t = new JTree();

t.setCellRenderer(new CheckBoxRenderer());
t.setCellEditor(new CheckBoxEditor());
t.setEditable(true);

javax.swing.JFrame f = new javax.swing.JFrame();

f.getContentPane().add(new javax.swing.JScrollPane(t));

f.setSize(500, 400);
f.setVisible(true);
}
}
 
Y

yew y via JavaKB.com

firstly i want to say thank you... my checkbox clickable..
but still hav ploblem is my checkbox can click but my 1st charactar of my
data already gone lah.... wat is the ploblem.... how can i set the
clickable value to perform action.....
 
C

Christian Kaufhold

yew y via JavaKB.com said:
firstly i want to say thank you... my checkbox clickable..
but still hav ploblem is my checkbox can click but my 1st charactar of my
data already gone lah.... wat is the ploblem.... how can i set the
clickable value to perform action.....

Please quote some context.

1. That is the way the data is stored. In this simple example,
the first character encodes whether the data is checked.

2. ActionListener?


Christian
 
Y

yew y via JavaKB.com

this is my full program tree.... is use for show the full path of the
directory.... i need to add chkbox beside to perform action
getSelectionMode() or getCellEditorValue() or action listener... i still
confuse....
from ur example , my checkbox already can click but the 1st character is
gone... can you solve tis ploblem?

/**
DirTree (former FileTree1)
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;

public class DirTree
extends JPanel {

public static final String APP_NAME = "Directories Tree";

public static final ImageIcon ICON_COMPUTER =
new ImageIcon("computer.gif");
public static final ImageIcon ICON_DISK =
new ImageIcon("disk.gif");
public static final ImageIcon ICON_FOLDER =
new ImageIcon("folder.gif");
public static final ImageIcon ICON_EXPANDEDFOLDER =
new ImageIcon("expandedfolder.gif");

protected JTree m_tree;
protected DefaultTreeModel m_model;
protected JTextField m_display;

public DirTree() {

DefaultMutableTreeNode top = new DefaultMutableTreeNode(
new IconData(ICON_COMPUTER, null, "My Computer"));

DefaultMutableTreeNode node;
File[] roots = File.listRoots();
for (int k=0; k<roots.length; k++) {
node = new DefaultMutableTreeNode(new IconData(ICON_DISK,
null, new FileNode(roots[k])));
top.add(node);
node.add( new DefaultMutableTreeNode(new Boolean(true)));
}

m_model = new DefaultTreeModel(top);
m_tree = new JTree(m_model);

m_tree.putClientProperty("JTree.lineStyle", "Angled");


m_tree.addTreeExpansionListener(new
DirExpansionListener());

m_tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
m_tree.setShowsRootHandles(true);
m_tree.setEditable(true);


JScrollPane splitPane = new JScrollPane(

new JScrollPane(m_tree)
);

setLayout( new BorderLayout() );

add( splitPane );
}

DefaultMutableTreeNode getTreeNode(TreePath path) {
return (DefaultMutableTreeNode)(path.getLastPathComponent());
}

FileNode getFileNode(DefaultMutableTreeNode node) {
if (node == null)
return null;
Object obj = node.getUserObject();
if (obj instanceof IconData)
obj = ((IconData)obj).getObject();
if (obj instanceof FileNode)
return (FileNode)obj;
else
return null;
}

// Make sure expansion is threaded and updating the tree model
// only occurs within the event dispatching thread.
class DirExpansionListener implements TreeExpansionListener {

public void treeExpanded(TreeExpansionEvent event) {

final DefaultMutableTreeNode node = getTreeNode(
event.getPath());
final FileNode fnode = getFileNode(node);

Thread runner = new Thread() {
public void run() {
if (fnode != null && fnode.expand(node)) {
Runnable runnable = new Runnable() {
public void run() {
m_model.reload(node);
}
};
SwingUtilities.invokeLater(runnable);
}
}
};
runner.start();
}

public void treeCollapsed(TreeExpansionEvent event) {}
}




public static void main(String argv[]) {

JFrame frame= new JFrame( "FileSystem Viewer");
DirTree dir = new DirTree();
frame.getContentPane().add(dir);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);



}
}

class IconData {
protected Icon m_icon;
protected Icon m_expandedIcon;
protected Object m_data;

public IconData(Icon icon, Object data) {
m_icon = icon;
m_expandedIcon = null;
m_data = data;
}

public IconData(Icon icon, Icon expandedIcon, Object data) {
m_icon = icon;
m_expandedIcon = expandedIcon;
m_data = data;
}

public Icon getIcon() {
return m_icon;
}

public Icon getExpandedIcon() {
return m_expandedIcon!=null ? m_expandedIcon : m_icon;
}

public Object getObject() {
return m_data;
}

public String toString() {
return m_data.toString();
}
}



class FileNode {
protected File m_file;

public FileNode(File file) {
m_file = file;
}

public File getFile() {
return m_file;
}

public String toString() {
return m_file.getName().length() > 0 ? m_file.getName() :
m_file.getPath();
}

// Alternatively we copud sub-class TreeNode
public boolean expand(DefaultMutableTreeNode parent) {
DefaultMutableTreeNode flag =
(DefaultMutableTreeNode)parent.getFirstChild();
if (flag==null) // No flag
return false;
Object obj = flag.getUserObject();
if (!(obj instanceof Boolean))
return false; // Already expanded

parent.removeAllChildren(); // Remove Flag

File[] files = listFiles();
if (files == null)
return true;

Vector v = new Vector();

for (int k=0; k<files.length; k++) {
File f = files[k];
if (!(f.isDirectory()))
continue;

FileNode newNode = new FileNode(f);

boolean isAdded = false;
for (int i=0; i<v.size(); i++) {
FileNode nd = (FileNode)v.elementAt(i);
if (newNode.compareTo(nd) < 0) {
v.insertElementAt(newNode, i);
isAdded = true;
break;
}
}
if (!isAdded)
v.addElement(newNode);
}

for (int i=0; i<v.size(); i++) {
FileNode nd = (FileNode)v.elementAt(i);
IconData idata = new IconData(DirTree.ICON_FOLDER,
DirTree.ICON_EXPANDEDFOLDER, nd);
DefaultMutableTreeNode node = new
DefaultMutableTreeNode(idata);
parent.add(node);

if (nd.hasSubDirs())
node.add(new DefaultMutableTreeNode(
new Boolean(true) ));
}

return true;
}

public boolean hasSubDirs() {
File[] files = listFiles();
if (files == null)
return false;
for (int k=0; k<files.length; k++) {
if (files[k].isDirectory())
return true;
}
return false;
}

public int compareTo(FileNode toCompare) {
return m_file.getName().compareToIgnoreCase(
toCompare.m_file.getName() );
}

protected File[] listFiles() {
if (!m_file.isDirectory())
return null;
try {
return m_file.listFiles();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null,
"Error reading directory "+m_file.getAbsolutePath(),
DirTree.APP_NAME, JOptionPane.WARNING_MESSAGE);
return null;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ploblem in jcheckbox 0
change icon in jtree 0
TreeCellRenderer 2
[SWING] Problem with JTree 2
Jtree renderers 0
JTable within JTree 1
TreeCellRenderer does not work? 2
Wrapped components not visible with FlowLayout 2

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top