1 Attachment(s)
To create a text area using swing
In the attached file i have given the code to create a text area when a buton is clicked along with a scrollbar .in the output i can see the button but it does not work so i cannot see the text area over which it will be written "button clicked. I have used the concept of interface and inner class.please see the code and help me out.
Re: To create a text area using swing
I cant view .java files on the phone but i suspect you haven't used a validate(); call.. This kind of operation is not thread safe so I'm not surprised it doesent work. But here's a walkaround: use textarea.setPrefferedSize(new Dimension(1,1)); for the not viewable text area and then call the same method with the parameters 450,450(viewable text area), for example.. To add a scrollbar you just do add(new JScrollPane(textArea));
Re: To create a text area using swing
Re: To create a text area using swing
it is still not working please help me.
Re: To create a text area using swing
Re: To create a text area using swing
try this
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JScrollPane.*;
import java.awt.event.ActionListener.*;
import javax.swing.JTextArea.*;
import javax.swing.*;
public class TextArea implements ActionListener
{
JTextArea text;
public static void main(String [] args)
{
TextArea gui=new TextArea();
gui.go();
}
public void go(){
JFrame frame;
frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("JUST CLICK IT");
button.addActionListener(this);
text=new JTextArea();
text.setLineWrap(true);
text.setPrefferedSize(new Dimension(1,1));
//pay attention here.. Is panel where "text" should be added?
panel.add(new JScrollPane(text));
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.SOUTH, button);
JScrollPane scroller=new JScrollPane();
panel.add(scroller);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
frame.setSize(350,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ev){
text.setPrefferedSize(new Dimension(350,350));
text.append("button clicked\n");
}
}
is this the way you did it?
also..you forgot to add "text" to the panel
Re: To create a text area using swing