Solved

How to disable checkbox in a GUI in Java SWT?

Posted on 2013-11-18
1
692 Views
Last Modified: 2013-11-19
Hi,
I am working on creating a GUI using JAVA SWT package. In this GUI I have three checkboxes to filter the results on a table.

However, it takes some time to populate the whole data on the table and I do not want the users to use the checkboxes before the the data generation is completed.

Therefore, I want to disable these checkboxes while the process is going on. I know how to dosable icons but I don't how to disable checkboxes.

Can you please help  me with it?


This is how I call this class in the main method in GUI.java class

public static void main(String[] args) throws SQLException, ClassNotFoundException {

SOME CODE IN HERE
 
csFilteringCheckBoxes = new CSFilteringCheckBoxes(compRESULTS, SWT.FLAT);

SOME CODE IN HERE

}


This is the  class that creates the checkboxes and does the filtering:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

public class CSFilteringCheckBoxes extends Composite {
    
    private static Shell shell;
    private static Table table;
    private static String[] titles;
    private static Group groupResults;
    private static Display display;
    
    private Button error = new Button(this, SWT.CHECK);
    private Button failed = new Button(this, SWT.CHECK);
    private Button passed = new Button(this, SWT.CHECK);
    private Label caption = new Label(this, SWT.HORIZONTAL | SWT.CENTER);
    
    private static List<CheckDetail> checkDetails = null;
    private static List<CheckDetail> checkDetailsTable = null;

    boolean fileCreated = false;
    boolean unChecked = false;
    
    
    public CSFilteringCheckBoxes(Composite parent, int style) {
        super(parent, style);
        Font font = new Font(parent.getDisplay(),"Arial",9, SWT.NORMAL);
        final Color lightgray;
        lightgray = new Color (parent.getDisplay(), 140, 140, 140);
        
        GridLayout gl = new GridLayout(); gl.verticalSpacing = 4;
        setLayout(gl);
        error.setText("Error");
        failed.setText("Failed");
        passed.setText("Passed");
        caption.setText("Filtering");
        caption.setFont(font);
      
        setAllChecked();
        
        
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption
        setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        error.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        failed.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        passed.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        caption.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        
        
        failed.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (failed.getSelection()) {
                    unChecked = false;
                    //show failed checks
                  //DEBUG:: System.out.println("Failed checked");
                    try {
                        showFilteredResults("Failed");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    try {
                        unChecked = true;
                        showFilteredResults("Failed");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                  
                }
            }
        });
   
    
        
        passed.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (getFilterChecks().get(0).getPassedStatus()) {
                    unChecked = false;
                    //show failed checks
                  //DEBUG:: System.out.println("Passed checked");
                    try {
                        showFilteredResults("Passed");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    try {
                        unChecked = true;
                        showFilteredResults("Passed");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                }
            }
        });
        
               
        error.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
               
                if (error.getSelection()) {
                    unChecked = false;
                    //show failed checks
                  //DEBUG:: System.out.println("Error checked");
                    try {
                        showFilteredResults("Error");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    try {
                        unChecked = true;
                        showFilteredResults("Error");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                }
            }
            });              
     } 
    
    public void showFilteredResults(String status) throws IOException, ClassNotFoundException{

        List<CheckDetail> checkDetails = null;
        List<CheckDetail> checkDetailsTable = null;
        String content = "";
        
        String tempDir = null;
        String OS = System.getProperty("os.name").toLowerCase();
        
        if (OS.indexOf("win") >= 0){
            tempDir = "C:/Temp/mtcheck";
        } else{
            tempDir = "/tmp/mtcheck";
        }
        
        
        File f = new File(tempDir + "/" + "mtcheckDetails.txt");
        if (!f.exists()){
            checkDetails = CheckRunner.getList();
            
            FileOutputStream fout= new FileOutputStream (tempDir + "/" + "mtcheckDetails.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(checkDetails);
            fout.close();
            
            FileOutputStream foutTable= new FileOutputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
            ObjectOutputStream oosTable = new ObjectOutputStream(foutTable);
            oosTable.writeObject(checkDetails);
            foutTable.close();
        }
        
        FileInputStream fin= new FileInputStream (tempDir + "/" + "mtcheckDetails.txt");
        ObjectInputStream ois = new ObjectInputStream(fin);
        checkDetails= (List<CheckDetail>)ois.readObject();
        fin.close();

        
        FileInputStream finTable= new FileInputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
        ObjectInputStream oisTable = new ObjectInputStream(finTable);
        checkDetailsTable= (List<CheckDetail>)oisTable.readObject();
        finTable.close();
        
        boolean edited = false;
        boolean newRun = false;
        
        if (!unChecked){  //means checked
            for(Iterator<CheckDetail> i = checkDetails.iterator();i.hasNext();) {
            CheckDetail checkDetail = i.next();
          //DEBUG:: System.out.println("Detail Status:" + checkDetail.getStatus());
            
                if (!status.equalsIgnoreCase("ERROR")){
                      if (!checkDetail.getStatus().equalsIgnoreCase(status)){
                          i.remove();
                          edited = true;
                          newRun = false;
                      } else{
                          edited = true;
                          newRun = false;
                      }
                } else {
                    if (!checkDetail.getStatus().contains("n/a")){  //This is a special case for errors. It will change after I update the CLI error message format.  
                        i.remove();
                        edited= true;
                        newRun = false;
                    } else{
                        edited= true;
                        newRun = false;
                    }
                }
            }                      
            checkDetailsTable.addAll(checkDetails);
        }
            
        if(unChecked){
            if (checkDetailsTable != null){
                for(Iterator<CheckDetail> i = checkDetailsTable.iterator();i.hasNext();) {
                    CheckDetail checkDetailTable = i.next();
                  //DEBUG:: System.out.println("Detail Status:" + checkDetailTable.getStatus());                  
                    
                    if (!status.equalsIgnoreCase("ERROR")){
                        if (checkDetailTable.getStatus().equalsIgnoreCase(status)){
                            //checkDetails.remove(i);
                            i.remove();
                            edited= true;
                            newRun = true;
                        } else{
                            edited= true;
                            newRun = true;
                        }
                    } else {
                        if (checkDetailTable.getStatus().contains("n/a")){
                            i.remove();
                            edited= true;
                            newRun = true;
                        } else{
                            edited= true;
                            newRun = true;
                        }
                    }
                }
            }
            checkDetails = checkDetailsTable;
        }
                  
        FileOutputStream foutTable= new FileOutputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
        ObjectOutputStream oosTable = new ObjectOutputStream(foutTable);
        oosTable.writeObject(checkDetailsTable);
        foutTable.close();
        
        //get the list of failed  checks
        if (edited){
            boolean evenRow = true;
            Gui.populateResultsTable(checkDetails, shell, table, titles, newRun, evenRow, groupResults);  
        }
    }
    
    
    public static void setTable(Table table, String[] titles) {
        CSFilteringCheckBoxes.table = table;
        CSFilteringCheckBoxes.titles = titles;
    }  
    
    
    public static void setShell(Shell shell, Display display) {
        CSFilteringCheckBoxes.shell = shell; 
        CSFilteringCheckBoxes.display = display;
    }
    
    public static void setGroupResults(Group groupResults) {
    	CSFilteringCheckBoxes.groupResults = groupResults; 
    }
    
    public void setAllChecked(){
        error.setSelection(true);
        //DEBUG:: System.out.println("Error Status after: " + error.getSelection());
        failed.setSelection(true);
        passed.setSelection(true);       
    }
       
    public List<FilterCheckStatus> getFilterChecks(){
        FilterCheckStatus filterCheckStatus = new FilterCheckStatus();
        List<FilterCheckStatus> filterCheckStatuses = new ArrayList<FilterCheckStatus>();
        
        boolean errorState = error.getSelection();
        filterCheckStatus.setErrorStatus(errorState);
        boolean failedState = failed.getSelection();
        filterCheckStatus.setFailedStatus(failedState);
        boolean passedState = passed.getSelection();
        filterCheckStatus.setPassedStatus(passedState);
        
        filterCheckStatuses.add(filterCheckStatus);
        return filterCheckStatuses;
    }
    
    public static void setTableDataInitialData() throws IOException{
        checkDetails = CheckRunner.getList();
        
        String tempDir = null;
        String OS = System.getProperty("os.name").toLowerCase();
        
        if (OS.indexOf("win") >= 0){
            tempDir = "C:/Temp/mtcheck";
        } else{
            tempDir = "/tmp/mtcheck";
        }
        
        FileOutputStream foutTable= new FileOutputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
        ObjectOutputStream oosTable = new ObjectOutputStream(foutTable);
        oosTable.writeObject(checkDetails);
        foutTable.close();        
    }
    
    public static List<CheckDetail> getTableData() throws IOException, ClassNotFoundException{
              
        String tempDir = null;
        String OS = System.getProperty("os.name").toLowerCase();
        
        if (OS.indexOf("win") >= 0){
            tempDir = "C:/Temp/mtcheck";
        } else{
            tempDir = "/tmp/mtcheck";
        }
        
        FileInputStream finTable= new FileInputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
        ObjectInputStream oisTable = new ObjectInputStream(finTable);
        checkDetailsTable= (List<CheckDetail>)oisTable.readObject();
        finTable.close();
        
        return checkDetailsTable;
    }
            
}

Open in new window

0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39658424
You should be able to control it with something as simple as...
csFilteringCheckBoxes.setEnabled(false);

Open in new window

(And setting that to "true" again when the process is complete)
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

696 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