2 JCheckBox behandeln (mit Code!)

schwaigerf

Grünschnabel
Hi Leute,
ich habe folgendes Problem:
Ich habe 2 JCheckBoxen und möchte diese getrennt ansteuern, d.h dass die eine unabhängig von der anderen sein soll.
Nur das bei mir nicht.
Hier der Code:

Code:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.Event;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.JCheckBox;

import javax.swing.JDialog;

import javax.swing.JFrame;

public class PreferencesDialog extends JDialog implements ItemListener {

	private JCheckBox cb_on;

	private JCheckBox lineWrapping;

	private JTextField autoSaveInput;


	public int value;

	private JButton takeAutoSave;

	private JButton takeWrap;

	JPanel panel = new JPanel();

	public PreferencesDialog(JFrame owner) {

		super(owner);

		initialize();

	}

	private void initialize() {

		setTitle("Preferences");

		setModal(true);

		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		setSize(400, 200);

		setLocation(getOwner().getX() + 20, getOwner().getY() + 10);

		// Checkbox Auto Save
		// ==========================================================

		cb_on = new JCheckBox("AutoSave ?");
		cb_on.setSelected(false);
		cb_on.addItemListener(this);
		panel.add(cb_on);

		// ==========================================================

		// TextField for Input of SaveTime
		// ==========================================================

		autoSaveInput = new JTextField(5);
		panel.add(autoSaveInput);
		takeAutoSave = new JButton("Apply");
		panel.add(takeAutoSave);
		takeAutoSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				if (event.getSource() == takeAutoSave) {

					String input = autoSaveInput.getText();
					value = Integer.parseInt(input);
					Editor.autoTime = value;
				}
			}
		});
		lineWrapping = new JCheckBox("LineWrapping ?");
		lineWrapping.setSelected(false);
		lineWrapping.addItemListener(this);
		panel.add(lineWrapping);

		{

		}

		// OK Button

		JButton btn_ok = new JButton("Ok");

		panel.add(btn_ok);
		this.setContentPane(panel);
		btn_ok.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent ev) {

				dispose();

			}

		});

	}

	public void itemStateChanged(ItemEvent e) {
		Object source = e.getItemSelectable();
		Object source2 = e.getItemSelectable();
		if (source == cb_on) {
			Editor.autoSave = true;
			System.out.println("AutoSave on");

		}
		if (e.getStateChange() == ItemEvent.DESELECTED) {
			Editor.autoSave = false;
			System.out.println("AutoSave off");
		}
		if(source2 == lineWrapping)
		{
			Editor.textArea.setLineWrap(true);
			System.out.println("LineWrap on");
		}
		if(e.getStateChange() == ItemEvent.DESELECTED)
		{
			Editor.textArea.setLineWrap(false);
			System.out.println("LineWrap off");
		}

	}

}
 
Mal eben drübergeflogen.. Müsste das Event nicht eher so behandelt werden?:

Java:
    public void itemStateChanged(ItemEvent e) {
        Object source = e.getItemSelectable();
        Object source2 = e.getItemSelectable();
        if (source == cb_on) 
        {
            if (e.getStateChange() == ItemEvent.DESELECTED) 
            {
                Editor.autoSave = false;
                System.out.println("AutoSave off");
            }
            else 
            {
                Editor.autoSave = true;
                System.out.println("AutoSave on");
            }
        }
        if(source2 == lineWrapping)
        {
            if(e.getStateChange() == ItemEvent.DESELECTED)
            {
                Editor.textArea.setLineWrap(false);
                System.out.println("LineWrap off");
            }
            else 
            {
                Editor.textArea.setLineWrap(true);
                System.out.println("LineWrap on");
            }
        }
    }
}
 
Zurück