Results 1 to 4 of 4
  1. #1
    danielparry is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default text editor problem

    Hi I need to create a small applet that reads in a script and then displays it on the applet window. I also need every "l" to be red and every "y" to be green. So far I have the following code (below) but when I run this code only a button appears and no text entry box. I understand that I have to use loops to be able to create the specific coloured letters but I have never done this before so any help would be greatly appreciated.

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    import javax.swing.JTextField;

    public class textfield extends Applet implements ActionListener
    {
    Button okButton;
    String text1;
    JTextField textField = new JTextField(30);
    public void paint(Graphics g)
    {

    g.setFont (new Font ("Helvetica", Font.BOLD, 20));
    g.setColor (Color.black);
    g.drawString(text1,20,100);

    }
    public void init()
    {
    setLayout(new FlowLayout());
    okButton = new Button("Enter Text");
    text1 = textField.getText();
    add(okButton);
    okButton.addActionListener(this);
    }
    public void actionPerformed(ActionEvent evt)

    {
    if (evt.getSource() == okButton)
    repaint();

    }

    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Use str.substring or charat to loop through the input and change to font color of the correct itema, are wanting the colors to switch after the ok button is hit? If so have the button listener set a string object equal to the field, then loop through the string and change the I and y's then set the textfield with the modified string.

  3. #3
    danielparry is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Use str.substring or charat to loop through the input and change to font color of the correct itema, are wanting the colors to switch after the ok button is hit? If so have the button listener set a string object equal to the field, then loop through the string and change the I and y's then set the textfield with the modified string.
    Hi, yes I need to type into the text editor box, which currently wont display as there is some sort of error which I dont know? Thank you for that answer but I am new to java and dont know how to do loops. Dont suppose you could give me a short example of some kind if thats not too much hassle? Sorry to be a pain.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I can certainly help you out, i will give you an example program to run which does extremely simple string manipulation.
    Java Code:
    public class MyClass{
      String s;
      MyClass(){
        s = "hello";
      }
      public static void main(String[] args){
        MyClass mc = new MyClass();
        for(int j = 0; j < mc.s.length(); j++){
          System.out.println(mc.s.substring(j, j + 1));
        }
      }
    }
    this just shows you how to split a string. Your program will be more complex as it will have to perform some tests and actually change values. Look up stringbuilder as well

Similar Threads

  1. text editor does not open file
    By globo in forum Advanced Java
    Replies: 2
    Last Post: 11-21-2010, 07:39 PM
  2. Help for a Text Editor.
    By necroninja in forum Advanced Java
    Replies: 0
    Last Post: 08-01-2009, 05:21 PM
  3. SWT Text Editor Demo
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:35 PM
  4. SWT Text Editor
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:43 PM
  5. Problem in Text Editor
    By mustahsan4u in forum New To Java
    Replies: 2
    Last Post: 03-26-2008, 02:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •