5 Replies - 626 Views - Last Post: 19 September 2009 - 11:58 AM Rate Topic: -----

#1 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

JTextArea wrapping issues

Posted 19 September 2009 - 11:30 AM

hey all, So i have a JTextArea to display some chat messages... Now the issue i'm having is if the user enters a really long sentence/paragraph then it will actually just expand the entire window to fit the text even if setWrap is set to true.. I'm wondering if there is any way to have the text wrap to a new line if the sentence length exceeds the length of the JTextArea?

Cheers
Is This A Good Question/Topic? 0
  • +

Replies To: JTextArea wrapping issues

#2 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: JTextArea wrapping issues

Posted 19 September 2009 - 11:45 AM

Really? When I enter long paragraphs into the JTextArea, my window never expands when I set both setLineWrap() and setWrapStyleWord() to true...
Or maybe you need to put a JScrollPane to the window?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JTextArea wrapping issues

Posted 19 September 2009 - 11:48 AM

What is the Layout of the JComponent holding this JtextArea ?
Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: JTextArea wrapping issues

Posted 19 September 2009 - 11:52 AM

View Postpbl, on 19 Sep, 2009 - 10:48 AM, said:

What is the Layout of the JComponent holding this JtextArea ?



well i have it in a couple layouts so i figure the code would probably be better to explain it...

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class gameGui 
{
	//components
	private JFrame mainWindow;
	private JPanel buttonsPanel;
	private JButton[][] gameButtons;
	
	//chat components
	private JTextArea chatArea;
	private JScrollPane chatScrollArea;
	private JTextField chatSendArea;
	private JButton chatSendButton;
	
	private JPanel chatPanel;
	private JPanel sendPanel;
	
	//game info components
	private JPanel rightPanel;	
	
	//values
	private final int WINDOW_HEIGHT = 515;
	private final int WINDOW_WIDTH = 800;
	
	private gameGui()
	{
		//instantiate all the elements for the Main Window
		mainWindow = new JFrame();
		mainwindow.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		mainwindow.setResizable(false);
		mainwindow.setDefaultCloseOperation(mainwindow.EXIT_ON_CLOSE);
		mainwindow.setTitle("Welcome to a Tic Tac Toe Battle!");
		mainwindow.setLocationRelativeTo(null);
		
		gameButtons = new JButton[3][3];
		buttonsPanel = new JPanel(new GridLayout(3,3));
		
		//instantiate the buttons and add them to the buttons panel
		for(int rows = 0; rows < 3; rows++)
		{
			for(int columns = 0; columns < 3; columns++)
			{
				gameButtons[rows][columns]= new JButton("");
				gameButtons[rows][columns].addActionListener(new boardButtonListener());
				buttonsPanel.add(gameButtons[rows][columns]);
			}
		}
		
		//chat area intantiations
		chatScrollArea = new JScrollPane();
		
		chatArea = new JTextArea(15,20);
		chatArea.setEditable(false);
		
		chatScrollArea.add(chatArea);
		
		chatSendArea = new JTextField(20);
		chatSendButton = new JButton("Send");
		chatSendButton.addActionListener(new sendChatButtonListener());
		
		sendPanel = new JPanel(new FlowLayout());
		sendPanel.add(chatSendArea);
		sendPanel.add(chatSendButton);
		
		chatPanel = new JPanel(new GridLayout(2,1));
		chatPanel.add(chatScrollArea);
		chatPanel.add(sendPanel);
		
		rightPanel = new JPanel(new BorderLayout());
		rightPanel.add(chatPanel, BorderLayout.SOUTH);
		
		mainwindow.setLayout(new BorderLayout());
		mainwindow.add(buttonsPanel, BorderLayout.CENTER);
		mainwindow.add(rightPanel, BorderLayout.EAST);
		
		
		mainwindow.setVisible(true);
	}
	
	public class sendChatButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{			
			chatArea.append("\n" + "Username: " +chatSendArea.getText());
			chatSendArea.setText("");
		}
	}
	
	public class boardButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{			
			for(int rows = 0; rows < 3; rows++)
			{
				for(int columns = 0; columns < 3; columns++)
				{
					if(e.getSource().equals(gameButtons[rows][columns]))
					{
						if(gameButtons[rows][columns].getText().equals("X")|| gameButtons[rows][columns].getText().equals("O"))
						{
							JOptionPane.showMessageDialog(mainWindow, "You cannot play where someone else already has...");
						}
						else
						{
							gameButtons[rows][columns].setText("X");
						}
					}
				}
			}
		}
	}
	
	public static void main(String[] args)
	{
		new gameGui();
	}
}



I just added it to a scroll pane and now i can't really even do much with it at all....
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JTextArea wrapping issues

Posted 19 September 2009 - 11:55 AM

replace

	 //chat area intantiations
		chatScrollArea = new JScrollPane();
		
		chatArea = new JTextArea(15,20);
		chatArea.setEditable(false);
		
		chatScrollArea.add(chatArea);
		


by
	 //chat area intantiations
	   chatArea = new JTextArea(15,20);
	   chatArea.setEditable(false);
  
	   chatScrollArea = new JScrollPane(chatArea);


Was This Post Helpful? 1
  • +
  • -

#6 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: JTextArea wrapping issues

Posted 19 September 2009 - 11:58 AM

View Postpbl, on 19 Sep, 2009 - 10:55 AM, said:

replace

	 //chat area intantiations
		chatScrollArea = new JScrollPane();
		
		chatArea = new JTextArea(15,20);
		chatArea.setEditable(false);
		
		chatScrollArea.add(chatArea);
		


by
	 //chat area intantiations
	   chatArea = new JTextArea(15,20);
	   chatArea.setEditable(false);
  
	   chatScrollArea = new JScrollPane(chatArea);




Right on thanks again pbl! haha don't know why i didn't catch that :P

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1