| Author |
Doubt in ternary operator
|
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
Dear All,
My requirement needs me to have two radio buttons & a textfield.
If the radio button "AnnotationEnabled" is selected then the textfield should display "Barchart annotation is possible" otherwise it should display it is not possible in the textfield.
When I try to use ternary operator to achieve this it displays not a statement error:
Complete Code:
Error Trace:
|
 |
Janardan Kelkar
Ranch Hand
Joined: May 05, 2008
Posts: 69
|
|
i have high chances of being wrong, but i doubt if we can do this
Try using the equals method on strings.
|
When the compiler's not happy, ain't nobody happy.
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
Thanks Janardhan for your opinions.
But anyhow the problem persists.
Error trace:
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
The expressions after the ? and : must evaluate to a value, not void. (JLS reference)
Note that it is not permitted for either the second or the third operand expression to be an invocation of a void method. In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear
So in order to use the ternary operator in your particular context you would need to do something like this:
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
Garrett Rowe wrote:The expressions after the ? and : must evaluate to a value, not void. (JLS reference)
Exactly; to be precise, they must be expressions and not statements.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
Thanks Garrett Rowe,
Now it's working!
My best Regards
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
When I tried to customize the JTextField according to the selected button, it's not working when I run the applet.
Eventhough the TextField is coming on the applet, nothing gets displayed.
Why is it so?
Regards
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1190
|
|
While comparing String values you should .equals or .equalsIgnoreCase method of String class, == operator for String compares references not values, so the following change in the code should solve your problem
your code
-------------
public void actionPerformed(ActionEvent ae)
{
//text.setText(ae.getActionCommand());
if(ae.getActionCommand()=="AnnotationEnabled")
{
text=new JTextField(25);
text.setText("Barchart Annotation is possible");
}
else
{
text=new JTextField(25);
text.setText("BarchartAnnotation not possible");
}
}
Changed code
-----------------
public void actionPerformed(ActionEvent ae)
{
//text.setText(ae.getActionCommand());
if(ae.getActionCommand().equals("AnnotationEnabled"))
{
text.setText("Barchart Annotation is possible");
}
else
{
text.setText("BarchartAnnotation not possible");
}
}
|
Swastik
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Actually you are instantiating the JTextPane newly in each case (if and else) and to which you are setting the text. But it IS NOT the same JTextPane you had added in your init() method.
Try hiding the line which you have set bold and run the program. It works fine. If you need to alter the size of the JTextPane, you might have to remove the earlier component and re-add this to the Pane I guess. Not very sure. Just check it out or other ranchers/bartenders might help in this scenario.
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
|
Yes, one another suggestion which has been repeatedly given is to use .equals() method when comparing the string contents and NOT the "==" operator which actually checks the references. Try adapting that as well.
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
If you need to alter the size of the JTextPane, you might have to remove the earlier component and re-add this to the Pane I guess.
I need to alter the size of the JTextPane for each case(if/else) with only one JTextPane object which i've initialized in the init() method.
Is it possible?
Regards
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1190
|
|
Resizing is possible
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code="AppletDemo" height=400 width=400>
</applet>
*/
public class AppletDemo extends JApplet implements ActionListener
{
JTextField text=null;
Container pane=getContentPane();
public void init()
{
text=new JTextField(25);
//pane.setLayout(new FlowLayout());
pane.setLayout(null);
JRadioButton buttonone=new JRadioButton("AnnotationEnabled");
buttonone.setBounds(50,50,200,30); //x,y,width,height
pane.add(buttonone);
buttonone.addActionListener(this);
JRadioButton buttontwo=new JRadioButton("AnnotationDisabled");
buttontwo.setBounds(260,50,200,30);
pane.add(buttontwo);
buttontwo.addActionListener(this);
//add textbox to the container panel
pane.add(text);
text.setBounds(50,90,100,30);
ButtonGroup buttongroup=new ButtonGroup();
buttongroup.add(buttonone);
buttongroup.add(buttontwo);
}
public void actionPerformed(ActionEvent ae)
{
//text.setText(ae.getActionCommand());
if(ae.getActionCommand()=="AnnotationEnabled")
{
//text=new JTextField(25);
//pane.setSize(300,200);
text.setText("Barchart Annotation is possible");
text.setBounds(50,90,200,text.getHeight());
}
else
{
//text=new JTextField(50);
//pane.setSize(300,300);
text.setText("BarchartAnnotation not possible");
text.setBounds(50,90,300,text.getHeight());
}
}
}
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
Thanks Swastik & it seems you've used setBounds() functionality to achieve this!
Regards!
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1190
|
|
|
Thats right, there might some other solution too.
|
 |
 |
|
|
subject: Doubt in ternary operator
|
|
|