user3477016
user3477016

Reputation: 145

In Java, why does my constructor not get called when it is in the outer class?

When running the following code, the constructor:

public ItemHandler(Font f)

Does not seem to run, or at least not as it should when the variable:

private Font font = null;

Is in the outer class. If I put it in the "ItemHandler" class it will work, but my main question is, why can't I put it on the class outside of it?

public class Window extends JFrame {

    private Font font = null;
    private Font plainFont;
    private Font boldFont;
    private Font italicFont;
    private Font boldItalicFont;
    private JTextField textField;
    private JRadioButton plainBtn;
    private JRadioButton boldBtn;
    private JRadioButton italicBtn;
    private JRadioButton boldItalicBtn;
    private ButtonGroup group;

    public Window() {

        super("Item Test Program");
        setLayout(new FlowLayout());

        plainFont = new Font("Serif", Font.PLAIN, 14);
        boldFont = new Font("Serif", Font.BOLD, 14);
        italicFont = new Font("Serif", Font.ITALIC, 14);
        boldItalicFont = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        textField = new JTextField("Please enter something here.", 16);
        plainBtn = new JRadioButton("Plain", true);
        boldBtn = new JRadioButton("Bold", false);
        italicBtn = new JRadioButton("Italic", false);
        boldItalicBtn = new JRadioButton("Bold and Italic", false);
        group = new ButtonGroup();

        ItemHandler plainItemHandler = new ItemHandler(plainFont);
        ItemHandler boldItemHandler = new ItemHandler(boldFont);
        ItemHandler italicItemHandler = new ItemHandler(italicFont);
        ItemHandler boldItalicItemHandler = new ItemHandler(boldItalicFont);

        plainBtn.addItemListener(plainItemHandler);
        boldBtn.addItemListener(boldItemHandler);
        italicBtn.addItemListener(italicItemHandler);
        boldItalicBtn.addItemListener(boldItalicItemHandler);

        textField.setFont(new Font("Serif", Font.PLAIN, 14));

        group.add(plainBtn);
        group.add(boldBtn);
        group.add(italicBtn);
        group.add(boldItalicBtn);

        add(textField);
        add(plainBtn);
        add(boldBtn);
        add(italicBtn);
        add(boldItalicBtn);

    }

    private class ItemHandler implements ItemListener { 

        public ItemHandler(Font f) {

            font = f;

        }

        public void itemStateChanged(ItemEvent event) {

            textField.setFont(font);

        }

    }

}

Upvotes: 0

Views: 218

Answers (2)

Pshemo
Pshemo

Reputation: 124225

ItemHandler constructor runs fine. Problem is that each of its instances try to set value in same font field in Window instance (they share it) so it will store value set by last created ItemHandler.

To avoid this problem move font field from Window to ItemHandler

public class Window extends JFrame {

    //private Font font = null; // move it from here ------+
    //...                                                  |
    private class ItemHandler implements ItemListener { // |
                                                        // |
        private Font font = null; // <--- [ here ]---------+

        public ItemHandler(Font f) {
            font = f;
        }

        public void itemStateChanged(ItemEvent event) {
            textField.setFont(font);
        }

    }

}

so each instance of ItemHandler will have its own field in which it will store its own information about font.

Upvotes: 0

ajb
ajb

Reputation: 31699

I don't think it's clear what you're asking. However, note that your constructor

    public ItemHandler(Font f) {
        font = f;
    }

is modifying a variable that belongs to Window, not ItemHandler. Thus, when the same instance of Window creates four ItemHandler instances, each of those ItemHandler constructors will modify the font variable in the same Window instance. That means that when the Window code does this:

    ItemHandler plainItemHandler = new ItemHandler(plainFont);
    ItemHandler boldItemHandler = new ItemHandler(boldFont);
    ItemHandler italicItemHandler = new ItemHandler(italicFont);
    ItemHandler boldItalicItemHandler = new ItemHandler(boldItalicFont);

they will all overwrite the same font variable, which means that only the last one counts. If you were expecting something different, i.e. four different font variables each set to the corresponding parameter, that isn't going to happen.

Upvotes: 2

Related Questions