Results 1 to 3 of 3
  1. #1
    daveyboy79 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default Update jButton from ListSelectionHandler

    Hi,

    Just starting a Java conversion from .NET so I do understand programming principals, just from the Microsoft world...

    I'm trying to update a button on a jPanel, so that it is enabled/disabled if a table row is selected. I have looked at all the examples from sun.com etc for information but unable to figure it out.

    I have a ListSelectionListener registered with the jTable selectionModel, but how do I reference the button in the jForm, as every time I write btnEdit, it is highlighted as an unrecognised variable?!?!

    Thanks for all assistance in advance.

    Java Code:
    public class ReplicatorView extends FrameView implements WindowListener {
    
        public ReplicatorView(SingleFrameApplication app) {
            super(app);
    
            initComponents();
            ...
            ...
            // Now my extra setup component setup
            this.getFrame().addWindowListener(this);
            jTable1.setModel(_tableModel);
            ListSelectionModel listSelectionModel = jTable1.getSelectionModel();
            listSelectionModel.addListSelectionListener(new ReplicatorListSelectionHandler());
            
            // Now initialise App
            LoadConfig();
    
        }
    }
    
    
    public class ReplicatorListSelectionHandler implements  ListSelectionListener {
        
        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel)e.getSource();
            
            // as we only have single row selection, this should be adequate
            if (!lsm.getValueIsAdjusting() && lsm.isSelectionEmpty())
            {
                btnEdit.setEnabled(false);
            }
            
            
        }
        
    }

  2. #2
    daveyboy79 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi all,

    Just realised that the class ReplicatorListSelectionHandler needs to be an internal class defined inside ReplicatorView.

    I did not pick up on this before, now it works. Hope this may help someone else in the future starting out. This had been bugging me for a couple of days before I realised the simple solution.

    Regards,

    Dave

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by daveyboy79 View Post
    Just realised that the class ReplicatorListSelectionHandler needs to be an internal class defined inside ReplicatorView.
    Thanks for posting your solution. This is one way to solve this dilemma, but not the only way, and not necessarily the best way. If you want to keep your Listener class as a separate stand-alone class, you can do so by passing a reference of the object that holds the button to the listener either by way of the listener's constructor or a setter method. Then you can set the button's enabled status via a public method that's in the object that holds the button (the button variable itself should be private).

Similar Threads

  1. How to update my jdk???
    By low224 in forum New To Java
    Replies: 4
    Last Post: 01-04-2009, 04:51 PM
  2. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM
  3. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:49 PM
  4. JButton onClick?
    By Joe2003 in forum AWT / Swing
    Replies: 2
    Last Post: 01-06-2008, 03:04 PM
  5. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 10:19 AM

Tags for this Thread

Posting Permissions

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