• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with JTextField

 
wasif akbar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was using a TextField component to serve the function of a status bar - to display messages to the user. No problems. Then I modified the TextField to a JTextfield, everything else is the same, and now I am having problems with displaying messages. The last message always displays, but none of the others previous to it do. It seems like some sort of a problem with repainting. Are there any known issues with JTextFields when it comes to repainting. This is basically the method that I am using. sField is the JTextField component.
public void setStatusBar(String text)
{
sField.setText(text);
}
I have a bunch of these statements in my main class. None of but the last one get displayed when using a JTextField.
myParent.setStatusBar("Connected to " + dbInstance + ".");
Thanks for your assistance.
Wasif Akbar
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't mix Swing components and AWT components. Any reason why you aren't using JLabel instead of JTextField?
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to display more than one String in the JTextField or TextField. You will always display the last one. So think about the display order at first. Please try this example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame1 extends JFrame {
JLabel jLabel1 = new JLabel("name:");
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel("password:");
JTextField jTextField2 = new JTextField();
JTextField jTextField = new JTextField();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public Frame1() {
super ("Test the display order");
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
jPanel1.setLayout(new GridLayout(3, 2));
jButton1.setText("Order1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("Order2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jPanel1.add(jLabel1);
jPanel1.add(jTextField1);
jPanel1.add(jLabel2);
jPanel1.add(jTextField2);
jPanel1.add(jButton1, null);
jPanel1.add(jButton2, null);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jTextField, BorderLayout.SOUTH);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
}
public static void main (String [] args) {
JFrame jFrame = new Frame1();
jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jFrame.setSize(300, 200);
jFrame.validate();
jFrame.setVisible(true);
}
void jButton1_actionPerformed(ActionEvent e) {
String name = jTextField1.getText();
if (name.length()==0)
jTextField.setText("please enter your name");
String password = jTextField2.getText();
if (password.length()==0)
jTextField.setText("Please enter your password");
}
void jButton2_actionPerformed(ActionEvent e) {
String name = jTextField1.getText();
if (name.length()==0)
jTextField.setText("please enter your name");
else {
String password = jTextField2.getText();
if (password.length()==0)
jTextField.setText("Please enter your password");
}
}
}
Good luck!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!