Results 1 to 6 of 6
  1. #1
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default Diable JList in JDialog at runtime

    Hi,

    I am facing a problem with JDialog. I have a JDialog(Modal) which have some panels and controls like- JList, JTextField, JButton etc.

    I have two JList one is LstCategory and second is lstSubcategory. If the user select a category and that is valid, lstSubcategory will fill with some options. But if the category is not valid, lstSubcategory will be disabled.

    I have used the methods like:

    lstSubcategory.setEnabled(false);
    or
    ScrollSubCategory..setEnabled(false);

    But no function is working.

    What should be done to disable or enable lstSubcategory at run time?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    Hi,

    I am doing that in the LstCategory SelectionChaged event. the code part is:

    Java Code:
    public class SaveDialog
    {
       JDialog dlgSave;
      JList lstCategory,lstSubCategory;
      DefaultListModel lstModel, lstSubModel;
      JScrollPane scrollcategory, ScrollSubCategory;
      //Other controls
       ........................
    
      SaveDialog()
      {
        lstModel = new DefaultListModel( );
        lstSubModel = new DefaultListModel( );
        dlgSave = new JDialog();
        dlgSave.setTitle("Save Parameters");
        dlgSave.setModal(true);
    
        lstCategory = new JList(lstModel);
        lstSubCategory = new JList(lstSubModel);
    
        lstCategory.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
        lstCategory.addListSelectionListener(new javax.swing.event.ListSelectionListener(){
                 public void valueChanged(ListSelectionEvent e) {
                    LstCategorySelectionChanged(e);
                }
            } );
           scrollcategory = new JScrollPane(lstCategory);
           scrollcategory.setHorizontalScrollBar(null);
           scrollcategory.setBounds(3, 200, 180, 45);
    
          lstSubCategory.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
          ScrollSubCategory = new JScrollPane(lstSubCategory);
          ScrollSubCategory.setHorizontalScrollBar(null);
          ScrollSubCategory.setBounds(205, 200, 180, 45);
    
           // Fill category list with some values
           ...................................
    
           JPanel panel = new JPanel();
           panel.setLayout(null);
           panel.setPreferredSize(new Dimension(400,513));
    
           panel.add(scrollcategory);
           panel.add(ScrollSubCategory);
          // other controls and panels
         ....................
         dlgSave.add(panel);
        dlgSave.setResizable(false);
        dlgSave.setVisible(true);
      }  // close cunstructor
    
      // lst category selection changed event
      // if the user select a valid category, the lstsubcategory will fill up with some values
      // and enabled otherwise lstsubcategory will be disabled here
      void LstCategorySelectionChanged(ListSelectionEvent e)
        {
            int idx = lstCategory.getSelectedIndex();
            if(idx>=0)
            {
                 if(!lstSubCategory.isEnabled())
                        lstSubCategory.setEnabled(true);  // not working
    
                if(lstSubModel.getSize()>0)
                    lstSubModel.clear();
    
               // Check the selected value is valid, fill the subcategory
               if(valid)
              {
                      // fill the subcategory
                      ..........................
              } 
              else
               {
                    lstSubCategory.setEnabled(false);  // not working
               }
            }
                        
         } // closing selection event
    } // closing of the class
    I am unable to find out where the problem is?
    Last edited by Eranga; 05-31-2010 at 03:45 AM. Reason: code tags added

  4. #4
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > What should be done to disable or enable lstSubcategory at run time?

    worked OK for me

  5. #5
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    I have find out where the problem was. Thanks to all.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by png View Post
    I have find out where the problem was. Thanks to all.
    Nice to hear that you've solve the problem. And also if someone else comes with the quite similar issue, your comment may be really helpful. So why don't you send the solution here as well.

    And also please use code tags when you posting code segments in the forum. Unformated codes are really hard to read. If you don't know how to do it, please look at the my forum signature. You can find the related link.

Similar Threads

  1. JDialog
    By frenk_castle in forum AWT / Swing
    Replies: 3
    Last Post: 05-11-2010, 12:28 PM
  2. Close JDialog
    By sky in forum AWT / Swing
    Replies: 7
    Last Post: 11-16-2009, 06:01 PM
  3. JDialog appearing in the toolbar
    By riddick in forum New To Java
    Replies: 6
    Last Post: 11-06-2009, 02:19 PM
  4. setLocation on a JDialog is ignored
    By ScottVal in forum AWT / Swing
    Replies: 7
    Last Post: 01-13-2009, 07:35 AM
  5. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 05:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •