Advertisement
Guest User

MainWindow

a guest
Feb 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. import org.eclipse.swt.SWT;
  2. import org.eclipse.swt.widgets.Display;
  3. import org.eclipse.swt.widgets.Shell;
  4. import org.eclipse.swt.widgets.Text;
  5. import org.eclipse.swt.widgets.Button;
  6. import org.eclipse.swt.widgets.Group;
  7. import org.eclipse.swt.layout.FillLayout;
  8. import org.eclipse.swt.events.*;
  9.  
  10.  
  11. //Main window
  12. public class MainWindow
  13. {
  14.     Display display;
  15.     Shell shell;   
  16.     Text text;
  17.     int radiobuttonsQuantity = 3;
  18.     Button[] radiobuttons;
  19.     Button button;
  20.    
  21.     //Run the window
  22.     public void run()
  23.     {
  24.         display = new Display();
  25.         shell = new Shell();
  26.         shell.setSize(480, 320);
  27.         shell.setText("Lab 1");
  28.         createContent(shell);
  29.         shell.open();
  30.        
  31.         //The cycle wich track down windows' destruction
  32.         while (!shell.isDisposed())
  33.         {
  34.             if (!display.readAndDispatch())
  35.             {
  36.                 display.sleep();
  37.             }
  38.         }
  39.         display.dispose();
  40.     }
  41.    
  42.     //Create main window's content
  43.     private void createContent(Shell shell)
  44.     {
  45.         //Vertical FillLayout
  46.         FillLayout fillLayout = new FillLayout();
  47.         fillLayout.type = SWT.VERTICAL;
  48.         fillLayout.marginHeight = fillLayout.marginWidth = fillLayout.spacing = 10;
  49.        
  50.         //Text input field
  51.         text = new Text(shell, SWT.NULL);
  52.         text.setData("Enter smth: ");
  53.        
  54.         //Radiobutton grouping
  55.         Group radiobuttonsGroup = new Group(shell, SWT.NONE);
  56.         radiobuttonsGroup.setLayout(new FillLayout(SWT.VERTICAL));
  57.         radiobuttonsGroup.setText("Choices");
  58.        
  59.         //A procedural generation of radiobuttons
  60.         radiobuttons = new Button[radiobuttonsQuantity];
  61.         for (int currRadiobuttonNumb = 0; currRadiobuttonNumb < radiobuttonsQuantity; currRadiobuttonNumb++)
  62.         {
  63.             radiobuttons[currRadiobuttonNumb] = new Button(radiobuttonsGroup, SWT.RADIO);
  64.             radiobuttons[currRadiobuttonNumb].setSelection(false);
  65.             radiobuttons[currRadiobuttonNumb].setText("RB " + Integer.toString(currRadiobuttonNumb + 1));
  66.         }
  67.        
  68.         //Button
  69.         button = new Button(shell, SWT.PUSH);
  70.         button.setBounds(40, 50, 50, 20);
  71.         button.setText("Click Me");
  72.         //???
  73.         button.addSelectionListener(new SelectionListener()
  74.         {
  75.            
  76.             @Override
  77.             public void widgetSelected(SelectionEvent event)
  78.             {
  79.                 radiobuttonSelector(text.getText());
  80.             }
  81.            
  82.             @Override
  83.             public void widgetDefaultSelected(SelectionEvent event)
  84.             {
  85.               text.setText("No problem");
  86.             }
  87.           });
  88.        
  89.         shell.setLayout(fillLayout);
  90.     }
  91.    
  92.     //Select inputted radiobutton
  93.     public void radiobuttonSelector(String text)
  94.     {
  95.         boolean isFound = false;
  96.         for (int currRadiobuttonNumb = 0; currRadiobuttonNumb < radiobuttonsQuantity; currRadiobuttonNumb++)
  97.         {
  98.             if (text.equals(radiobuttons[currRadiobuttonNumb].getText()))
  99.             {
  100.                 radiobuttons[currRadiobuttonNumb].setSelection(true);
  101.                 isFound = true;
  102.             }
  103.         }
  104.         if (!isFound)
  105.         {
  106.             new ErrorWindow(shell).run();
  107.         }
  108.     }
  109.    
  110.     public static void main(String[] args)
  111.     {
  112.         new MainWindow().run();
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement