Reputation: 692
I am working in a project where i must use a javax.swing.JSpinner for representing a money increment value.
I have the following problem: When I built it in my Class Constructor like the following code, the JSpinner does not change, instead it continues with the default behavior. Even I modify the enable property and nothing happens
How I can do to make the JSpinner behave as I want?
This is the code built in Netbeans:
public class TesingSpinner extends javax.swing.JFrame {
SpinnerNumberModel model;
JSpinner.NumberEditor editor;
/**
* Creates new form ProbandoSpinner
*/
public TesingSpinner() {
initComponents();
model = new SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
jSpinner1 = new JSpinner(model);
editor = new JSpinner.NumberEditor(jSpinner1);
jSpinner1.setEditor(editor);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jSpinner1.setEnabled(false);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jSpinner1.setEnabled(true);
}
private void initComponents() {
jSpinner1 = new javax.swing.JSpinner();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("El Spinner");
jButton1.setText("Bloquear");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Desbloquear");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)))
.addContainerGap(164, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(42, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TesingSpinner().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JSpinner jSpinner1;
// End of variables declaration
}
Upvotes: 1
Views: 1317
Reputation: 692
@VGR, i follow your annotations and what I did was to delete the definition in my Constructor class and code it in the initComponents() method. The behavior that i want is now happening, and the component changes it properties.
public TesingSpinner() {
initComponents();
// javax.swing.SpinnerNumberModel model;
// javax.swing.JSpinner.NumberEditor editor;
// model = new javax.swing.SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
// jSpinner1 = new javax.swing.JSpinner(model);
// editor = new javax.swing.JSpinner.NumberEditor(jSpinner1);
// jSpinner1.setEditor(editor);
}
private void initComponents() {
javax.swing.SpinnerNumberModel model;
javax.swing.JSpinner.NumberEditor editor;
model = new javax.swing.SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
jSpinner1 = new javax.swing.JSpinner(model);
editor = new javax.swing.JSpinner.NumberEditor(jSpinner1);
jSpinner1.setEditor(editor);
...
Upvotes: 2
Reputation: 5762
Delete the JSpinner
definition in initComponents()
jSpinner1 = new javax.swing.JSpinner();
and change this in the constructor
public TesingSpinner() {
model = new SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
jSpinner1 = new JSpinner(model);
editor = new JSpinner.NumberEditor(jSpinner1);
jSpinner1.setEditor(editor);
initComponents();
}
Actually what is happening is that you are creating two JSpinner
; one in the constructor and another in the init
. After pack()
you are controlling the second one i.e. the init
one which is the default spinner.
If you can not change the code in initComponents
you can also do this
initComponents();
model = new SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
jSpinner1.setModel(model);
editor = new JSpinner.NumberEditor(jSpinner1);
jSpinner1.setEditor(editor);
Upvotes: 3
Reputation: 3622
You can use NetBeans to add your own component initialization to work together with the initComponents
method. See for example: How to modify/add code to the initComponents() method in Java using NetBeans?
This allows you to "Enter code as part of one of the following code properties: Pre-Creation Code, Post-Creation Code, Pre-Init Code, Post-Init Code, Post-Listener Code, Pre-Population Code, Post-Population Code and After-All-Set Code."
Upvotes: 0