Making a JTextArea appear/disappear
I have:
JFrame frame;
JComponent screen;
JTextField chatbox;
Container pane = frame.getContentPane();
pane.add(screen,BorderLayout.CENTER);
pane.add(chatbox,BorderLayout.SOUTH);
The screen draws the graphics and I want the chatbox to pop up at the bottom whenever the user presses enter.
I originally call chatbox.setVisible(false) during the initialization. But, later when I call chatbox.setVisible(true) it does not show up. I dont have to call pack() on the frame every time right?
[540 byte] By [
Nethera] at [2007-10-3 10:39:05]

You would get a better response on the swing forum.
Aye, the way you are doing it, I think you would need to recall pack, as when you create it, it takes up no space, so it is allocated no space. And you want it to take up space, so pack would give it some.
I think I'd do it with JSplitPane, and setDividerLocation.
Or (if you don't want stuff to change positions), maybe have the JTextComponent on the Glass Panel (/higher z-order).
mlka at 2007-7-15 6:03:09 >

Ya, I would use a JSplitPane, but I dont want the user to be able to move it. What is a glass panel?
The only thing that I'm thinking is that you're not properly adding it to your frame when you initialize it. This works just fine for me (click the button to show/hide the jTextArea)
import javax.swing.*;
public class Test extends JFrame {
private JTextArea jta;
private JScrollPane jsp;
private JButton btn;
public Test() {
jta = new JTextArea(10, 50);
jsp = new JScrollPane(jta);
btn = new JButton("Click");
this.getContentPane().setLayout(new java.awt.FlowLayout());
this.getContentPane().add(jsp);
this.getContentPane().add(btn);
this.pack();
jta.setVisible(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (!jta.isVisible()) {
jta.setVisible(true);
} else {
jta.setVisible(false);
}
}
});
}
public static void main(String[] argv) { new Test().setVisible(true); }
}
A problem was that i called setVisible(false) before pack. Now it appears and disappears, but my screen component does not draw all the way to the bottom of the screen when the chatbox is not visible.
Place your jTextField on your screen component.
thats what I originally tried to do. But even when I dont ever call setVisible(false)
the textarea never shows.
I have this:
screen = new Screen();
Container pane = getContentPane();
pane.add(screen);
public class Screen{
public Screen(){
initChatbox()
}
public void paint(Graphics g){
//paints images that fill up the whole screen.
g.fillRect(0,0,getWidth(),getHeight())
}
private void initChatBox(){
chatBox = new JTextField();
add(chatBox)
}
}
use getContentPane().validate() ?
1) Swing related questions should be posted in the Swing forum.
2) Navy_Coder, showed you how to write an SSCCE. We can't help you based on a few lines of code.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
Alright here is some compilable code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SSCCE extends JComponent{
private JTextField field;
public SSCCE(){
setLayout(new BorderLayout());
field = new JTextField();
add(field,BorderLayout.SOUTH);
validate();
addMouseListener();
}
public void paint(Graphics g){
g.setColor(Color.blue);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.white);
g.drawString("This text should be visible only when the JTextField is not showing.",5,getHeight());
}
private void addMouseListener(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
field.setVisible(!field.isVisible());
repaint();
}
});
}
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.getContentPane().add(new SSCCE());
frame.pack();
frame.setSize(400,400);
frame.show();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
It looks really weird when it first starts and then when you click to make it invisible, that works, but it never becomes visible again.
Check out my response in this posting: http://forum.java.sun.com/thread.jspa?threadID=788923
Aha! Thats the problem. I switched public void paint(g) to public void paintComponent(g) and it works just like I want it to.Thanks!
> Ya, I would use a JSplitPane, but I dont want the user to be able to move it. create the splitPanesplitPane.setResizeWeight(...); //for location of the dividersplitPane.setEnabled(false);//can't move divider