Graphical User Interfaces > Example Graphical User Interface > Setting Up Inputs From the GUI to the Model

Setting Up Inputs From the GUI to the Model
To add some more dynamics to the application, add a method of inputting parameters to the model and create a Solve button.
The input data is provided by fields in the right side of the window.
Add these fields to the top of the class definition of the BeamModelDemo class:
JTextField editH;
JTextField editb;
JTextField edittf;
JTextField editr;
JTextField edittw;
A method leftPanel is created that sets up the various components.
private JPanel leftPanel() {
  MigLayout layout = new MigLayout("wrap 2");
  JPanel panel = new JPanel(layout);
 
  JLabel label = new JLabel("H:");
  panel.add(label);
  editH = new JTextField(16);
  editH.setText("160");
  panel.add(editH);
  
  label = new JLabel("b:");
  panel.add(label);
  editb = new JTextField(16);
  editb.setText("145");
  panel.add(editb);
 
  label = new JLabel("tf:");
  panel.add(label);
  edittf = new JTextField(16);
  edittf.setText("22");
  panel.add(edittf);
 
  label = new JLabel("tw:");
  panel.add(label);
  edittw = new JTextField(16);
  edittw.setText("13");
  panel.add(edittw);
 
  label = new JLabel("r:");
  panel.add(label);
  editr = new JTextField(16);
  editr.setText("12");
  panel.add(editr, "wrap 10px");
 
  JButton solveButton = new JButton("Solve");
  panel.add(solveButton, "span, wrap 10px");
 
  solveButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    solve();
  }
  });
 
  return panel;
}
Add a call to leftPanel in the start method just before the definition of the graphicsPanel variable. Also comment out the call to the solve method at the end of the start method. Solving is handled manually by clicking the Solve button.
JPanel panel = leftPanel();
mainPanel.add(panel, BorderLayout.LINE_START);
Run the application by, for example, pressing Ctrl+F11
It is now possible to re-solve the model by pressing the Solve button, but the fields still needs to be hooked into the model.
1
Navigate to the solve method in the BeamModelDemo.java file. Add the following lines to the top of the method:
model.param().set("H", editH.getText()+"[mm]");
model.param().set("b", editb.getText()+"[mm]");
model.param().set("tw", edittw.getText()+"[mm]");
model.param().set("tf", edittf.getText()+"[mm]");
model.param().set("r", editr.getText()+"[mm]");
2
3
Try to change the H parameter to, for example, 200 and press the Solve button.
4