Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:11:56+00:00 2026-06-15T09:11:56+00:00

I have a problem. I have an element that would JPanel. In this JPanel

  • 0

I have a problem.
I have an element that would JPanel.
In this JPanel would be four JButtons and image.
This image is ‘green LED lights’ that would change depending on which JButton would be pressed.
It works very well.
But also I want the JComboBox to change this paramter.

And here’s the problem.
Although the parameter (from 1 to 4) is changing,(this shows JLabel near LEDs),
but the picture (LED lights) do not want to change it to the right one.

screenshot

The program is divided into 3 classes.

MyFrame.class This main class.

public class MyFrame extends JFrame {

int ctrlTriggers = 1;
private JLabel label = new JLabel();
private Triggers triggers;
private String[] typeTrig = {"1", "2", "3", "4"};

public MyFrame() {
  JFrame frame = new JFrame("JComboBox Problem");
  frame.setBounds(50, 0, 800, 240);
  frame.setLayout(new BorderLayout());


  int tvalue = 1;
  String tstr = Integer.toString(tvalue);
  JPanel panelTrig = new JPanel(new BorderLayout());
  label = new JLabel(tstr, 4);
  panelTrig.add(label);
  final JLabel et = label;

  triggers = new Triggers(typeTrig, "trigger " + Integer.toString(1));

  triggers.addChangeListener(new ChangeListener() {
     public void stateChanged(ChangeEvent e) {
        Triggers tr = (Triggers) e.getSource();
        int vol;

        vol = (int) tr.getValue();
        et.setText(Integer.toString(vol));

     }
  });

  panelTrig.add(triggers, BorderLayout.LINE_START);



  // JCOMBOBOX
  JPanel pCombo = new JPanel();
  pCombo.setLayout(new FlowLayout());
  Presets combo = new Presets();
  combo.addChangeListener(new ChangeListener() {
     public void stateChanged(ChangeEvent e) {
        Presets presetcombo = (Presets) e.getSource();
        int tval;
        tval = (int) presetcombo.getValue(ctrlTriggers);
        label.setText(Integer.toString(tval));
        triggers.setValue(tval);
     }
  });

  pCombo.add(combo, BorderLayout.CENTER);
  frame.add(pCombo, BorderLayout.CENTER);
  frame.add(panelTrig, BorderLayout.LINE_START);


  frame.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent e) {
        System.exit(0);
     }
  });

  setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  frame.show();
}

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        new MyFrame();
     }
  });
 }
}

Triggers.class This class is JPanel where is 4 JButton and ‘LED like image’

