0

I think im heading in the wrong direction. Im creating a notepad app. I have every method running perfectly except one - WordWrap

Its just a JTextarea inside a panel inside a frame.

I think i should be using a JScrollPane instead of a Textarea? Or aswell as it even?

How would i go about resizing the width of a textarea or am i correct in saying i need to insert a JScrollPane.

Edit

Ok so my attempt is gone wrong somehow. Text area doesnt work. Something possibly needs resizing.

public class TextEditor extends JFrame implements ActionListener{

JFrame textFrame = new JFrame();
JPanel textPanel = new JPanel();

JTextField textArea = new JTextField();
JScrollPane scroll = new JScrollPane(textArea);


JTextArea text = new JTextArea(24,33);

public TextEditor(String str){

    super(str);

    textFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    textFrame.add(textPanel);
    textPanel=(JPanel)getContentPane();
    textPanel.setLayout(new FlowLayout());
    textPanel.setBackground(Color.WHITE);



    // Create text Area


    textPanel.add(scroll);
    scroll.add(text);
    textPanel.setFont(textAreaFont);
    textArea.setFont(textAreaFont);
    text.setFont(textAreaFont);

}

public static void main(String args[])
{
    TextEditor notePad = new TextEditor("Notepad");
    notePad.setSize(500,500);
    notePad.setVisible(true);
    notePad.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
user237462
  • 387
  • 1
  • 5
  • 24
  • Yes, it would be common to display a `JTextArea` in a `JScrollPane`. Try it. If it does not work for you, get back to us with your best attempt. – Andrew Thompson Oct 28 '13 at 13:35
  • So JFrame > JPanel > JScrollPane > JTextArea? Correct? – user237462 Oct 28 '13 at 13:43
  • Yep, basically. The only way I'd differ from that simple logic is to *construct* the `JScrollPane` using the `JTextArea` as the argument (to save one code line). Also make sure to look at text area methods that have the words `LineWrap` and `WrapStyle` to get the text area behaving in a sensible and predictable way, and setting the rows and columns of the text area to suggest a size for it. – Andrew Thompson Oct 28 '13 at 13:51
  • Thanks. Ive edited Post with attempt thats going slightly wrong. – user237462 Oct 28 '13 at 14:02
  • For better help sooner, post an [SSCCE](http://sscce.org/). An SSCCE of this problem should take no more than 40 lines of code (as opposed to 118). – Andrew Thompson Oct 28 '13 at 14:04
  • Apologies i thought someone would want to copy paste into eclipse so i left it in. – user237462 Oct 28 '13 at 14:30
  • *"i thought someone would want to copy paste"* That **is** the idea of an SSCCE. But have a long think about that code and figure out which pparts of it are *really* needed to show this problem. I don't see any need for menus, buttons, action listeners.. That takes around 3/4 of that code out straight away. Note that the code of user2777005 is almost what I am talking about. It just needs to have the imports added, to make it one. – Andrew Thompson Oct 28 '13 at 14:44
  • Seems ok now. Any ideas why the text area in a JScrollPane isnt working? It looks like something needs resizing. Similar to when a JButton size isnt set. – user237462 Oct 28 '13 at 14:47
  • *"why the text area in a JScrollPane isnt working?"* Doesn't it? How does it fail? What is the SSCCE used in your tests? – Andrew Thompson Oct 28 '13 at 14:49

2 Answers2

1

Have a look at what I have tried to put together:

public class SO{
public static void main(String[] args) {

    JFrame f = new JFrame();
    JPanel p = new JPanel();

    JTextArea outputArea = new JTextArea();
    outputArea.setColumns(20);
    outputArea.setRows(20);  
    outputArea.setLineWrap(true); //Set line wrap
    outputArea.setWrapStyleWord(true); //set word wrap

    JScrollPane sp = new JScrollPane(outputArea); //Create new scroll pane with textarea inside
    p.add(sp); //add scrollPane to panel
    f.add(p); //Add panel to frame
    f.pack()
    f.setLocationRelativeTo(null); //frame location
    f.setVisible(true);
    }
}

The scroll pane is created using the textarea in the constructor, this seems to allow the scroll pane to 'contain' the JTextArea, adding scroll bars when the text the area contains exceeds the limits. Earlier when creating the JTextArea I set two lines of code to set a word wrap on it, this stops words seeping off the sides by pushing them onto the next line. Have a look and see if it can help with your project.

Good Luck!

Levenal
  • 3,796
  • 3
  • 24
  • 29
  • 1) `f.setSize(300, 400);` unnecessary, given the text area suggests a size. Simply call `pack()` at the end. 2) GUIs should be created on the EDT, even for a quick trow-away example like this. 3) Nice comments though, and overall, +1. – Andrew Thompson Oct 28 '13 at 14:48
  • @AndrewThompson Thanks for the tips, I am still just learning about GUIs and wasn't aware of the ability of pack(), and as for threads, they are well above my level at the moment :) – Levenal Oct 28 '13 at 14:55
  • *"as for threads, they are well above my level at the moment"* Well, I'd helpfully say 'See my example' ..except that I could not be bothered taking the code that far. (What a hypocrite!) So being lazy, I'll just link to 'an example we prepared at home' - in [this answer](http://stackoverflow.com/a/19637706/418556). The EDT related starting begins when it declares a runnable, and ends with `SwingUtilities.invokeLater(r);`. The code jammed in the `run()` method is the stuff that should be 'done on the EDT'. – Andrew Thompson Oct 28 '13 at 15:05
1

enter image description here

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

public class TextEditor extends JFrame {

JFrame textFrame = new JFrame();
JPanel textPanel = new JPanel();
JTextArea textArea = new JTextArea(10,25);

public TextEditor(String str){

    super(str);

    textFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // nicer
    add(textPanel);
    textPanel.setLayout(new GridLayout());
    textPanel.setBackground(Color.WHITE);

    // Create text Area
    JScrollPane scroll = new JScrollPane(textArea);
    textPanel.add(scroll);
}

public static void main(String args[])
{
    TextEditor notePad = new TextEditor("Notepad");
    notePad.setVisible(true);
    notePad.setDefaultCloseOperation(EXIT_ON_CLOSE);
    notePad.pack();
}
}

There were so many things wrong in that short code that I lost track of the changes. Two things I can recall are:

  • The code was quite confused about what was a JTextField and what was a JTextArea.
  • It added strange things to other strange things for no apparent reason.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433