Solved

How to disable checkbox in a GUI in Java SWT?

Posted on 2013-11-18
1
674 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
1 Comment
 
LVL 35

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

726 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