Mike
Mike

Reputation: 83

CookSwing - Retrieving the string from a textfield

I'm using CookSwing (http://cookxml.yuanheng.org/cookswing/) to build a Java Swing UI, but the site doesn't have a lot of information. I'm stuck trying to get my Java class to retrieve the string from a textfield in the form (declared in XML). This should be easy to do, but nothing I've tried works. Does anyone have experience doing this? Here is the Java class:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import cookxml.cookswing.CookSwing;

public final class CookSwingForm
{
  // Listener for the Quit button
  public ActionListener exitAction = new ActionListener()
  {
    public void actionPerformed (ActionEvent e)
    {
      System.exit(0);
    }
  };

  public CookSwingForm()
  {
    CookSwing cookSwing = new CookSwing(this);
    cookSwing.render("sampleform.xml").setVisible(true);
  }

  public static void main(String[] args)
  {
    new CookSwingForm();
  }
}

Here is the XML file ("sampleform.xml"):

<frame title="Sample Form" size="300,70"
       defaultcloseoperation="EXIT_ON_CLOSE">
  <borderlayout>
    <constraint location="West">
       <textfield columns="20" />
    </constraint>
    <constraint location="East">
      <button text="Quit" actionlistener="exitAction" />
    </constraint>
  </borderlayout>
</frame>

I just need to have the Java class retrieve the string from the textfield that's declared in the XML file. Any help is greatly appreciated. Thanks!

Upvotes: 1

Views: 608

Answers (1)

ring bearer
ring bearer

Reputation: 20793

I think you should use the id attribute and then use that as the variable name for the text field.

The xml would look as : <textfield id="box1" />

Here is what you have to do:

JTextField txtField = (JTextField) cookSwing.getId("box1").object;
//now,set some text 
txtField.setText("Blah!");
//or get some text as you may wish

Also. Looks like the library is no longer being developed. That's a red flag right there.

Upvotes: 1

Related Questions