Check when an user has not entered anything in JTextArea [duplicate]












1















This question already has an answer here:




  • How do I compare strings in Java?

    23 answers




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);









share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 10:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • See : stackoverflow.com/questions/513832/…
    – Arnaud
    Nov 23 '18 at 7:30
















1















This question already has an answer here:




  • How do I compare strings in Java?

    23 answers




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);









share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 10:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • See : stackoverflow.com/questions/513832/…
    – Arnaud
    Nov 23 '18 at 7:30














1












1








1








This question already has an answer here:




  • How do I compare strings in Java?

    23 answers




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);









share|improve this question














This question already has an answer here:




  • How do I compare strings in Java?

    23 answers




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);




This question already has an answer here:




  • How do I compare strings in Java?

    23 answers








java equals jtextarea






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 7:15









Learncoholic

61




61




marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 10:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 10:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • See : stackoverflow.com/questions/513832/…
    – Arnaud
    Nov 23 '18 at 7:30


















  • See : stackoverflow.com/questions/513832/…
    – Arnaud
    Nov 23 '18 at 7:30
















See : stackoverflow.com/questions/513832/…
– Arnaud
Nov 23 '18 at 7:30




See : stackoverflow.com/questions/513832/…
– Arnaud
Nov 23 '18 at 7:30












1 Answer
1






active

oldest

votes


















0














Comparing String with == operator will compare references, that means it will work only if it is same String instance. To check if values of the Strings are the same, you should always use equals method.



Good approach is to use StringUtils methods IsEmpty/IsBlank - checks if a String contains text https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Comparing String with == operator will compare references, that means it will work only if it is same String instance. To check if values of the Strings are the same, you should always use equals method.



    Good approach is to use StringUtils methods IsEmpty/IsBlank - checks if a String contains text https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html






    share|improve this answer


























      0














      Comparing String with == operator will compare references, that means it will work only if it is same String instance. To check if values of the Strings are the same, you should always use equals method.



      Good approach is to use StringUtils methods IsEmpty/IsBlank - checks if a String contains text https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html






      share|improve this answer
























        0












        0








        0






        Comparing String with == operator will compare references, that means it will work only if it is same String instance. To check if values of the Strings are the same, you should always use equals method.



        Good approach is to use StringUtils methods IsEmpty/IsBlank - checks if a String contains text https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html






        share|improve this answer












        Comparing String with == operator will compare references, that means it will work only if it is same String instance. To check if values of the Strings are the same, you should always use equals method.



        Good approach is to use StringUtils methods IsEmpty/IsBlank - checks if a String contains text https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 7:34









        Centos

        19019




        19019















            Popular posts from this blog

            Trompette piccolo

            How do I get these specific pathlines to nodes?

            What visual should I use to simply compare current year value vs last year in Power BI desktop