public class Triggers extends JPanel
    implements ActionListener, ItemListener {

public Triggers(String s[], String name) {

  JPanel panel = new JPanel(new BorderLayout());
  this.value = Integer.parseInt(s[selected]);

  for (int i = 0; i < s.length; i++) {


     ImageIcon cup = new ImageIcon();
     button[i] = new JButton(z[i], cup);
     button[i].setPreferredSize(new Dimension(70, 25));

     Font font = button[i].getFont();
     button[i].setFont(new Font(font.getFontName(), font.getStyle(), 10));
     button[i].setActionCommand(s[i]);

     if (i == selected) {
        button[i].setSelected(true);
     } else {
        button[i].setSelected(false);
     }
  }



  ButtonGroup group = new ButtonGroup();

  for (int i = 0; i < s.length; i++) {
     group.add(button[i]);
     button[i].addActionListener(this);
     button[i].addItemListener(this);

  }



  diody = new JLabel(createImageIcon("/resources/diody/"
          + this.value
          + ".png"));


  diody.setPreferredSize(new Dimension(20, 100));

  JPanel triggs = new JPanel(new FlowLayout());
 triggs.setPreferredSize(new Dimension(80, 120));

  for (int i = 0; i < s.length; i++) {
     triggs.add(button[i]);
  }

  panel.add(triggs, BorderLayout.LINE_START);
  panel.add(diody, BorderLayout.LINE_END);
  add(panel);


}

public void actionPerformed(ActionEvent e) {

  cmd = e.getActionCommand();
  diody.setIcon(createImageIcon("/resources/diody/"
          + cmd
          + ".png"));



  if (cmd.equals("1")) {

     setValue(1);

  } else if (cmd.equals("2")) {

     setValue(2);

  } else if (cmd.equals("3")) {

     setValue(3);

  } else if (cmd.equals("4")) {

     setValue(4);
  }
}

public static ImageIcon createImageIcon(String path) {
  java.net.URL imgURL = Triggers.class.getResource(path);
  if (imgURL != null) {
     return new ImageIcon(imgURL);
  } else {
     System.err.println("Couldn't find file: " + path);
     return null;
  }
}

public int setController() {
  return value;
}

public void setValue(int k) {

  this.value = k;
  cmd = Integer.toString(this.value);
  repaint();
  fireChangeEvent();
}

public int getValue() {
  return this.value;
}

public void setSel(int k) {
  selected = k;
  repaint();
  fireChangeEvent();
}

public void itemStateChanged(ItemEvent ie) {
  String s = (String) ie.getItem();
}

public void addChangeListener(ChangeListener cl) {
  listenerList.add(ChangeListener.class, cl);

}

public void removeChangeListener(ChangeListener cl) {
  listenerList.remove(ChangeListener.class, cl);
}

protected void fireChangeEvent() {
  Object[] listeners = listenerList.getListenerList();
  for (int i = listeners.length - 2; i >= 0; i -= 2) {
     if (listeners[i] == ChangeListener.class) {
        if (changeEvent == null) {
           changeEvent = new ChangeEvent(this);
        }
        ((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
     }
  }
}
private ChangeEvent changeEvent = null;
private EventListenerList listenerList = new EventListenerList();
private JLabel diody;
private int x = 1;
private int y = 4;
private JButton button[] = new JButton[y];
double border = 4;
double vborder = 1;
int controller[] = new int[4];
String z[] = {"1", "2", "3", "4"};
private int selected;
private int value;
private String cmd;
}

class Presets Here is JCombobox.

public class Presets extends JPanel
       implements ActionListener {
  private int value;
  private int[] ctrlIdx = new int[27];
  private int controller;
  private ChangeEvent changeEvent = null;
  private EventListenerList listenerList = new EventListenerList();
  private JLabel label;
  private String[][] presets = {
     {"0", "3"},
     {"1", "2"},
     {"2", "3"},
     {"3", "1"},
     {"4", "4"},
     {"5", "2"},};
  String[] ctrlName = {"preset 1 (value 3)", "preset 2 (value 2)", "preset 3 (value 3)", "preset 4 (value 1)", "preset 5 (value 4)", "preset 5 (value 2)"};

  public Presets() {


     JPanel panel = new JPanel();
     panel.setLayout(new BorderLayout());
     panel.setPreferredSize(new Dimension(600, 30));

     JComboBox combolist = new JComboBox(ctrlName);
     combolist.setSelectedIndex(2);
     combolist.addActionListener(this);

     combolist.setPreferredSize(new Dimension(300, 30));
     label = new JLabel();
     label.setFont(label.getFont().deriveFont(Font.ITALIC));
     label.setHorizontalAlignment(JLabel.CENTER);
     int indeks = combolist.getSelectedIndex();
     updateLabel(ctrlName[combolist.getSelectedIndex()], indeks);

     label.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
     label.setPreferredSize(new Dimension(300, 30));


     panel.add(combolist, "East");
     add(panel);
     setPreferredSize(new Dimension(600, 40));

  }

  public void setSelectedItem(int a) {
  }

  public void setValue(int c, int v) {

     controller = c;
     value = v;
     ctrlIdx[controller] = value;

     repaint();
     fireChangeEvent();

  }

  public int getController() {
     return controller;
  }

  public int getValue(int c) {
     int w = (int) ctrlIdx[c];
     return (int) w;
  }

  public void actionPerformed(ActionEvent e) {
     JComboBox cb = (JComboBox) e.getSource();
     String pres = (String) cb.getSelectedItem();
     int indeks = cb.getSelectedIndex();
     updateLabel(pres, indeks);



     for (int i = 0; i < ctrlName.length; i++) {
        for (int j = 0; j < 2; j++) {

           setValue(j, Integer.parseInt(presets[indeks][j]));
        }
     }


  }

  protected void updateLabel(String name, int ii) {

     label.setToolTipText("A drawing of a " + name.toLowerCase());

  }

  protected static ImageIcon createImageIcon(String path) {
     java.net.URL imgURL = Presets.class.getResource(path);
     if (imgURL != null) {
        return new ImageIcon(imgURL);
     } else {
        System.err.println("Couldn't find file: " + path);
        return null;
     }
  }

  public void addChangeListener(ChangeListener cl) {
     listenerList.add(ChangeListener.class, cl);
  }

  public void removeChangeListener(ChangeListener cl) {
     listenerList.remove(ChangeListener.class, cl);
  }

  protected void fireChangeEvent() {

     Object[] listeners = listenerList.getListenerList();

     for (int i = listeners.length - 2; i >= 0; i -= 2) {
        if (listeners[i] == ChangeListener.class) {

           if (changeEvent == null) {
              changeEvent = new ChangeEvent(this);
           }
           ((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
        }
     }
  }

}

Could someone help me, what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:11:58+00:00Added an answer on June 15, 2026 at 9:11 am

    Your image is only begin loaded in the actionPerformed method of your Triggers class, which means when you call setValue in response to the stateChanged event thrown by the Presets class, no new image is begin loaded.

    Move your image loading logic so it is either in the setValue method or triggered by the setValue method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a gstreamer plugin with an element inside that would generate some
I have this problem converting java.sql.Timestamp.toString back to java.sql.Timestamp . Given element = 2012-08-01
I have this problem that i want to transfer specific ( yet not that
I have a weird problem. I have a vector that I would like to
I have problems setting the focus on an input element that has been created
I have a problem finding elements in XPath that's contains a certain string ignoring
My problem is simple: I have a long list of elements that I want
I have a problem with element selection, i have a form and i want
I have a problem where an element is not showing on Opera nor Firefox.
I have a problem positioning an element in certain browsers. I'm using the jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.