Results 1 to 7 of 7
- 11-16-2012, 09:57 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
setCaretPosition not working properly
I have a text area in this program that is supposed to let the user write a message, send the message, and then return the Caret back to the first position on the first line. When I type in the text area, no matter how long the string is, and I press enter it sends the message but it always returns the Caret one position lower than the beginning of the text area (the beginning of the next line). I tried using setCaretPosition(0) but it still returns the Caret in the wrong position. Please see below and let me know if you spot my mistake. (Line 290 is what I'm referring to.)
Java Code:import java.net.*; import java.io.*; import java.awt.event.*; public class Client extends javax.swing.JFrame implements Runnable { private Socket s; private DataOutputStream dout; private DataInputStream din; private String userName; private static String host; private static int port; private boolean isConnected = false; public static void main(String args[]) { /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Client().setVisible(true); } }); } public Client() { initComponents(); } private void processMessage(String message) { try { dout.writeUTF(message); } catch(IOException ie) { System.out.println(ie); } } public void run() { try { while(true) { String message = din.readUTF(); chatTextDisplay.append(message); } } catch(IOException ie) { System.out.println(ie); chatTextDisplay.append("!!!!!(Error sending message)!!!!!\n"); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { usernameLabel = new javax.swing.JLabel(); usernameText = new javax.swing.JTextField(); connectButton = new javax.swing.JButton(); disconnectButton = new javax.swing.JButton(); usersOnlineLabel = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); usersOnlineDisplay = new javax.swing.JTextArea(); jScrollPane2 = new javax.swing.JScrollPane(); chatTextDisplay = new javax.swing.JTextArea(); jScrollPane3 = new javax.swing.JScrollPane(); userChatInput = new javax.swing.JTextArea(); sendButton = new javax.swing.JButton(); hostLable = new javax.swing.JLabel(); hostText = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); portText = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); usernameLabel.setBackground(new java.awt.Color(255, 254, 254)); usernameLabel.setText("Username:"); connectButton.setText("Connect"); connectButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { connectButtonActionPerformed(evt); } }); disconnectButton.setText("Disconnect"); usersOnlineLabel.setText("Users Online"); usersOnlineLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); usersOnlineDisplay.setEditable(false); usersOnlineDisplay.setColumns(10); usersOnlineDisplay.setRows(5); jScrollPane1.setViewportView(usersOnlineDisplay); chatTextDisplay.setEditable(false); chatTextDisplay.setColumns(20); chatTextDisplay.setLineWrap(true); chatTextDisplay.setRows(5); chatTextDisplay.setWrapStyleWord(true); jScrollPane2.setViewportView(chatTextDisplay); userChatInput.setColumns(20); userChatInput.setLineWrap(true); userChatInput.setRows(5); userChatInput.setWrapStyleWord(true); userChatInput.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { userChatInputKeyPressed(evt); } }); jScrollPane3.setViewportView(userChatInput); sendButton.setText("Send"); sendButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sendButtonActionPerformed(evt); } }); hostLable.setText("Host:"); hostText.setText("localhost"); jLabel1.setText("Port:"); portText.setText("50000"); portText.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { portTextActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(20, 20, 20) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jScrollPane2) .add(layout.createSequentialGroup() .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 363, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(sendButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)) .add(layout.createSequentialGroup() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(usernameLabel) .add(hostLable)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(usernameText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE) .add(hostText)) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(connectButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 108, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(disconnectButton) .add(5, 5, 5)) .add(layout.createSequentialGroup() .add(24, 24, 24) .add(jLabel1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(portText))))) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 192, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(layout.createSequentialGroup() .add(56, 56, 56) .add(usersOnlineLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) .add(15, 15, 15)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(14, 14, 14) .add(usersOnlineLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jScrollPane1)) .add(layout.createSequentialGroup() .add(18, 18, 18) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(hostText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(hostLable, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jLabel1) .add(portText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(connectButton) .add(disconnectButton) .add(usernameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(usernameText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 278, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE) .add(sendButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addContainerGap(9, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(isConnected==false) { userName = usernameText.getText(); host = hostText.getText(); port = Integer.parseInt(portText.getText()); try { s = new Socket(host, port); chatTextDisplay.append("Connected to host: "+host+". Connected on port: "+port+"\n"); isConnected = true; din = new DataInputStream(s.getInputStream()); dout = new DataOutputStream(s.getOutputStream()); new Thread(this).start(); hostText.setEditable(false); portText.setEditable(false); usernameText.setEditable(false); } catch(IOException ie) { chatTextDisplay.append("Server is offline.\n"); isConnected = false; hostText.setEditable(true); portText.setEditable(true); usernameText.setEditable(true); } } } private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void portTextActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void userChatInputKeyPressed(java.awt.event.KeyEvent evt) { // TODO add your handling code here: int key = evt.getKeyCode(); if(key==KeyEvent.VK_ENTER) { String message = userChatInput.getText(); message = userName+": "+message+"\n"; if(message!="") { processMessage(message); userChatInput.setText(""); userChatInput.setCaretPosition(0); } } } // Variables declaration - do not modify private javax.swing.JTextArea chatTextDisplay; private javax.swing.JButton connectButton; private javax.swing.JButton disconnectButton; private javax.swing.JLabel hostLable; private javax.swing.JTextField hostText; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTextField portText; private javax.swing.JButton sendButton; private javax.swing.JTextArea userChatInput; private javax.swing.JLabel usernameLabel; private javax.swing.JTextField usernameText; private javax.swing.JTextArea usersOnlineDisplay; private javax.swing.JLabel usersOnlineLabel; // End of variables declaration }
- 11-16-2012, 11:08 PM #2
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: setCaretPosition not working properly
Gentle bump. :)
- 11-17-2012, 12:05 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: setCaretPosition not working properly
Please don't bump your threads - especially when it has been online for only a little over an hour. We are all volunteers, and showing impatience such as this is rude to those investing their free and valuable time. If you do feel the need to bump your thread after a given amount of time, there is usually a reason why no one has replied - you should do more to make your questions easier to answer - an SSCCE for example, for instance your code is not even compilable without 3rd party libraries (a GUI builder I presume - which, if you are a beginner I would not recommend using). Lastly,
If you wish to compare object value's, such as comparing if two String's are the same value, always use the equals() method.Java Code:if(message!="")
- 11-17-2012, 01:27 AM #4
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: setCaretPosition not working properly
I apologize for bumping the thread. I also did notice that if statement and had already made changes. Thank you for pointing it out.
- 11-17-2012, 01:59 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: setCaretPosition not working properly
Alrighty I found the solution. Apparently, when enter is pressed it creates a new line. In order to not register that ability we use consume(). That worked for me. Thought I'd post it here just in case someone was having the same problem:
Java Code:evt.consume();
- 11-17-2012, 09:10 AM #6
Re: setCaretPosition not working properly
The correct approach to intercept typed text is to use a DocumentFilter. For this specific use case, reassigning the Key Binding for the <Enter> key may also be viable.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-17-2012, 09:11 AM #7
Similar Threads
-
iterator not working properly
By arieltal in forum Advanced JavaReplies: 3Last Post: 09-29-2012, 01:01 PM -
If statement is not working properly
By Alpa in forum New To JavaReplies: 3Last Post: 02-04-2012, 08:40 PM -
NetBeans IDE not working properly
By farmer in forum NetBeansReplies: 2Last Post: 12-09-2011, 03:12 PM -
TCP/IP client not working properly
By Chrillz in forum NetworkingReplies: 2Last Post: 07-20-2011, 07:39 PM -
date is not working properly
By newnewgen in forum New To JavaReplies: 1Last Post: 10-12-2010, 09:04 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks