FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   TextField and TextArea
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: TextField and TextArea  (Read 7317 times)
michael05

WWW
TextField and TextArea
« on: May 5th, 2004, 2:00am »

hi
 
i try to add AWT textAreas and textFields to processing. it works very easy. but i did not find a chance to arrange them on the screen. they just pop centered in the middle of the screen.
 
there are some hints in the documentation but non of them works in this context.
 
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/TextField.html
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/TextArea.html
 
and here is the code:
 
//
TextField inputLine = new TextField("inputLine", 15);
TextArea inputArea = new TextArea("inputArea", 5, 20);
 
void setup() {
  size(640, 480);
 
  //setBounds(0,0,100,100);
  add(inputLine);
  add(inputArea);
}
 
void mousePressed()
{
  String iLine = inputLine.getText();
  String iArea = inputArea.getText();
  println("inputLine: " + iLine);
  println("inputArea: " + iArea);
}
 

같같같같같같같같같같
http://www.m05.de
Vair


Re: TextField and TextArea
« Reply #1 on: Nov 18th, 2004, 2:13pm »

I know this post is several months old and you've probably either solved or long since given up on this method but reading this post set me off trying to figure it out. I was half successful.
 
The answer is to use a LayoutManager - http://java.sun.com/developer/technicalArticles/GUI/AWTLayoutMgr/
 
The BorderLayout manager almost works but (at least in my copy of processing) elements get pushed out of the bottom of the window and being unable to control their height meant that I coudn't get it to work satisfactorily.
 
LayoutManagers are a hideously complicated way to lay out elements but you are supposed to be able to create a container, set its LayoutManager to null and then use absolute positioning. Losing relative positioning isn't much of a big deal when you're working with a non-resizable window anyway. Only, it didn't work either. The only way I could get it to work (after several hours) was to make to use a dummy container (you'll see what I mean when you look at the code). I have absolutely no idea why it works - or why it doesn't work without the dummy.
 
Hopefully someone with a good grasp of these things will be able to say why, and how to do it the right way.
 
I haven't yet figured out how to get events out these text fields so if anyone has any clues about that... let me know.
 
Here's the code - it has two fields, one for typed input and one for imaginary output.
 
Code:
void setup(){
  size(640,480);
   
  Panel dummyPanel = new Panel(); // voodoo
  Panel outputPanel = new Panel(); // holder for output display
  Panel inputPanel = new Panel(); // holder for input field
   
  // give all the containers a layout
  setLayout(new BorderLayout()); // main container (window?)
  outputPanel.setLayout(new BorderLayout());
  inputPanel.setLayout(new BorderLayout());
   
  // add the containers to the main container
  add(outputPanel);
  add(inputPanel);
   
  int borderSize = 10; // for calculating size of fields
  int inputHeight = 20; // height of input field
   
  // Now we can set the bounds of the text fields // left, top, width, height
  outputPanel.setBounds(borderSize,borderSize,width-borderSize*2,height-(i nputHeight+borderSize*3));
  inputPanel.setBounds(borderSize,height-borderSize-inputHeight,width-bord erSize*2,inputHeight);
   
  // Don't know why this works but it does.
  // The dummy is held by the main container and doesn't contain anything
  // yet without it, the input field inexplicably expands to contain the output field
  // (try commenting it out).
  // It also needs to be placed in the center.
  dummyPanel.setBackground(new Color(51,102,153));
  add(dummyPanel, BorderLayout.CENTER);
   
  // make the text boxes
  TextArea output = new TextArea("output");
  TextField input = new TextField("input");
   
  // make output read only and color it back to white
  output.setEditable(false);
  output.setBackground(Color.white);
   
  // add text boxes to their containers
  outputPanel.add(output);
  inputPanel.add(input);
}
 
Vair


Re: TextField and TextArea
« Reply #2 on: Nov 19th, 2004, 2:43am »

Figured out the getting data from the input field bit - very easy.
 
(After moving the input and output declarations outside of setup())
 
