How to combine Combo Box with tree in Swing?

For my application, I want a Combo Box that displays its items when dropped as a Tree . The problem is that I don't know enough Swing to know how to do this. At least without finishing writing a new widget from scratch or something.

How would I do something like this without building it from scratch?

+1


source to share


4 answers


I think I will implement this as a JTree component in a JViewPort and then an extension button. When it crashes, it will look like a combo box. When you click the expand button, the viewport expands, allowing you to scroll and select a node in the JTree. When you selected a node, the view port would flip back to display only the selected node and extension button.



+4


source


Hey, guess what! This is your lucky day.

I have used this structure in the past. It's very complete. I didn't know they had it already.

JIDE Soft



alt text http://img89.imageshack.us/img89/8324/combotreejj1.png

Not too expensive, but it will take you a while to understand the API (it's not that hard, but they've created a lot)

+2


source


Override the getListCellRendererComponent method and create components in level order. For each level of the tree, move the colored row 3 spaces to the right.

Example:

1

... and

... b

2

... from

Original implementation which you can look at from

public Component getListCellRendererComponent(
                                       JList list,
                                       Object value,
                                       int index,
                                       boolean isSelected,
                                       boolean cellHasFocus) {
        //Get the selected index. (The index param isn't
        //always valid, so just use the value.)
        int selectedIndex = ((Integer)value).intValue();

    if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
    } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }

    //Set the icon and text.  If icon was null, say so.
    ImageIcon icon = images[selectedIndex];
    String pet = petStrings[selectedIndex];
    setIcon(icon);
    if (icon != null) {
        setText(pet);
        setFont(list.getFont());
    } else {
        setUhOhText(pet + " (no image available)",
                    list.getFont());
    }

    return this;
}

      

0


source


You can create a ComboBoxEditor whose component (returned by getEditorComponent) is a JTree

You may have tried this already.

I don't know how it will look. Post screenshot if you get it working. :)

EDIT

I give it a quick dirty try. This is terrible, but this is the beginning.

alt text http://img120.imageshack.us/img120/2563/yiakxk2.png

Here's the code for what it's worth. :(

Perhaps you should start thinking about alternatives. As for the fake Combo, which is a JButton with no border when the hidden panel is clicked, it will appear with the tree displayed.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ComboTree {
    public static void main( String [] args ) { 
        JComboBox c = new JComboBox( new String [] { "Hello", "there"});
        c.setModel( new CustomComboModel() );
        c.setEditor( new TreeComboEditor() );
        c.setRenderer( new TreeComboEditor() );
        JFrame frame = new JFrame();
        frame.add( c , BorderLayout.NORTH ) ;
        frame.pack();
        frame.setVisible( true );

    }
}

class CustomComboModel implements ComboBoxModel {
     public Object  getSelectedItem() { return ":P"; }
     public void    setSelectedItem(Object anItem) {}
     public void    addListDataListener(ListDataListener l) {}
     public Object  getElementAt(int index)  { return "at " + index ; }
     public int getSize()  { return 2; }
     public void    removeListDataListener(ListDataListener l)  {}
}
class TreeComboEditor implements ComboBoxEditor, ListCellRenderer {

     // Editor interface
     public void addActionListener(ActionListener l) {}
     public Component   getEditorComponent() {
         return new JTree() ;
         }
     public Object  getItem() { return "";}
     public void    removeActionListener(ActionListener l) {}
     public void    selectAll() {}
     public void    setItem(Object anObject) {}

     // Render interface
     public Component getListCellRendererComponent(JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
        return new JTree();
    }
}

      

0


source







All Articles