Hi, I made a scrollable JTextArea that receives strings from a JTextField that I made. It works like a chat window so that when you enter text in the field, it goes straight to the text area. The UIWindow I made has the exact format as the Xith3DGuiTest.java TestWindow. When I add text to the JTextArea, I can’t scroll down and look at the text. When I place the cursor in the JTextArea, I can use the arrow keys to move down and up, but it screws up the character graphics. Please help!
Here is my code:
ackage oos.ui;
import java.awt.*;
import java.awt.event.*;
import oos.utils.*;
import com.xith3d.scenegraph.*;
import com.xith3d.render.*;
import com.xith3d.render.jogl.*;
import javax.vecmath.*;
import javax.swing.*;
import com.xith3d.image.*;
import com.xith3d.loaders.texture.*;
import com.xith3d.userinterface.*;
import com.xith3d.userinterface.UIWindowManager;
import com.xith3d.userinterface.UIWindow;
import com.xith3d.userinterface.UIEventAdapter;
/**
* @author Jonghwan Andrew Yang
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class TryUI{
UIWindowManager windowMgr;
View view;
String inputText = "";
public void init() throws Exception
{
// Prepare full-screen canvas adapted for current resolution
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
VirtualUniverse universe = new VirtualUniverse();
view = new View();
view.setBackClipDistance(100f);
view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION);
view.getTransform().lookAt(
new Vector3f(0, 0, 3),
new Vector3f(0, 0, 0),
new Vector3f(0, 1, 3));
Locale locale = new Locale();
universe.addLocale(locale);
universe.addView(view);
RenderPeer renderPeer = new RenderPeerImpl();
CanvasPeer canvasPeer =
renderPeer.makeCanvas(
null,
screenSize.width - 200,
screenSize.height - 200,
16,
true);
canvasPeer.getWindow().setLocation(100, 100);
Canvas3D canvas = new Canvas3D();
canvas.set3DPeer(canvasPeer);
view.addCanvas3D(canvas);
// construct a window manager for this canvas
windowMgr = new UIWindowManager(canvas);
TestWindow w = new TestWindow(1000,150);
windowMgr.addOverlay(w);
windowMgr.setPosition(w,100,690);
windowMgr.setVisible(w,true);
UIEventAdapter eventAdapter = new UIEventAdapter(windowMgr);
canvas.get3DPeer().getComponent().addKeyListener(eventAdapter);
canvas.get3DPeer().getComponent().addMouseListener(eventAdapter);
canvas.get3DPeer().getComponent().addMouseMotionListener(eventAdapter);
canvas.get3DPeer().getComponent().setFocusable(true);
//BranchGroup scene = new Scenegraph();
//locale.addBranchGraph(scene);
view.startView();
}
class TestWindow extends UIWindow{
public TestWindow(int width, int height) {
super(width, height, false, false );
setRoot(buildGUI(width,height));
}
private JComponent buildGUI (int width, int height) {
JSplitPane pane;
JPanel p = new JPanel();
p.setDoubleBuffered(true);
p.setSize(new Dimension(width,height));
p.setLocation(0,0);
p.setBackground(new Color(.7f,.7f,.7f,0.5f)); //the 4th parameter changes the transparency setting
p.setLayout(new GridLayout(3,1));
//scrollable text area that can't be edited and displays the text that chat people can read
final JTextArea textAreaScreen = new JTextArea();
textAreaScreen.setEditable(false);
textAreaScreen.setBackground(new Color(1f,0,0,.5f));
textAreaScreen.setLineWrap(true);
JScrollPane scrollPaneScreen = new JScrollPane(textAreaScreen);
p.add(scrollPaneScreen);
//this is the text field that allows someone to write something so that it can be put in the scrollable
//text area.
final JTextField textFieldInput = new JTextField();
textFieldInput.setBackground(new Color(1f,0,0,1f)); //the 4th parameter changes the transparency settings
textFieldInput.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
//this notices when 'enter' is pressed and the string in the text field should be sent to the
//text area for everyone to see when chatting
if(e.getKeyChar() == 10){
inputText = inputText + "\n" + textFieldInput.getText();
System.out.println(inputText);
textFieldInput.setText("");
textAreaScreen.setText("");
textAreaScreen.setText(inputText);
}
}
});
p.add(textFieldInput);
//adds an exit button to quit the program
JButton logoutButton = new JButton();
logoutButton.setPreferredSize(new Dimension(100,100));
logoutButton.setText("Logout");
logoutButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
p.add(logoutButton);
return p;
}
}
public static void main(String[] args)
{
System.out.println("Hit SPACE to toggle projection policy, or ESC to exit");
try
{
TryUI test = new TryUI();
test.init();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
