Question

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...

Was it helpful?

Solution

  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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top