Code:

 
public boolean action(Event e, Object arg) {
  Object target = e.target;
 
  if (target == input){
    output.setText(output.getText() + "\n" + (String)arg);
    input.setText("");
    return true;
  }
  return false;
}
 
cello

marcello3d WWW
Re: TextField and TextArea
« Reply #3 on: Nov 19th, 2004, 6:21am »

Keep in mind that's using the deprecated java 1.0 event system... and that's somewhat discouraged (really should avoid it if you can).
 
Granted, the "proper" way to do it is probably over most processing users' heads.  Essentially you need to make an ActionListener class (or possibly even a TextListener class in this case, if you want to do more advanced stuff), define the appropriate functions, and then do input.addTextListener(yourclass), or whatever.
 
Hah, again, that's probably really confusing to anyone not familiar with Java interfaces and stuff.
 
Marcello
 
Vair


Re: TextField and TextArea
« Reply #4 on: Nov 19th, 2004, 7:34am »

Well I'll give it a shot.
 
Vair


Re: TextField and TextArea
« Reply #5 on: Nov 19th, 2004, 8:01am »

Got it:
 
Code:

  ...
  input.addActionListener(new MyActionListener());
}
 
class MyActionListener implements ActionListener{
  public void actionPerformed(ActionEvent e) {
   output.append(input.getText() + "\n");
   input.setText("");
    }
}

 
Should I be worried by the fact that the AWT documentation says not to use AWT?
 
cello

marcello3d WWW
Re: TextField and TextArea
« Reply #6 on: Nov 19th, 2004, 5:17pm »

Not really, considering the alternatives are:
a) use Swing, which is only available on Java 1.2 and later, basically making it useless for applets you want to run everywhere.  Plus Swing is kinda chunky and only really recommended for full blown applications.
b) use another gui set, such as SWT, which requires native code to be installed in the user's computer.  It's fine if you're doing a standalone program, but again, useless for applets
c) write your own gui widget(s).  which is not actually *that* difficult, though an advanced text widget will take some work to get right.  To do this you would write a class that extends from java.awt.Component,   implements mouse/keyboard input, and uses paint(Graphics g) to display itself.  example of custom widgets in an applet
 
The reason they don't recommend AWT widgets such as TextField/TextArea is because they use native "heavyweight" components which are not guaranteed to look the same on every operating system.  There are also problems with mixing heavyweight and lightweight components (swing and other java.awt.Component based custom guis are lightweight).
 
But as you can see, there's not much choice.  Ideally one would write their own widget, but for a textfield, handling stuff like caret and selections is a pain.  Plus you won't be able to access the clipboard to allow users to copy/paste from the textarea (not sure how much of a problem that is, but worth noting).
 
Marcello
 
arielm

WWW
Re: TextField and TextArea
« Reply #7 on: Nov 19th, 2004, 5:55pm »

very nice set of custom widgets cello!
 
i'm also designing custom widgets (when i have lots of free-time): i think that beyond solving compatibility and uniformity issues it's also an excellent way to explore new directions...
 

Ariel Malka | www.chronotext.org
Vair


Re: TextField and TextArea
« Reply #8 on: Nov 20th, 2004, 7:44am »

Very nice
 
Sjeiti

WWW
Re: TextField and TextArea
« Reply #9 on: Mar 17th, 2005, 10:11am »

I've been fiddling around with that panel-script above and I think I found out what it was with that weird dummy panel. It's not the dummy panel, what you need an overal setLayout (the one after the loop).
 
Paste this Code:
void setup(){
  size(640,480);
  line(0,0,width,height);
   
  int iMrg = 30;
  int iHgt = 20;
  for (int i=0;i<5;i++) {
    Panel inputPanel = new Panel();
    inputPanel.setLayout(new BorderLayout());
    add(inputPanel);
    inputPanel.setBounds(iMrg,iMrg+iHgt*i, 150,iHgt);
    TextField input = new TextField("jaja "+i);
    inputPanel.add(input);
  }
  setLayout(new BorderLayout());
}

 
grtz
 

http://www.sjeiti.com/
Pages: 1 

« Previous topic | Next topic »