Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Why cant I see my JTextArea?

  Asked By: John    Date: Jul 11    Category: Java    Views: 1183
  

Everything runs fine but I dont see the text I set in my JTextArea.

Any ideas?

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


public class Mywindow {

public static void main(String[] args) {

CenteredFrame frame = new CenteredFrame();
GPanelWithText GPanel = new GPanelWithText();
frame.getContentPane().add(GPanel);
//frame.setContentPane(GPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

}
}
class CenteredFrame extends JFrame
{
public CenteredFrame()
{
//get screen dimensions


Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;

// center frame in screen


setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);

// set frame icon and title


Image img = kit.getImage("icon.gif");
setIconImage(img);
setTitle("CenteredFrame");
}
}

class GPanelWithText extends JScrollPane {

public GPanelWithText () {

JTextArea text = new JTextArea("This is a test",5,30);
text.setLineWrap(true);
text.setWrapStyleWord(true);
text.setVisible(true);
this.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.setPreferredSize(new Dimension(250,250));
this.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("My
Panel"),
BorderFactory.createEmptyBorder
(5,5,5,5)),
this.getBorder()));

} //end constructor

} //ends GPanelWithText


Share: 

 

6 Answers Found

 
Answer #1    Answered By: Brian Ross     Answered On: Jul 11

Your GPanelWithText class  should not extend from JScrollPane, rather it should
extend from JTextArea and should be inserted inside of a JScrollPane, like so:

GPanelWithText gPanel = new GPanelWithText();
frame.getContentPane().add(new JScrollPane(gPanel));

You can still configure the scroll pane if you want, i.e.:

GPanelWithText gPanel = new GPanelWithText();
JScrollPane scrollPane = new JScrollPane(gPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollPane);

 
Answer #2    Answered By: Heidi Larson     Answered On: Jul 11

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


public class  Mywindow {

public static  void main(String[] args) {

CenteredFrame frame  = new CenteredFrame();
GPanelWithText GPanel = new GPanelWithText();
frame.getContentPane().add(GPanel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

}
}
class CenteredFrame extends JFrame
{
public CenteredFrame()
{

//get screen  dimensions


Toolkit kit  = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;

// center  frame in screen


setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);

// set  frame icon  and title


Image img  = kit.getImage("icon.gif");
setIconImage(img);
setTitle("CenteredFrame");
}
}

class GPanelWithText extends JTextArea {

public GPanelWithText () {

super("This is a test",5,30);
setLineWrap(true);
setWrapStyleWord(true);
this.setPreferredSize(new Dimension(250,250));
setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("My
Panel"),BorderFactory.createEmptyBorder(5,5,5,5)),this.getBorder()));

} //end constructor

} //ends GPanelWithText

 
Answer #3    Answered By: Nagina Mian     Answered On: Jul 11

To show a JTextArea, don't forget to add it to panel.
Add this 'this.add(text);' to 'public GPanelWithText()' -- last line.
Hope it solve your problem.

 
Answer #4    Answered By: Whitney Cruz     Answered On: Jul 11

The text  never gets added to the scrollpane. Use the JScrollPane
method addViewportView(Component) to load the scrollable component.
Usually the scrollable object is passed directly to the JScrollPane
constructor.

 
Answer #5    Answered By: Asir Hashmi     Answered On: Jul 11

My apologies I went back and tried what you said and it worked this
is what I did
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class  Mywindow {

public static  void main(String[] args) {

CenteredFrame frame  = new CenteredFrame();
JTextArea text  = new JTextArea("This is a test",5,30);
text.setLineWrap(true);
text.setWrapStyleWord(true);
GPanelWithText GPanel = new GPanelWithText(text);
frame.getContentPane().add(GPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);


}
}
class CenteredFrame extends JFrame
{
public CenteredFrame()
{
//get screen  dimensions


Toolkit kit  = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;

// center  frame in screen


setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);

// set  frame icon  and title


Image img  = kit.getImage("icon.gif");
setIconImage(img);
setTitle("CenteredFrame");
}
}

class GPanelWithText extends JScrollPane {

public GPanelWithText (JTextArea t) {

this.setViewportView(t);
this.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.setPreferredSize(new Dimension(250,250));
this.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("My
Panel"),
BorderFactory.createEmptyBorder
(5,5,5,5)),
this.getBorder()));

} //end constructor

} //ends GPanelWithText



 
Answer #6    Answered By: Saxon Anderson     Answered On: Jul 11

I tried that, got no errors but it still didnt
work for some reason.

Then a friend suggested that instead of having my object extend
JScrollPane that I have it extend JPanel.

Then I created a JScrollPane in that JPanel.

Cant see from the 1.4.1 docs why you HAVE TO have a JScrollPane on a
JPanel BEFORE you can see it.

Does anybody know?

 
Didn't find what you were looking for? Find more on Why cant I see my JTextArea? Or get search suggestion and latest updates.




Tagged: