Hi I am creating a simple Java-Server chat and I simply cannot get swing to play nice with long strings.
.
I don't want the horizontal scroll bar and I want the word to break when needed and flow to another line.
The code I have used to create the JTextPane is:
super("Message Server");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
getContentPane().add(userText, BorderLayout.SOUTH);
chatWindow = new JTextPane();
JScrollPane scrollPane = new JScrollPane(chatWindow);
getContentPane().add(scrollPane);
setSize(300, 450); //Sets the window size
setVisible(true);
chatWindow.setEditable(false);
Instead of using JTextPane
you can use JTextArea
and corresponding wrapping related methods setLineWrap()
and setWrapStyleWord()
to achieve what you want.
Yes you can set Font with JTextArea
Eg.
JTextArea txtArea = new JTextArea();
Font font = new Font("Verdana", Font.BOLD, 12);
txtArea.setFont(font);
txtArea.setForeground(Color.BLUE);
txtArea.setText("Hellow World!");