开发者

How to process forms in Java?

Using Swing in Java I wrote a form containing radio buttons, text fields and so on. In the very end I have a "Submit" button.

Now I want to "s开发者_Python百科end" the information given by the user to the program. How do I do that? Is there a good tutorial about that?

Is it kind of similar to PHP? (I am asking just because I know how to do it in PHP. To avoid confusions I probably need to mention that I do NOT program a web application).


Processing data in Swing is way different from the typical web REQUEST/RESPONSE paradigm.

To Take something you may know, it's more in the fashion of Javascript actions in an HTML page : each time user performs an operation, one or more events are sent, and the application developper can update application content according to it.

In your case, if you register an ActionListener to the button, it will be called each time button is clicked. You'll then have the possibility to perform any operation you want.

But that's not all !

Each time a component is keyboard focused, or receives the mouse, events are sent, as well as when a key is stroked or when widget's model is updated.

I would really suggest you to read documents such as Swing tutorial (which dives in greater details than I could do in 1 month).


Not completely sure what you mean by "send to the program". You are in the program so I assume that you have a dialog that renders this form? Just pass the dialog the object that you want to use to store the data. For example, your dialog's constructor can take an argument.

public class MyDialog extends JPanel {

  private UserInfo userInfo;

  private JTextField name;

  /**
   * The main area of the dialog.
   */
  protected JPanel panel;

  public MyDialog(UserInfo userInfo) {
    this.userInfo = userInfo;
  }

  public showDialog() {

    // Some code to create the form which it looks like you already know how to do

    // Create a name field
    JLabel nameLabel = new JLabel("Name:");
    panel.add( nameLabel );

    JButton submit = new JButton("Submit");
    submit.addActionListener( new ActionListener() {
      public void actionPerformed (ActionEvent event)
      {
        this.userInfo.setName(name.getText().trim());
      } } );
      panel.add( submit );      
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