Replacing dots with spaces
When I give in for example a String called : This.is.a.test and press the convert button , the textfield becomes empty.
I used String function : replace all to replace the dots with spaces.
When I set the text of the textfield with a normal string(" ") it works but with a variable it doesnt work.
Thanks in advance
Heres my code :
Code Java:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class TestReplace {
private JFrame frame;
private JTextField inputString;
String s;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestReplace window = new TestReplace();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestReplace() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
inputString = new JTextField();
inputString.setBounds(67, 88, 307, 34);
frame.getContentPane().add(inputString);
inputString.setColumns(10);
JLabel lblGeefStringIn = new JLabel("String :");
lblGeefStringIn.setBounds(173, 55, 108, 14);
frame.getContentPane().add(lblGeefStringIn);
JButton btnNewButton = new JButton("Convert");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
s = inputString.getText();
s = s.replaceAll(".", " ");
inputString.setText(s);
}
});
btnNewButton.setBounds(162, 150, 108, 23);
frame.getContentPane().add(btnNewButton);
}
}
Re: Replacing dots with spaces
I have no idea what your problem is. You need to provide a better explanation. Don't reply "Just run my code and you will see what I mean". If you can explain it clearly then I don't need to waste my time copying your code, compiling it and running it.
Re: Replacing dots with spaces
Try resizing the GUI. If your text shows up after that then you have a painting issue.
Re: Replacing dots with spaces
Ahhh, I just realised. The replaceAll method uses a regular expression and the dot has a special meaning in regex. Try escaping the dot.
Re: Replacing dots with spaces
I didn't know that.
Anyway if you use replace instead of replaceAll it works .
If you need replaceAll you need to escape values.
Thanks for your help.