Button Logic

Posted on 2011-10-20
Last Modified: 2012-05-12
I have a Remove button on my base dialog.  The action of the button is to remove a selected row from my jtable.  So if there is not any objects in the my jtable the button should be grayed out or invisible - I don't care which.  So I created some logic around the button to see if there are are in objects in the table.

      if(checkList > 0){

I want to start the button out not there or not enabled in case there are no data in the table.  This works.  So now the user can add objects to the ArrayList in which updates the table.  So now I have a object in the table and the button is not enabled.  I can close the dialog and reopen it and it is fine.  

How do I let the button know that there has been a change to the jtable and to check the logic again?
Question by:jkteater
    LVL 86

    Accepted Solution

    In the same routine that fires TableModel events, check the state of your underlying Collection and en/disable the button accordingly
    LVL 47

    Assisted Solution

    In the same place where your program adds a line to your table - enable / dsable the button. And it does not matter that it may happen in different class. Have a methiod in your original class (with the button) wqhich would enable/disable the button, pass the handle to that class to any other class where it is necessary and use. This handle to invoke the enable/disable button of the original class. Make sure that your button is an instance vraiable of course

    Author Comment

    does this look like right for the method in the singleton class

    static ArrayList<RevDataset> rds = new ArrayList<RevDataset>();
    JButton removeButton = new JButton("Remove Dataset");
    private static SingletonSelectTable instance = null;

     public JButton getButton() {
             if(rds.size() > 0){
             else {
            return removeButton;

    so to use the button in the dialog class woud it be something like

    myModel.getButton().addActionListener(new ActionListener() {}  ??

    Author Comment

    I have it working thanks to both of you
    LVL 86

    Expert Comment

    btw, i'm talking about http:Q_27390851.html#36949991
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now