Create an application similar to the eclipse available updates

The eclipse IDE tool has a nice feature called Check for Updates, which captures all available updates and gives the client a choice at runtime for what to update. In this tutorial, we will create an application similar to the eclipse available updates.

You will learn how to:

  • Custom LookAndFeel on static block
  • Initialize the JTable data from DefaultTableModel
  • Add ListSelectionListener and event handler
  • Use propertyChange to detect which cell is editing

 

Prerequisites

Before you begin to read this tutorial, you must:

  1. Familiar with Java swing development. Go to Oracle Java Swing to get the tutorial.
  2. Familiar with the first three tutorials on JSwing.
  3. Familiar with the Operation of Apache NetBeans.
  4. Familiar with the timing and object of propertyChange get called.

 

Create an AvailableUpdates Form

  1. Create a JFrame class named AvailableUpdates on the form of the packages, and then drag the necessary components onto the form, the tree diagram as shown in the following figure.

    AvailableUpdates tree-view

  2. Change the properties of the components as shown in the table below.
    ComponentstextactionCommandname
    jButton1Select AllSelect AllSelect All
    jButton2Deselect AllDeselect AllDeselect All
    jButton3InstallInstallInstall
    jButton4CancelCancelCancel
    jLbael1<html><strong>Available Updates</strong></html>  
    jTextField1Check the updates that you wish to install.  
    jTextArea  Description
  3. After the successful step above, Press Ctrl-S to save the file, and press F9 to Compile the Form, shown as the following.

    AvailableUpdates Form

Create an AvailableUpdates App

Create a Java class named AvailableUpdates on the app, and then add code to the Class.

package app;

import javax.swing.event.ListSelectionEvent;
import jswing.application.*;
public class AvailableUpdates extends App {
    static {
        try {
            javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    
    public static void main(String args[]) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Form.open("form.AvailableUpdates","AvailableUpdates");
                UserInterface.initialize("app.AvailableUpdates",Form.getCurrentForm());
                tableInit();
            }
        });
    }
    
    private static void tableInit(){
        final Object dataVector[][] = {
            {false,"e(fx)clipse - IDE","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - IDE - Basic","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - IDE - Convert","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - IDE - CSS","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - IDE - DSL to setup JavaFX based code editors","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - IDE - FXGraph","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - IDE - FXML","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - IDE - GModel Feature","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - IDE - 10nSupport","3.7.0.20221031","org.eclipse.fx.ide"},
            {false,"e(fx)clipse - PDE - FXGraph","3.7.0.20221031","org.eclipse.fx.ide"}
        };
        final String columnName[] = {"Select","Name","Version","Id"};
        
        //Description        
        final String descr[] = {"Feature collecting all IDE-Features for easy install",
                                "Feature holding basic IDE addons like the basic JDT-Integration",
                                "This feature holds the SVG conversion support",
                                "Feature holding relevant CSS extensions",
                                "DSL to create code editors",
                                "Feature providing FXGraph editing support",
                                "Feature holding all FXML support",
                                "GModel Feature to model generic serialized data",
                                "Feature holding all localization (l10n) support",
                                "PDE integration for JavaFX"};

        //set table model
        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) ((javax.swing.JTable)Form.getCurrentForm().getComponentByName("AvailableList")).getModel();
        model.setDataVector(dataVector, columnName);
        
        //add selectionListener
        final javax.swing.ListSelectionModel listSelectionModel = new javax.swing.DefaultListSelectionModel();
        javax.swing.event.ListSelectionListener selectionListener = new javax.swing.event.ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                String s = descr[listSelectionModel.getMaxSelectionIndex()];
                ((javax.swing.JTextArea)Form.getCurrentForm().getComponentByName("Description")).setText(s);
            }
        };
        listSelectionModel.addListSelectionListener(selectionListener);
        ((javax.swing.JTable)Form.getCurrentForm().getComponentByName("AvailableList")).setSelectionModel(listSelectionModel);
    }   
    
    @Override
    public void propertyChange(java.awt.Component component){
        if (component.equals(Form.getCurrentForm().getComponentByName("AvailableList")) &&
            ((javax.swing.JTable)(component)).getEditingColumn() == 0) {
            int row = ((javax.swing.JTable)(component)).getEditingRow();
            boolean b = false;
            if(((javax.swing.JTable)(component)).getValueAt(row, 0).equals(true)){
                b = true;
            } else {
                int j = ((javax.swing.JTable)(component)).getRowCount();
                for (int i = 0; i < j; i++){
                    if((((javax.swing.JTable)(component)).getValueAt(i, 0)) == null) {
                    }    
                    else {    
                        b = (Boolean)((javax.swing.JTable)(component)).getValueAt(i, 0);
                        if(b){
                            i = j;                            
                        }
                    }
                }
            }
            ((javax.swing.JButton)Form.getCurrentForm().getComponentByName("Install")).setEnabled(b);
        }
    }
    
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e){
        String action = e.getActionCommand();
        switch(action){
            case "Install":
                javax.swing.JOptionPane.showMessageDialog(null,
                    "Finish",
                    "Dialog",
                    javax.swing.JOptionPane.INFORMATION_MESSAGE);
                break;
            case "Select All":
                setChoiceValue(((javax.swing.JTable)Form.getCurrentForm().getComponentByName("AvailableList")),true);
                break;
            case "Deselect All":
                setChoiceValue(((javax.swing.JTable)Form.getCurrentForm().getComponentByName("AvailableList")),false);
                break;
            case "Cancel":
                System.exit(0);
        }
    }   
    private void setChoiceValue(javax.swing.JTable table, boolean b){
        int j = table.getRowCount();
        for(int i = 0; i < j; i ++){
            if((table.getValueAt(i, 0)) == null) {
            } else {    
                table.setValueAt(b, i, 0);
                ((javax.swing.JButton)Form.getCurrentForm().getComponentByName("Install")).setEnabled(b);
            }    
        }
    }
}

 

Code Explained

  1. ListSelectionListener

    By default, all of the components in java swing have built-in all suitable event listeners on JSwing. In this tutorial, we wish that shows the description of available updates of the item, so we must manually add a ListSelectionListener and an event handler named valueChanged and then display the description of the item if the item is selected.

  2. propertyChange

    propertyChange gets called when a bound property is changed. In this tutorial, propertyChange gets called when a bound property is changed. In this tutorial, we use it to detect changes in selected or unselected and then enable or disable the named install button.

 

Run application

Press Ctrl-S to save the file, and press Shift-F6 to Run the application, and then you will get the following output.

AvailableUpdates

 

Exercises

  1. Redesign this application and make it can filter items of the available updates.

Next steps

Click download source code or next for more tutorials.

Share this page

Let more people know about this site, please share