Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Text;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Group;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.events.*;
- //Main window
- public class MainWindow
- {
- Display display;
- Shell shell;
- Text text;
- int radiobuttonsQuantity = 3;
- Button[] radiobuttons;
- Button button;
- //Run the window
- public void run()
- {
- display = new Display();
- shell = new Shell();
- shell.setSize(480, 320);
- shell.setText("Lab 1");
- createContent(shell);
- shell.open();
- //The cycle wich track down windows' destruction
- while (!shell.isDisposed())
- {
- if (!display.readAndDispatch())
- {
- display.sleep();
- }
- }
- display.dispose();
- }
- //Create main window's content
- private void createContent(Shell shell)
- {
- //Vertical FillLayout
- FillLayout fillLayout = new FillLayout();
- fillLayout.type = SWT.VERTICAL;
- fillLayout.marginHeight = fillLayout.marginWidth = fillLayout.spacing = 10;
- //Text input field
- text = new Text(shell, SWT.NULL);
- text.setData("Enter smth: ");
- //Radiobutton grouping
- Group radiobuttonsGroup = new Group(shell, SWT.NONE);
- radiobuttonsGroup.setLayout(new FillLayout(SWT.VERTICAL));
- radiobuttonsGroup.setText("Choices");
- //A procedural generation of radiobuttons
- radiobuttons = new Button[radiobuttonsQuantity];
- for (int currRadiobuttonNumb = 0; currRadiobuttonNumb < radiobuttonsQuantity; currRadiobuttonNumb++)
- {
- radiobuttons[currRadiobuttonNumb] = new Button(radiobuttonsGroup, SWT.RADIO);
- radiobuttons[currRadiobuttonNumb].setSelection(false);
- radiobuttons[currRadiobuttonNumb].setText("RB " + Integer.toString(currRadiobuttonNumb + 1));
- }
- //Button
- button = new Button(shell, SWT.PUSH);
- button.setBounds(40, 50, 50, 20);
- button.setText("Click Me");
- //???
- button.addSelectionListener(new SelectionListener()
- {
- @Override
- public void widgetSelected(SelectionEvent event)
- {
- radiobuttonSelector(text.getText());
- }
- @Override
- public void widgetDefaultSelected(SelectionEvent event)
- {
- text.setText("No problem");
- }
- });
- shell.setLayout(fillLayout);
- }
- //Select inputted radiobutton
- public void radiobuttonSelector(String text)
- {
- boolean isFound = false;
- for (int currRadiobuttonNumb = 0; currRadiobuttonNumb < radiobuttonsQuantity; currRadiobuttonNumb++)
- {
- if (text.equals(radiobuttons[currRadiobuttonNumb].getText()))
- {
- radiobuttons[currRadiobuttonNumb].setSelection(true);
- isFound = true;
- }
- }
- if (!isFound)
- {
- new ErrorWindow(shell).run();
- }
- }
- public static void main(String[] args)
- {
- new MainWindow().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement