0

Am a newbie to JFrame, am designing an application... In that I have created 16 checkboxes, each assigned different value... Now my question is when a JCheckBox is checked I want to write that value in a JFormattedTextField...

I have added Checkboxes like this

JCheckBox[] jbx = new JCheckBox[16];    
for(int i = 0; i < jbx.length; i++) {
    if(i%2 == 0) {
        jbx[i].setBounds(x_axis-300,y_axis,300,40);
        panel.add(jbx[i]);
    } else {
        jbx[i].setBounds(x_axis,y_axis,300,40);
        panel.add(jbx[i]);
        y_axis += 30;
    }
}

Can anyone please help me to do this...?

Edited....

I used this code

b.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            System.out.println(e.getStateChange() == ItemEvent.SELECTED ? "Selected" : "Deselected");
        }
    });

When i tried arithmetic operation instead of printing, its not worked...

Ramkumar P
  • 209
  • 1
  • 2
  • 14
  • use [JCheckBox#isSelected()](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#isSelected%28%29) – Braj May 10 '14 at 13:42
  • **Again**, `"When i tried arithmetic operation instead of printing, its not worked..."`, tells us nothing of use. What arithmetic operatin are you trying? Please ask your questions with an eye towards our point of view -- folks who have no clue as to what you're trying to do or how your code is working and not working. – Hovercraft Full Of Eels May 10 '14 at 17:46
  • For example i tried z=x+y if checkbox is selected, but its giving error that all the three variables should be final... But cant assign value to the final variable, thats the my problem... – Ramkumar P May 10 '14 at 18:04
  • @RamkumarP: again, edit your question, and show the actual offending code, including where you declare the variables and where you use them. It sounds like you're trying to use local variables in an anonymous inner class, your listener, and when doing this, the variables must either be declared final, or non-local (fields). – Hovercraft Full Of Eels May 10 '14 at 18:21
  • @HovercraftFullOfEels: Thank you! I found my error and corrected it... – Ramkumar P May 10 '14 at 18:29
  • @RamkumarP: glad you've got it working. – Hovercraft Full Of Eels May 10 '14 at 18:58

1 Answers1

2
  1. Add an ActionListener or ItemListener to your JCheckBox if you want to be notified when it is checked or unchecked and then respond to it. Simple as that.
  2. As for writing to the JFormattedTextField part, we have no idea about what specifically has you hung up given the limited information you've posted so far.
  3. Don't use null layouts and setBounds(...) this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's. Your current code suggests that you want to use a GridLayout with 2 columns.
  4. Google and then study the Java Swing Tutorials as all this information and more is to be easily found there.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Yeah the ItemListener working, but i couldn't able to use arithmetic operation inside the ItemStateChanged... The code is here.. 'b.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { System.out.println(e.getStateChange() == ItemEvent.SELECTED ? "Slected" : "Deselected"); } });' ... I want to do some arithmetic operation instead of printing, what i have to do for this... – Ramkumar P May 10 '14 at 17:11
  • @RamkumarP: code is impossible to read in comments. [Edit your original question](http://stackoverflow.com/posts/23581759/edit) (click on the link) please and add any new code and question at the bottom. Also show what you're trying to do. `" I want to do some arithmetic operation..."` tells us very little I'm afraid. – Hovercraft Full Of Eels May 10 '14 at 17:16