如何消除TreeItem小部件中选定文本周围的矩形框?

时间:2012-03-01 16:30:16

标签: java tree swt

我想实现一个带有可检查根节点的树和一些参数(实际上,这种情况下的参数无关紧要)。所以,我制作一个树,然后放置2列,然后放置一个TreeItem。然后我需要放置一个带有复选框按钮的TreeEditor 按钮按钮=新按钮(树,SWT.CHECK);

存在一个问题:如果未设置TreeItem中的文本(或等于“”),则在展开树节点元素时,将显示一个小的ractangle并且不会被隐藏。如果文本设置为TreeItem,那么将在TreeItem中的文本周围显示矩形,但如果鼠标从TreeItem移出,它将隐藏。有没有办法不显示这个矩形?

Empty text area http://s15.postimage.org/8u20pkezv/checkbox.png

以下是来源:

Tree tree = new Tree(composite_1, SWT.NONE);
tree.setHeaderVisible(true);
tree.setLinesVisible(true);

TreeColumn trclmnProperty = new TreeColumn(tree, SWT.NONE);
trclmnProperty.setWidth(100);
trclmnProperty.setText(Messages.AppWindow_trclmnProperty_text);

TreeColumn trclmnValue = new TreeColumn(tree, SWT.NONE);
trclmnValue.setWidth(100);
trclmnValue.setText(Messages.AppWindow_trclmnValue_text);

// Reading contents of a tree
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
    builder = factory.newDocumentBuilder();
    File f = new File("ftplist.xml");
    org.w3c.dom.Document doc = builder.parse(f);
    NodeList ftpList = doc.getElementsByTagName("ftp");

    for (int i = 0; i < ftpList.getLength(); i++) {
        NodeList nodeList = ((Element) ftpList.item(i)).getElementsByTagName("ftpName");
        TreeItem treeItemRootNode = new TreeItem(tree, SWT.NONE);

        TreeEditor treeRootNodeEditor = new TreeEditor(tree);

        final Button button = new Button(tree, SWT.CHECK);
        button.setText(nodeList.item(0).getTextContent());
        nodeList = ((Element) ftpList.item(0)).getElementsByTagName("ftpEnabled");
        button.setSelection(nodeList.item(0).getTextContent().equalsIgnoreCase("true"));

        button.pack();

        treeRootNodeEditor.minimumWidth = button.getSize().x;
        treeRootNodeEditor.horizontalAlignment = SWT.LEFT;

        treeRootNodeEditor.setEditor(button, treeItemRootNode, 0);

        nodeList = ((Element) ftpList.item(0)).getElementsByTagName("ftpHost");
        TreeItem treeItemFtpHost = new TreeItem(treeItemRootNode, SWT.NONE);
        treeItemFtpHost.setText(new String[] { "Host", nodeList.item(0).getTextContent() });
        }

    } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

ftplist.xml - 它只是一个配置文件,只包含ftp-settings,如host,port等。

ftplist.xml:

<?xml version="1.0"?>
<ftps>
    <ftp>
        <ftpName>SuperFtp</ftpName>
        <ftpEnabled>true</ftpEnabled>
        <ftpHost>ftp.com</ftpHost>
        <ftpPort>21</ftpPort>
        <ftpLogin>Login</ftpLogin>
        <ftpPassword>Pass</ftpPassword>
        <ftpUsePassiveMode>true</ftpUsePassiveMode>
        <ftpRemoteDir>/</ftpRemoteDir>
        <localSyncDir>C:/</localSyncDir>
    </ftp>
    <ftp>
        <ftpName>SuperFtp2</ftpName>
        <ftpEnabled>true</ftpEnabled>
        <ftpHost>ftp.com</ftpHost>
        <ftpPort>21</ftpPort>
        <ftpLogin>Login</ftpLogin>
        <ftpPassword>Pass</ftpPassword>
        <ftpUsePassiveMode>true</ftpUsePassiveMode>
        <ftpRemoteDir>/</ftpRemoteDir>
        <localSyncDir>C:/</localSyncDir>
    </ftp>
<ftps>

1 个答案:

答案 0 :(得分:2)

以下代码段会阻止您的Tree绘制其选择内容:

tree.addListener(SWT.EraseItem, new Listener()
{
  public void handleEvent(Event event)
  {
    if ((event.detail & SWT.SELECTED) != 0)
    {
      event.detail &= ~SWT.SELECTED;
    }
  }
});