0

I have a custom control that I've created. It functions properly but is rendered incorrectly. Spinner Design Problem

As can be seen the designer (netbeans) keeps everything neatly packed. However in the running program there are spaces and the text on the buttons does not show.

Here is the code.

public class AllocateSpinner extends javax.swing.JPanel {

  /**
   * Creates new form AllocateSpinner
   */
  public AllocateSpinner() {
    initComponents();
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  private void initComponents() {

    Min = new java.awt.Button();
    Decrement = new java.awt.Button();
    NumberEdit = new javax.swing.JTextField();
    Increment = new java.awt.Button();
    Max = new java.awt.Button();

    setMinimumSize(new java.awt.Dimension(100, 23));
    setPreferredSize(new java.awt.Dimension(100, 23));
    setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.X_AXIS));

    Min.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
    Min.setLabel("|<");
    Min.setMaximumSize(new java.awt.Dimension(15, 32767));
    Min.setMinimumSize(new java.awt.Dimension(15, 23));
    Min.setPreferredSize(new java.awt.Dimension(15, 23));
    Min.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        MinActionPerformed(evt);
      }
    });
    add(Min);

    Decrement.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
    Decrement.setLabel("<<");
    Decrement.setMaximumSize(new java.awt.Dimension(15, 32767));
    Decrement.setMinimumSize(new java.awt.Dimension(15, 23));
    Decrement.setPreferredSize(new java.awt.Dimension(15, 23));
    Decrement.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        DecrementActionPerformed(evt);
      }
    });
    add(Decrement);

    NumberEdit.setText("0");
    NumberEdit.setMinimumSize(new java.awt.Dimension(20, 23));
    NumberEdit.setPreferredSize(new java.awt.Dimension(20, 23));
    NumberEdit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        NumberEditActionPerformed(evt);
      }
    });
    add(NumberEdit);

    Increment.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
    Increment.setLabel(">>");
    Increment.setMaximumSize(new java.awt.Dimension(15, 32767));
    Increment.setMinimumSize(new java.awt.Dimension(15, 23));
    Increment.setPreferredSize(new java.awt.Dimension(15, 23));
    Increment.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        IncrementActionPerformed(evt);
      }
    });
    add(Increment);

    Max.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
    Max.setLabel(">|");
    Max.setMaximumSize(new java.awt.Dimension(15, 32767));
    Max.setMinimumSize(new java.awt.Dimension(15, 23));
    Max.setPreferredSize(new java.awt.Dimension(15, 23));
    Max.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        MaxActionPerformed(evt);
      }
    });
    add(Max);
  }// </editor-fold>                        

  private void MinActionPerformed(java.awt.event.ActionEvent evt) {                                    
    setValue(_MinValue);
  }                                   

  private void DecrementActionPerformed(java.awt.event.ActionEvent evt) {                                          
    setValue(_Value - _ValueStep);
  }                                         

  private void IncrementActionPerformed(java.awt.event.ActionEvent evt) {                                          
    setValue(_Value + _ValueStep);
  }                                         

  private void MaxActionPerformed(java.awt.event.ActionEvent evt) {                                    
    setValue(_MaxValue);
  }                                   

  private void NumberEditActionPerformed(java.awt.event.ActionEvent evt) {                                           
    try {
      setValue(Integer.parseInt(NumberEdit.getText()));
    } catch (NumberFormatException e) {
      NumberEdit.setText(_Formatter.format(_Value));
    }
  }                                          

  // Variables declaration - do not modify                     
  private java.awt.Button Decrement;
  private java.awt.Button Increment;
  private java.awt.Button Max;
  private java.awt.Button Min;
  private javax.swing.JTextField NumberEdit;
  // End of variables declaration                   

  private int _Value = 0;
  private int _MinValue = 0;
  private int _MaxValue = 100;
  private int _ValueStep = 1;
  private NumberFormat _Formatter = NumberFormat.getIntegerInstance();

  /**
   * Sets the value. If the value is less than MinValue or greater than MaxValue the value is set to the limit.
   * @param value The value to set.
   */
  public void setValue(int value) {
    if (_Value != value) {
      int ov = _Value;
      value = Math.max(_MinValue, value);
      value = Math.min(_MaxValue, value);
      _Value = value;
      NumberEdit.setText(_Formatter.format(_Value));
      firePropertyChange("Value", ov, _Value);
    }
  }

  /**
   * Get the current value.
   * @return The current value.
   */
  public int getValue() {
    return _Value;
  }

  /**
   * Sets the minimum value. If the current value is less than the new minimum value the current value is set to minimum.
   * @param value The new minimum value to set.
   */
  public void setMinValue(int value) {
    if (_MinValue != value) {
      int ov = _MinValue;
      value = Math.min(_MaxValue, value);
      _MinValue = value;
      firePropertyChange("MinValue", ov, _MinValue);
      if (_Value < _MinValue) {
        setValue(_MinValue);
      }
    }
  }

  /**
   * Get the minimum value that can be set.
   * @return The minimum value.
   */
  public int getMinValue() {
    return _MinValue;
  }

  /**
   * Set the maximum value. If the current value is greater than the new maximum the current value is set to maximum.
   * @param value The new Maximum value.
   */
  public void setMaxValue(int value) {
    if (_MaxValue != value) {
      int ov = _MaxValue;
      value = Math.max(_MinValue, value);
      _MaxValue = value;
      firePropertyChange("MaxValue", ov, _MaxValue);
      if (_Value > _MaxValue) {
        setValue(_MaxValue);
      }
    }
  }

  /**
   * Get the maximum value that can be set.
   * @return The maximum value.
   */
  public int getMaxValue() {
    return _MaxValue;
  }

  /**
   * Set the amount that the number will be changed on increment or decrement operations.
   * @param value The step amount.
   */
  public void setValueStep(int value) {
    if (_ValueStep != value) {
      int ov = _ValueStep;
      _ValueStep = value;
      firePropertyChange("ValueStep", ov, _ValueStep);
    }
  }

  /**
   * Get the amount that the value changes on increment or decrement operations.
   * @return The step amount.
   */
  public int getValueStep() {
    return _ValueStep;
  }

  /**
   * Set the number formatter to use.
   * @param formater The formatter to use.
   */
  public void setFormatter(NumberFormat formater) {
    if (_Formatter != formater) {
      NumberFormat of = _Formatter;
      _Formatter = formater;
      NumberEdit.setText(_Formatter.format(_Value));
      this.firePropertyChange("Formatter", of, _Formatter);
    }
  }

  /**
   * Get the formatter currently being used.
   * @return The NumberFormat currently being used.
   */
  public NumberFormat getFormatter() {
    return _Formatter;
  }
}
Robert Reinhart
  • 507
  • 4
  • 10
  • 2
    Your Buttons are awt buttons, not swing JButtons – ControlAltDel Apr 30 '15 at 20:42
  • GRRRR... Yea, that seams to have made the difference! – Robert Reinhart Apr 30 '15 at 21:24
  • 1
    Variable names should NOT start with an upper case character. Follow the Java conventions. Don't use setMinimumSize() and setPreferredSize(). Let the layout manager determine the size. – camickr Apr 30 '15 at 21:37
  • yea, those were getting set trying to get the layout to work right. They got removed when the change to JButton fixed the allignment. As for the naming... I'm porting a program that was written first in visual basic ported to c++ then to c# through 4 different development teams. Keeping the same variable names (whatever the casing) is just a sanity thing right now. I'll change them once the project is working.... along with all the other fixes that it needs! – Robert Reinhart May 01 '15 at 00:42
  • 1
    Avoid [`setXxxSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod May 01 '15 at 01:18

1 Answers1

0

Comment of "Your Buttons are awt buttons, not swing JButtons" and subsequent adjustment from Button to JButton seems to have led to a resolution of this problem.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80