This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in ternary operator

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have high chances of being wrong, but i doubt if we can do this


Try using the equals method on strings.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Janardhan for your opinions.
But anyhow the problem persists.


Error trace:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Garrett Rowe,
Now it's working!
My best Regards
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
}
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Swastik & it seems you've used setBounds() functionality to achieve this!
Regards!

 
Swastik Dey
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats right, there might some other solution too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic