0

I've come across a problem where when I want to use the setText() method on a label, it won't change the label's text. I've searched for a long time, but couldn't find any solution. Here is a sample of the code:

class OptionsListener implements ItemListener{
    public void itemStateChanged(ItemEvent e) {
        if (optionsI1.isSelected()){
            lesu1.setText("8:30");
        }
    }
}

I display all the components I use into another class, everything is displayed correctly.

I've tried simplifying the code by doing something like this:

class OptionsListener implements ItemListener{
    public void itemStateChanged(ItemEvent e) {
        if (optionsI1.isSelected()){
            System.out.println("bla");
        }
    }

Which seems to work fine and display the message "bla". Anything I'm missing here?

The declaration of my elements (only showing the labels and menu's in the order I coded it):

    // labels //
    lesu1 = new JLabel("1");
    lesu1.setBounds(8, 39, 20, 22);

    lesu2 = new JLabel("2");
    lesu2.setBounds(8, 69, 20, 22);

    lesu3 = new JLabel("3");
    lesu3.setBounds(8, 99, 20, 22);

    lesu4 = new JLabel("4");
    lesu4.setBounds(8, 129, 20, 22);

    lesu5 = new JLabel("5");
    lesu5.setBounds(8, 159, 20, 22);

    lesu6 = new JLabel("6");
    lesu6.setBounds(8, 189, 20, 22);

    lesu7 = new JLabel("7");
    lesu7.setBounds(8, 219, 20, 22);

    lesu8 = new JLabel("8");
    lesu8.setBounds(8, 249, 20, 22);

    dag = new JLabel("07/08");
    dag.setBounds(5, 15, 36, 13);

    // menubar //
    menu = new JMenuBar();

    options = new JMenu("Opties");

    optionsI1 = new JCheckBoxMenuItem("Weergeef de lesuren in uren");
    optionsI1.addItemListener(new OptionsListener());

    menu.add(options);

    options.add(optionsI1);

This is the order I placed those components. I also added them to the panel in this order.

GeeSplit
  • 53
  • 3
  • 12

2 Answers2

0

Your Label lesu1 is not placed on any component like panel,button etc.

Add lable to some component.

For ex.

menu.add(lesu1);
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
0

I found out what the problem was. I was confused and used a constructor for everything instead of methods. That solved it.

As I said: I'm a beginner in Java.

GeeSplit
  • 53
  • 3
  • 12
  • 1
    I recommend you not to use `null layout`, swing is designed to work with [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). Using `null layout` will make you create bad habits of programming, read that link. And while you're on that please read [The use of multiple `JFrames` Good, Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657) Both will help you to improve your Java Programming skills :) – Frakcool Jul 28 '14 at 20:04