3

Everytime I move my JSlider my JDialog box is appearing for each position it is moved, I only want it to be displayed once when the user tries to move the JSlider without them making a selection from my menu. How would I do this?

public void stateChanged(ChangeEvent e)
{
  if(myFrame.shape == null)
  {

JOptionPane.showMessageDialog(popUp, "You should select an item first.", "Information",     JOptionPane.WARNING_MESSAGE);
  }
  else if(myFrame.shape != null)
  {
    DecimalFormat df = new DecimalFormat("0.0");
    float value = diameterJSlider.getValue();
    String strValue = Float.toString(value);
    sliderLabel.setText(strValue);
    boundaryTextField.setText("" + df.format(myFrame.shape.getBoundary(value)));
    areaTextField.setText("" + df.format(myFrame.shape.getArea(value)));

    myTopPanel.reDraw(diameterJSlider.getValue());
  }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1839601
  • 119
  • 1
  • 9
  • 1
    It appears you are getting an NPE every time your slider moves. – tckmn Nov 20 '12 at 17:20
  • You should probably just disable the slider until they select something from your menu. – Jacob Schoen Nov 20 '12 at 17:22
  • You should never catch NPE's. That's bad programming practice. Sure check for null in an if block, but not like you're doing it. – Hovercraft Full Of Eels Nov 20 '12 at 17:23
  • Yes I am aware of that, that is why I have made my try-catch and would like some output to the user. But everytime the JSlider moves one position it makes the JDialog box appear for each position it has moved. If you move it 20 times, you get the JDialog 20 times. How can I resolve this so it only appears once? – user1839601 Nov 20 '12 at 17:23
  • 1
    Actually not a bad idea to have the JSlider de-selected untill they make a choice. Thanks. – user1839601 Nov 20 '12 at 17:24
  • I still run into the same issue when I change it to an if statement, the JDialog is appearing over and over again. @HovercraftFullOfEels – user1839601 Nov 20 '12 at 17:40
  • That's because you're not accounting for the state of the object. If you only want the dialog to be shown once, you need to use a variable or variables to record if the dialog has been shown, and to not show it if it has been shown. Then reset this variable when the need arises. This won't magically occur unless you code it to occur. But I'm with the other folks: disable the JSlider so that it's not even possible for the user to use it unless it makes sense to. – Hovercraft Full Of Eels Nov 20 '12 at 17:44
  • @HovercraftFullOfEels, I will do just that, diameterJSlider.setVisible(false); then when a selection has been made I'll make it true. However I'm not quite sure were to gain marks for this assignment for Exception Handling if I just fixed the only exception that I've come across. lol. – user1839601 Nov 20 '12 at 17:52
  • 1
    Ah, even better - I can use setEnabled(boolean); – user1839601 Nov 20 '12 at 17:56
  • Again, a NullPointerException should never be handled this way. You should look for other exceptions that are better handled via Exception handling. And you don't have to use `setVisible(...)` but instead could use `setEnabled(...)`. – Hovercraft Full Of Eels Nov 20 '12 at 17:56
  • @HovercraftFullOfEels, I'm one step ahead of you! :P – user1839601 Nov 20 '12 at 17:56
  • Indeed you are, and something you should be proud of! – Hovercraft Full Of Eels Nov 20 '12 at 17:57
  • @HovercraftFullOfEels, could I talk to you in chat? I'm not quite sure how to work chat... lol. – user1839601 Nov 20 '12 at 18:04
  • No, I'm afraid not. I'm at work and lunch break is over. – Hovercraft Full Of Eels Nov 20 '12 at 18:05
  • Aww, shame. Thanks anyway :). – user1839601 Nov 20 '12 at 18:07

1 Answers1

0

But everytime the JSlider moves one position it makes the JDialog box appear for each position it has moved. If you move it 20 times, you get the JDialog 20 times. How can I resolve this so it only appears once?

You may try to ignore adjusting events, to get desired behavior.

  public void stateChanged(ChangeEvent e) {
    JSlider source = (JSlider)e.getSource();
    if (!source.getValueIsAdjusting()) {
      // your code
    }
  }
gmatagmis
  • 318
  • 2
  • 5