I have written the below code in Java to check for no entry by a user in JTextArea. Only JTextArea.getText().equals("") works while others do not. It would be of great help in case you can explain why other other checks don't work.
Thanks
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestJTextArea{
private JFrame frame;
private JPanel panel;
private JLabel textLabel;
private JTextArea textArea;
private JButton submitButton;
public TestJTextArea(){
frame = new JFrame("Test JTextArea");
panel = new JPanel();
textLabel = new JLabel("Enter your Text below");
textArea = new JTextArea(20,20);
submitButton = new JButton("Submit");
setupGUI();
}
private void setupGUI(){
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
textArea.setFont(new Font("sansserif", Font.BOLD, 24));
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.add(textLabel);
panel.add(scrollPane);
panel.add(submitButton);
submitButton.addActionListener(new SubmitButtonListener());
frame.setSize(200,200);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private class SubmitButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Check for equals("") works. So the below code throws True:
if(textArea.getText().equals("")){
JOptionPane.showMessageDialog(null,"No Text test True for equals()","Check equality through equals()",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"No Text test False for equals()","Check equality through equals()",JOptionPane.WARNING_MESSAGE);
The below code throws False:
if(textArea.getText() == ""){
JOptionPane.showMessageDialog(null,"No Text test True for ==","Check equality through ==",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"No Text test False for ==","Check equality through ==",JOptionPane.WARNING_MESSAGE);
The below code throws False:
if(textArea.getText() == null){
JOptionPane.showMessageDialog(null,"No Text test True for null","Check equality through null",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"No Text test False for null","Check equality through null",JOptionPane.WARNING_MESSAGE);
All the below Codes throw False:
if(textArea.getText().toString() == ""){
JOptionPane.showMessageDialog(null,"No Text test True for toString()","Check equality through toString()",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"No Text test False for toString()","Check equality through toString()",JOptionPane.WARNING_MESSAGE);
if(textArea.getText().toString() == null){
JOptionPane.showMessageDialog(null,"No Text test True for toString() = null","Check equality through toString() = null",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"No Text test False for toString() = null","Check equality through toString() = null",JOptionPane.WARNING_MESSAGE);