Through event-driven, create different parts

A very easy thing is to dispose the first re-create.

Written mainly to see if they have a better realization method

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
 *  The program purpose: event-driven, dynamically created components simple demo.
 *  Select Yes when: choose from the drop-down box.
 *  Select "no" when you are making a specific text configuration
 * @author Administrator
 */
public class MyComposite {

        private  Button useOutSourceButton;
        private  Button newSourceButton;
        private  Composite dbSourceComposite;
        private  Shell shell;

        /**
         *  The main program
         * @param args
         */
        public static void main(String[] args) {
                MyComposite myComposite = new MyComposite();

                myComposite.open();
        }

        /**
         * shell
         */
        private void open() {
                Display display = Display.getDefault();
                shell = new Shell(display);
                shell.setText(" Component replacement test  ");
                shell.setSize(400, 200);
                shell.setLayout(new GridLayout(3, false));

                final Label label = new Label(shell, SWT.NONE);
                label.setLayoutData(new GridData(100, SWT.DEFAULT));
                label.setText(" Use the database resources  ");

                useOutSourceButton = new Button(shell, SWT.RADIO);
                useOutSourceButton.setText(" Is  ");
                newSourceButton = new Button(shell, SWT.RADIO);
                newSourceButton.setText(" No  ");

                dbSourceComposite = new Composite(shell, SWT.NONE);
                dbSourceComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,true, false,3,1));
                dbSourceComposite.setLayout(new GridLayout(3,false));

                createInnerListeners();

                shell.open();
                while (!display.isDisposed()) {
                        if (!display.readAndDispatch()) {
                                display.sleep();
                        }
                }
                display.dispose();

        }

        /**
         *  Event listener
         */
        protected void createInnerListeners() {
                useOutSourceButton.addSelectionListener(new SelectionListener() {
                        public void widgetSelected(SelectionEvent e) {
                                Button btn = (Button) e.getSource();
                                if (btn.getSelection()) {
                                        initSelectDBSourceUI(dbSourceComposite);
                                }
                        }

                        public void widgetDefaultSelected(SelectionEvent e) {
                        }
                });
                newSourceButton.addSelectionListener(new SelectionListener() {
                        public void widgetSelected(SelectionEvent e) {
                                Button btn = (Button) e.getSource();
                                if (btn.getSelection()) {
                                        initConfigDBSourceUI(dbSourceComposite);
                                }
                        }

                        public void widgetDefaultSelected(SelectionEvent e) {
                        }
                });
        }

        /**
         *  Create part Group 1
         * @param composite
         */
        private void initConfigDBSourceUI(final Composite composite) {
                this.disposedComposite(composite);
                Text configureText = new Text(composite, SWT.BORDER);
                configureText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
                                false));
                configureText.setEditable(false);
                Button configureButton = new Button(composite, SWT.NONE);
                configureButton.setText(" Configuration  ");
                Button resetButton = new Button(composite, SWT.NONE);
                resetButton.setText(" Reset  ");
                shell.layout(true, true);
        }

        /**
         *  Create part Group 2
         * @param composite
         */
        private void initSelectDBSourceUI(final Composite composite) {
                this.disposedComposite(composite);
                Label dbTypeLabel = new Label(composite, SWT.NONE);
                dbTypeLabel.setText(" Database resource name  ");
                dbTypeLabel.setLayoutData(new GridData(100, SWT.DEFAULT));
                dbTypeLabel.setToolTipText(" Select an existing database resources  ");

                CCombo dbCombo = new CCombo(composite, SWT.BORDER);
                dbCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
                dbCombo.setEditable(false);

                Button resetButton = new Button(composite, SWT.NONE);
                resetButton.setText(" Reset  ");
                shell.layout(true, true);
        }

        /**
         *  Destroy the original parts
         * @param composite
         */
        private void disposedComposite(Composite composite) {
                Control[] arrays = composite.getChildren();
                for (Control control : arrays) {
                        if (control != null)
                                control.dispose();
                }
        }
}

Effect Picture:

Through event-driven, create different parts

Through event-driven, create different parts

分类:Java 时间:2010-03-29 人气:222
分享到:
blog comments powered by Disqus

相关文章

  • String [] args = new String [] {sql.toString ()} 2011-06-29

    In this problem StringBuffer sql = new StringBuffer (); Which sql.toString results: 'Test User 1', 'male' String [] args = new String [] {sql.toString ()} args.length 1 results Obviously not what we need. Note: new String []{"","",&quo

  • C#中List〈string〉和string[]数组之间的相互转换 2014-07-14

    List<string>和string[]数组之间的相互转换,需要的朋友可以参考下 1,从System.String[]转到List<System.String> System.String[] str={"str","string","abc"}; List<System.String> listS=new List<System.String>(str); 2, 从List<System.

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • How the eclipse of the main the String [] args parameter passed, and then export it 2010-12-26

    How the eclipse of the main the String [] args parameter passed, and then export it? In the Run menu to find Open Run dialog opens Select Java Application in the program need to run Select the Arguments Tab Enter the parameters in the Programe Argume

  • 浅析C#中的Main(String[] args)参数输入问题 2014-07-20

    本篇文章主要是对C#中的Main(String[] args)参数输入问题进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 指定相关的测试代码 首先,写一个用于测试的关于Main(String[] args)参数输入有关的代码类,如下: using System; public class Hello { public static void Main(String[] args) { switch (args[0]) { case "老板": Console.Write

  • A classic problem of Java String objects (new String ()) 2010-03-29

    First look at an example, the code is as follows: public class Test { public static void main(String[] args) { String str = "abc"; String str1 = "abc"; String str2 = new String("abc"); System.out.println(str == str1); System.

  • Get a string in another string appear in all locations 2010-03-24

    public class StringTest { public static void main(String args[]) { String s = "hello, this is a test this is a test this is a this is test"; String p = "test"; for(int i = 0; i < s.length(); i ++) { int at = s.indexOf(p, i); if(at &

  • [String handling 2] string edit distance algorithm 2010-03-15

    We look at a practical application. Many modern search technology to provide high quality, efficient service as the target. For example: baidu, google, sousou and other well-known full-text search system. When we enter a wrong query = "Jave" whe

  • A relatively straightforward question, to copy a string to another string, string required to remove extra spaces, leaving only one. 2010-06-02

    A relatively straightforward question, to copy a string to another string, string required to remove extra spaces, leaving only one. This is Ubisoft's development department (Chi Pak) recruiting software engineers questions document. Intended to stud

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.382 (s). 12 q(s)