This discussion is archived
2 Replies Latest reply: Aug 18, 2011 5:44 AM by DarrylBurke RSS

Java AWT Form

883128 Newbie
Currently Being Moderated
Hi everybody,
I'm developing the first Applets, I made a window extending Frame class and I used KeyListener to write on it from keyboard.
I'd like to have a method that resize the text creating a new paragraph everytime the text exceed the width of the window, such
a common Text Editor.

Do you know such a method or how I can realize one to accomplish this task?

Thanks in advance,
Paolo F.C.

P.S. here is my code :
*******************************************
package com.testapplet;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Window extends Frame
{
     
     public String testo = "";
     public Ascoltatore asc;
     public AscoltaTastiera asc_tast;
     
     public static void main(String argv[])
     {
          Window wind = new Window();
     }
     
     Window()
     {
          setBounds(0,0,800,600);
          setTitle(getClass().getName());
          setVisible(true);
         setBackground(Color.white);
         setForeground(Color.black);
          asc = new Ascoltatore();
          asc_tast = new AscoltaTastiera(this);
          addWindowListener(asc);
          addKeyListener(asc_tast);
     }
     
     public void paint(Graphics g)
     {
          testo = asc_tast.testo;
          g.drawString(testo, 10, 22+14);
     }
}

*******************************************
package com.testapplet;

import java.awt.event.*;

public class Ascoltatore extends WindowAdapter
{
     public void windowClosed (WindowEvent evt)
     {
          System.out.println(evt);
          System.exit(0);
     }
     public void windowClosing (WindowEvent evt)
     {
          evt.getWindow().dispose();
          System.out.println(evt);
     }
     
}

*********************************************
package com.testapplet;

import java.awt.*;
import java.awt.event.*;

public class AscoltaTastiera implements KeyListener
{
     public String testo = "";
     public Frame parent;
     public AscoltaTastiera(Frame frame)
     {
          this.parent = frame;
     }
               public void keyPressed(KeyEvent e)
               {}
               public void keyReleased(KeyEvent e)
               {}
               public void keyTyped(KeyEvent e)
               {
                    testo += e.getKeyChar();
                    if (testo != null)
                   parent.repaint();
               }
          }
Edited by: 880125 on 18-ago-2011 5.49
  • 1. Re: Java AWT Form
    Kayaman Guru
    Currently Being Moderated
    Some hints:
    You might want to use Swing instead of AWT, since Swing is newer and has more and easier to use components.

    The g.drawString() method will not wrap the text, that's why there are ready made components for that. I'm not sure about AWT (well, at least the TextArea), but with Swing you have components like JTextPane.
  • 2. Re: Java AWT Form
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Moderator action: Moved from Java Programming.

    Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

    Then edit your post and format the code correctly.

    db

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points