Editable components in JList cell?

I want to create a JList of objects, where the list entries contain a JCheckBox that corresponds to a boolean property.
It's easy to add the checkbox by writing a new ListCellRenderer. BUT, the checkbox can't be edited-- mouse-clicks only select/deselect the item.
Can someone tell me if and how the checkbox can be made editable? Or should I give up and use a JTable instead?

You can also use a JTable rather than a JList, just configure it so the line-cell-separators will not be displayed, and also do not use the table header. JTable already take care of editors. Here is a ref where you can found some source code.
http://www2.gol.com/users/tame/swing/examples/JTableExamples3.html

Similar Messages

  • How to include a panel in a jlist cell

    i have used the jlistcellrenderer and returning the panel there in the getcellrendererComponent mathod .. its working ... but when i am adding one label to that panel , then it should come like ..... in one cell, one panel, and in that one label ... but i am finding many labels in one jlist cell ... it shows like that ..
    can anyone help ????
    if anyone have a sample example then plz send it ...

    You could subclass JTable and override getCellRenderer
    -Puce

  • Only scroll to JList cell if required

    Hi all.
    I have a JList in a JScrollPane. I would like the the selected cell to be visible but the method list.ensureIndexIsVisible(index); doesn't do quite what I want.
    The behaviour I would like is that if the cell is already visible don't scroll at all. If the cell isn't visible, scroll so that it is the last cell selected.
    The code below demonstrates the problem. With index set to 3, I get the behaviour I want, however, if I set the index to 10, I can't see the selected index.
    If I enable the ensureIndexIsVisible method, the selected cell is always shown at the top of the list, or as far towards the top as possible.
    thanks..
    import javax.swing.*;
    public class JListTest extends JFrame {
        public JListTest() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            JList list = new JList(new String[] {"BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla", "BlaBlaBla"});
            JScrollPane scrollPane = new JScrollPane(list);
            add(scrollPane);
            int index = 3;
            list.setSelectedIndex(index);
    //        list.ensureIndexIsVisible(index);
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new JListTest();
    }

    I actually thought this was the default behaviour.
    If you change your code to:
            int index = 3;
            list.setSelectedIndex(index);
    //        list.ensureIndexIsVisible(index);
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
            list.ensureIndexIsVisible(index);then I believe it works the way you want. So I guess the problem is that the components haven't yet been realized when you invoke the method call which is causing the scrolling for some (unknown) reason.
    It also works if you wrap the method call in a SwingUtilities.invokeLater().

  • Modifying Components within a cell table not working

    Hello,
    I am displaying JTextFields within JPanels in each cell of a JTable, but when I select a cell I am unable to enter text into the text boxes. Why has this been disabled now that these components are withing a JTable cell?
    Thanks,
    Emily

    Do I have the CellEditor just return the same Panel the renderer returns?It depends and I suppose rather not.. The JTable invokes editor to provide editing component with cell object as parameter. The editor method is then expected, in your case, to set appropiate values to all components in panel and return this panel. However, JTable may also need to draw another cells while you are editing. It will then ask renderer to provide panel for that - if you use exactly same object you may have a "crostalk" between renderer and editor.
    Whatever you will do, the behaviour of JPanel build in JTable cell will be litle different than standalone JPanel - if for example user double clicks in JTextField in cell, this click won't select JTextField - it will start editor only. Single click may not work at all. I did experimented with JComboBox as an editor and found, that to not confuse user too much it is better to have different view for renderer and editor - this way user will be aware about switching between edit and select/view mode.
    Also, any listeners may get confused in JPanel embeded in JTable - they will recive extra events when you prepre panel to display/edit and may miss many mouse events since they will be attached to event pump only while JTable keeps cell in edit mode.
    You need some experiments, I think. I would start from having two instances of same JPanel subclass - one which will work as a renderer and second, which will work as an editor.

  • Jbutton in a Jlist cell

    I want each cell of Jlist to have some text and image and a button, and this button to have some associated action. How do i do that??
    if i implement ListCellRenderer and extend JButton, then i think the whole thing will become a button.
    Please help,
    Thanks,
    Puneet

    Your'e thimking right but however if you extend JPanel and put all of this components you want things might yust work out fine...
    (JavaGod)

  • How to stop editing in a table cell

    i m using JTextArea as renderer and editor for table cell. While editing i want to stop editing in that cell at some perticular instance. i have used stopCellEditing() function on TableCellEditor but still the Caret does not goes away and when i press any key it still types that character at that place. I want to hide the Caret and do not want any character to print in the cell after calling stopCellEditing().

    What you have already done, and the resulting behavior is unclear from your post. Post more details.

  • How to edit contents of a cell?

    In Numbers 2013, how does one edit the content of a cell besides typing directly in the cell?
    In all prior versions of Numbers (like Excel), there was an editing area below the toolbar that extended across the screen. That area could be used to edit the contents of the currently selected cell.
    Has this been completely removed from Numbers 2013 or is there a way to make it appear? If not, how does one edit a cell when a lot of text is being entered?

    I may not have the proper terminology, but it always starts out covering the cell, and sometimes other adjacent cells, but can be dragged anywhere on the page.
    Jerry

  • Using jtextpane as jlist cell renderer component

    hi,
    I want to use Jlist (in a Jscrollpane) to list a series of boxes of text. The boxes have to be kept the same width, but the height can vary depending on the amount of text.
    I want to use jtextpane because it wraps automatically on word boundaries... although I am confused by the jtextpane functionality...
    but I just can't seem to crack it: presumably its going to involve
    class MyCellRenderer extends JTextPane implements CellRenderer {
    public Component getListCellRendererComponent( ...
    then what ??? help!
    mike rodent
    PS also, how to make Jlist put a line (a single line) between each of the components in its list... it's no good doing setBorder inside the above method, as you then get 2 lines coalescing between adjacent Jlist elements...

    PS also, how to make Jlist put a line (a single line) between each of
    the components in its list... it's no good doing setBorder inside the
    above method, as you then get 2 lines coalescing between adjacent
    Jlist elements...Who says you need to have a Border with top and bottom lines?

  • Problem using an editable JComboBox as JTable cell editor

    Hi,
    i have a problem using an editable JComboBox as cell editor in a JTable.
    When i edit the combo and then I press the TAB or ENTER key then all works fine and the value in the TableModel is updated with the edited one, but if i leave the cell with the mouse then the value is not passed to the TableModel. Why ? Is there a way to solve this problem ?
    Regards
    sergio sette

    if (v1.4) [url
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTa
    le.html#setSurrendersFocusOnKeystroke(boolean)]go
    hereelse [url
    http://forum.java.sun.com/thread.jsp?forum=57&thread=43
    440]go here
    Thank you. I've also found this one (the first reply): http://forum.java.sun.com/thread.jsp?forum=57&thread=124361 Works fine for me.
    Regards
    sergio sette

  • Maintaining editable state in multiple cells on datagrid

    Hi,
    I'm looking to create a component, (and from the tutorials
    i've read, i imagine a datagrid would be best) where i can change
    multiple cells, keep them in an editable state (visual reminder of
    changes made as well as functionally editable) - and to then flush
    the changes through with a keystroke - just hitting return
    basically. the component would be recieving push data through
    httpservices/webservices, and the user will send adjustments back,
    usually multiple changes at once. i've looked at item renderers,
    but that seems to control more the format/appearance of a component
    than its functions?
    Is this possible to do in flex? i'm assessing alongside gwt,
    and trying to figure just how lowlevel the customisation can go in
    flex. any ideas/tutorials/examples would be very much appreciated,
    cheers!

    Hi,
      Refer the sample code
    https://forums.sdn.sap.com/click.jspa?searchID=12153389&messageID=1593610
    Regards
    Kiran Sure

  • Customizing JList Cell Renderer

    Hi all,
    I got a problem with JList not rendering properly in my program. basically I create a customized renderer by extending JPanel and implementing ListCellRenderer.
    but somehow i cannot get the tooltiptext from the JLabel inside my JPanel (the renderer) to display the tooltiptext and responding to my mouse gesture. here's a snippet of my code.
    did i do something wrong?
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    public class TestR {
         private static class ListItem {
              private Color color;
              private String value;
              public ListItem(Color c, String s) {
                   color = c;
                   value = s;
              public Color getColor() {
                   return color;
              public String getValue() {
                   return value;
         private static class MyCellRenderer extends JPanel implements
                   ListCellRenderer {
              private JLabel lbl;
              public MyCellRenderer() {
                   // Don't paint behind the component
                   setOpaque(true);
                   setLayout(new BorderLayout());
                   lbl = new JLabel();
                   add(lbl);
              // Set the attributes of the
              //class and return a reference
              public Component getListCellRendererComponent(JList list, Object value, // value to display
                        int index, // cell index
                        boolean iss, // is selected
                        boolean chf) // cell has focus?
                   // Set the text and
                   //background color for rendering
                   lbl.setText(((ListItem) value).getValue());
                   lbl.setToolTipText("tooltip: "+((ListItem) value).getValue());
                   setBackground(((ListItem) value).getColor());
                   // Set a border if the
                   //list item is selected
                   if (iss) {
                        setBorder(BorderFactory.createLineBorder(Color.blue, 2));
                   } else {
                        setBorder(BorderFactory.createLineBorder(list.getBackground(),
                                  2));
                   return this;
         // Create a window
         public static void main(String args[]) {
              JFrame frame = new JFrame("Custom List Demo");
              frame.addWindowListener(new WindowAdapter() {
                   @Override
                   public void windowClosing(WindowEvent e) {
                        System.exit(0);
              // Use a list model that
              //allows for updates
              DefaultListModel model = new DefaultListModel();
              JList statusList = new JList(model);
              statusList.setCellRenderer(new MyCellRenderer());
              // Create some dummy list items.
              ListItem li = new ListItem(Color.cyan, "test line one");
              model.addElement(li);
              li = new ListItem(Color.yellow, "foo foo foo");
              model.addElement(li);
              li = new ListItem(Color.green, "quick brown fox");
              model.addElement(li);
              // Display the list   
              JPanel panel = new JPanel();
              panel.add(statusList);
              frame.getContentPane().add("Center", panel);
              frame.pack();
              frame.setVisible(true);
    }

    hi!
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    public class TestR {
         private static class ListItem {
              private Color color;
              private String value;
              public ListItem(Color c, String s) {
                   color = c;
                   value = s;
              public Color getColor() {
                   return color;
              public String getValue() {
                   return value;
         private static class MyCellRenderer extends JPanel implements
                   ListCellRenderer {
              private JLabel lbl;
              public MyCellRenderer() {
                   // Don't paint behind the component
                   setOpaque(true);
                   setLayout(new BorderLayout());
                   lbl = new JLabel();
                   add(lbl);
              // Set the attributes of the
              //class and return a reference
              public Component getListCellRendererComponent(JList list, Object value, // value to display
                        int index, // cell index
                        boolean iss, // is selected
                        boolean chf) // cell has focus?
                   // Set the text and
                   //background color for rendering
                   lbl.setText(((ListItem) value).getValue());
                   /*lbl.*/setToolTipText("tooltip: "+((ListItem) value).getValue());
                   setBackground(((ListItem) value).getColor());
                   // Set a border if the
                   //list item is selected
                   if (iss) {
                        setBorder(BorderFactory.createLineBorder(Color.blue, 2));
                   } else {
                        setBorder(BorderFactory.createLineBorder(list.getBackground(),
                                  2));
                   return this;
         // Create a window
         public static void main(String args[]) {
              JFrame frame = new JFrame("Custom List Demo");
              frame.addWindowListener(new WindowAdapter() {
                   @Override
                   public void windowClosing(WindowEvent e) {
                        System.exit(0);
              // Use a list model that
              //allows for updates
              DefaultListModel model = new DefaultListModel();
              JList statusList = new JList(model);
              statusList.setCellRenderer(new MyCellRenderer());
              // Create some dummy list items.
              ListItem li = new ListItem(Color.cyan, "test line one");
              model.addElement(li);
              li = new ListItem(Color.yellow, "foo foo foo");
              model.addElement(li);
              li = new ListItem(Color.green, "quick brown fox");
              model.addElement(li);
              // Display the list   
              JPanel panel = new JPanel();
              panel.add(statusList);
              frame.getContentPane().add("Center", panel);
              frame.pack();
              frame.setVisible(true);
    }try this out. this may help you. note at line no 50. /*lbl.*/setToolTipText("tooltip: "+((ListItem) value).getValue());:)
    Aniruddha

  • InfoPath Form Library cannot edit in Datasheet view, cells delete themself

    Hello,
    I wanted to be able to edit a cell in datashee view for my form library. I have made sure within the publishing wizard that the cells can be edited in datasheet view. This seemed to work fine last week, but now when I input text into the cells, pause for
    3 seconds, the cells then delete themselves???
    I have no idea why, the cells are not read only and I have triple checked they can be edited in datasheet view. they are simple text fields!!
    Has anyone ever seen this behavior before??

    Hi Palumbob,
    I tested the same scenario per your post in my environment, however the datasheet view worked fine when I edited the cells.
    For troubleshooting this issue, I recommend to test the things below:
    Check if there are any workflows or event receivers to set the column value to be null in the library.
    Type value in the simple text field and then click other cells to see how the value works.
    Create a new datasheet for the library to see if the issue still occurs.
    Create a new column in the library and test.
    Create a new library and test.
    Thanks,
    Victoria
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Victoria Xia
    TechNet Community Support

  • JList Cell Height Problem

    I am trying to use a customer cell renderer for a JList. It sort of works, but my cell heights are incorrect because when a cell is selected the JPanel returned from my cell renderer need more space to paint on than an unselected list item.
    Sample code below demontrates the problem - use it as the cell renderer for something and make some selections - you can see that when the text is enlarged when the cell is selected, it is not resized to accommodate the larger text.
    I've tried various combinations of setSize, revalidate etc at various points but nothing helps...
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TestListCellRenderer extends JPanel implements ListCellRenderer {
         JLabel l;
         public TestListCellRenderer() {
              setLayout( new BorderLayout() );
              l = new JLabel( );
              setBorder( BorderFactory.createLineBorder( Color.BLACK ) );
              add( l, BorderLayout.NORTH );
         public Component getListCellRendererComponent(
                   JList list,
                   Object value,
                   int index,
                   boolean isSelected,
                   boolean hasFocus ) {
              if( isSelected == false ) {
                   l.setText( value.toString() );
                   l.setFont( new Font( "Serif", Font.BOLD, 12 ) );
              else {
                   l.setText( value.toString() );
                   l.setFont( new Font( "Serif", Font.BOLD, 18 ) );
              return this;
    }

    I came across this thread when I was having a similar problem. I have found a way to get it to work.
    My problem was this: I have a list of news headlines. When I increased the font size of the items in my list I needed to increase the height of each cell in the list so that the headline wouldn't get cut off top and bottom by the restrictive height of the list cell.
    Solution is to use setFixedCellHeight(int height) from the JList class.
    In my case I find set the fixed cell height in 3 places.
    the constructor - when the list is being created
    the cell renderer method - when the list is being rendered
    in an action listener method - to act when a user changes the size of the font.
    The code excerpt below is a chopped down version of the working code but gives you an idea.
    public class NewsWindow implements ListSelectionListener {
         /** Component which holds the headlines */
         public JList list;
         public NewsWindow(Container parent, WindowProperties properties) {
              DefaultListModel listModel = new DefaultListModel();
              list = new JList(listModel);
              list.setCellRenderer(new HeadlineCellRenderer());
              bounceListCellHeight(); // set the correct list cell height up front
         /** when the font size has changed this method is eventually called via a super class */
         public void fontSizeHasChangedListener() {         
         bounceListCellHeight();
         /** THIS METHOD SETS THE HEIGHT OF THE LIST CELLS */     
         public void bounceListCellHeight() {
              int height = getFontMetrics(getNewsFont()).getHeight(); // get the height of the font
              list.setFixedCellHeight(height + 10); // set the cell height
         * Inner class to render the list.
         private class HeadlineCellRenderer extends JLabel implements ListCellRenderer {
              public HeadlineCellRenderer() {
              setOpaque(true);
              public Component getListCellRendererComponent(JList myList, Object value, int index, boolean isSelected, boolean cellHasFocus) {
              bounceListCellHeight(); // call the method to set the list cell height
              setText(getHeadlineText());
              this.revalidate();
              setFont(getNewsFont());
              return this;
    Hope this is of some help.

  • Changing JList cell renderer on selection

    Hi,
    In our application we need to change the renderer of the selected cell of JList to JTextArea while maintaining default cell renderer for unselected cells. I tried by providing custom cell renderer (code is given below) but it does not work..:-(. Though the component used by JList for rendering the cell is JTextArea, the height of the cell remains same as that of unselected cells. Our requirement is to change the cell height of the selected row so as to give a feel that selected row expands and shows some more information about the selected item to the user.
    Here is the code snippet of the cell renderer that I wrote:
    class CellRenderer1 extends DefaultListCellRenderer{
    private JTextArea selTxtArea;
    CellRenderer1() {
    selTxtArea = new JTextArea(3,20);
    this.setOpaque(true);
    public Component getListCellRendererComponent(JList list,
    Object value, int index,
    boolean isSelected, boolean cellHasFocus) {
    String name = (String) value;
    if ( isSelected ) {
    selTxtArea.setBackground(list.getSelectionBackground());
    selTxtArea.setForeground(list.getSelectionForeground());
    selTxtArea.setText(name + "\n" + name);
    return selTxtArea;
    else {
    this.setBackground(list.getBackground());
    this.setForeground(list.getForeground());
    this.setText(name);
    return this;
    //return this;
    Any pointers or help will be highly appreciated.
    Thanks
    Atul

    JList calculates fixedCellHeight and then uses the same for every cell. This was causing the problem. By overriding the getRowHeight method of BasicListUI class I was able to achieve different cell heights for selected and unselected rows. Following is the code snippet which shows how this was achieved:
    protected int getRowHeight(int row) {
    if ( (cellHeights == null) || (cellHeights.length < row )) {
    cellHeights = new int[row];
    ListModel model = list.getModel();
    Object value = model.getElementAt(row);
    ListSelectionModel selModel = list.getSelectionModel();
    boolean isSelected = selModel.isSelectedIndex(row);
    Component comp = list.getCellRenderer().
    getListCellRendererComponent( list, value, row,
    isSelected, false);
    Dimension dim = comp.getPreferredSize();
    int height = dim.height;
    cellHeights[row] = height;
    return cellHeights[row];
    }

  • Make Editable only for one cell

    Hi,
    Is it possible to make one cell editable in a non-editable column of a matrix?
    I have made a column non-editable.  Now, based on a CFL value (which is in another column) selected, i want to make that row's non-editable column as editable.
    My code, which fails:
    If objMatrix.Columns.Item("V_1").Cells.Item(pVal.Row).Specific.value = 0 Then
                objMatrix.Columns.Item("V_0").Cells.Item(pVal.Row).Specific.Enabled = True
    End If

    If is column set to non editable, you cannot allow to user change the vaule in any cell. The only way is set the column as editable and catch the events as
    If pVal.ItemUID = "matrix" And pVal.BeforeAction = True And pVal.EventType = SAPbouiCOM.BoEventTypes.et_VALIDATE (for cghanges, gotfocus for accesing) And pVal.ColUID = "col" And pVal.ItemChanged = True Then
    which will catch the changes of cell and if the cell isnt enabled for changes, set bubleevent to true.

Maybe you are looking for

  • Creation of new logical systems in IS Retail

    Hi All,   "For every new store opened, create one Logical System"...i have come across this sentence in 1 of the documents which spaeks about creation of logical systems So should i create one Logical System for every store and if i am sending Gift V

  • HT5639 how to install bootcamp on windows 7 64 bit on late 2006 imac

    I ma trying to get Windows 2007 ultimate to install boot camp on my late 2006 24"CTO iMac. I am able to install the OS but when it come to install bootcamp it tells me that 64 bit is not supported on this machine. I need to be able to install it so a

  • Edge 540 model : when I connect/disconnect AC power cord, screen goes dark

    For the past few days, everytime I connect the power cord, the screen goes dark for a second. The same thing happens when I disconnect the power cord. Can someone help me find out what is wrong with it? Is it a hardware issue or is it just a momentar

  • After "insufficient power" message, N9 appears to ...

    Hi, I've got an N9 on PR1.0 (10.2011.34-1.339.01.2_PR_339). When I connect it to my car charger (2 different, actually), or my GUM plus charger, they're recognized as a computer. I just tap the "X" to dismiss the dialog, but then I get the "insuffici

  • JDeveloper 10g - Connection problems

    I have start testing with JDeveloper 10g and made a connection to Oracle Lite 9. The Navigator shows the tables but when clicking on a table it returns with an error [POL-5205] column not found. A test with Mysql behaved about the same, it showed the