This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

Java AWT Form

883128
883128 Member Posts: 1
edited Aug 18, 2011 8:50AM in Abstract Window Toolkit (AWT)
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

Answers

  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    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.
  • darrylburke
    darrylburke Member Posts: 18,007
    edited Aug 18, 2011 8:44AM
    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
This discussion has been closed.