This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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:
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
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
public void actionPerformed(ActionEvent ae)
text=new JTextField(25); text.setText("Barchart Annotation is possible");
text=new JTextField(25); text.setText("BarchartAnnotation not possible");
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.
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.
Joined: Oct 06, 2008
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?