|
|
Help with JList and JComboBox
Calling swing gurus,
I have a requirement to implement a sublist to a JList. What I've come
up with is implement ListCellRenderer and extend JPanel, then add a
JCombobox when a certain item on the list is clicked. I can get the
list cell to display ok, but I can't seem to make the combobox drop or
become selectable. The code is as below. Any help is very much
appreciated:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RenderList
extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JList listbox;
// Constructor of main frame
public RenderList()
{
// Set the frame characteristics
setTitle( "Rendered ListBox Application" );
setSize( 300, 160 );
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create some items to add to the list
String listData[] =
{
"One",
"Two",
"Three",
"Four"
};
listbox = new JList( listData );
listbox.setCellRenderer( new CustomCellRenderer() );
listbox.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = listbox.locationToIndex(e.getPoint());
Rectangle rect = listbox.getCellBounds(index, index);
listbox.repaint(rect);
}
});
topPanel.add( listbox, BorderLayout.CENTER );
}
public static void main( String args[] )
{
// Create an instance of the test application
RenderList mainFrame = new RenderList();
mainFrame.setVisible( true );
}
}
===========================
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class CustomCellRenderer
extends JPanel
implements ListCellRenderer, ActionListener
{
private JLabel label;
private JTable table;
private JComboBox combo;
private String select = "Select: ";
public CustomCellRenderer()
{
setOpaque(true);
label = new JLabel();
add(label);
combo = new JComboBox();
combo.setVisible(false);
add(combo);
combo.addActionListener(this);
setLayout ( new BoxLayout(this, BoxLayout.LINE_AXIS) );
}
public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus )
{
String val = value.toString();
label.setText(val);
if((index == 2) && cellHasFocus)
{
combo.addItem("A");
combo.addItem("B");
combo.addItem("C");
label.setText(val + " " + select);
combo.setVisible(true);
}
else
{
combo.setVisible(false);
}
if(isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
/* (non-Javadoc)
* @see
java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String select = (String) combo.getSelectedItem();
}
}
|
|
0
|
|
|
|
Reply
|
dzuy (1)
|
2/8/2005 4:37:53 PM |
|
Your code has been truncated.
Please explain what functionality you want this "sublist" to have.
--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "
|
|
0
|
|
|
|
Reply
|
Fahd
|
2/8/2005 6:11:35 PM
|
|
Hmm... It only misses the 2 closing braces.
I have a JList. When clicking on a certain item on the list, it
presents a dropdown (combobox) to further select. I got the gui to
display ok, only problem is that clicking on the combobox does nothing.
|
|
0
|
|
|
|
Reply
|
dzuy
|
2/8/2005 7:30:46 PM
|
|
Thats because when you click on the drop down box it actually fires off
the list's mouse listener. You could try transferring focus to the
combo box but I doubt that will work. The best solution would be to
reengineer the whole widget and put the combo box its own container,
rather than having it within the jlist. Maybe have a jwindow...
something like code completion in IDEs.
--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "
|
|
0
|
|
|
|
Reply
|
Fahd
|
2/11/2005 10:53:28 AM
|
|
Fahd Shariff wrote:
> Thats because when you click on the drop down box it actually fires
off
> the list's mouse listener. You could try transferring focus to the
> combo box but I doubt that will work. The best solution would be to
> reengineer the whole widget and put the combo box its own container,
> rather than having it within the jlist. Maybe have a jwindow...
> something like code completion in IDEs.
>
> --
> Fahd Shariff
> http://www.fahdshariff.cjb.net
> "Let the code do the talking... "
I tried all focus and listener methods to no avail. The problem is
that JList only implements custom renderer, not editor for cell on the
list. I've seen examples where people have implement checkbox
successfully by intercepting the mouse click and translate that into
checking/unchecking the check box. That won't work for combobox
(probably can with mouse motion detection). If only JList provides a
cell editor like table does...
|
|
0
|
|
|
|
Reply
|
dzuy
|
2/11/2005 2:25:13 PM
|
|
|
4 Replies
460 Views
(page loaded in 0.118 seconds)
|
|
|
|
|
|
|
|
|