user4031188
user4031188

Reputation:

Java SE7 getpercentinstance

I have a program that uses an actionPerformed listener on a JFormattedTextField that is formatted using the NumberFormat.getPercentInstance. It is set up as follows:

    percentFormat = NumberFormat.getPercentInstance();
    txtRPI = new JFormattedTextField(percentFormat);
    percentFormat.setMinimumFractionDigits(1);
    percentFormat.setMaximumIntegerDigits(2);
    GridBagConstraints gbc_txtRPI = new GridBagConstraints();
    gbc_txtRPI.insets = new Insets(0, 0, 5, 5);
    gbc_txtRPI.gridx = 3;
    gbc_txtRPI.gridy = 2;
    gbc_txtRPI.anchor = GridBagConstraints.FIRST_LINE_START;
    txtRPI.setValue(objParams.getRPI());
    panTop.add(txtRPI, gbc_txtRPI);
    txtRPI.setColumns(10);
    txtRPI.addActionListener(new ActionListener () {
        public void actionPerformed(ActionEvent e) {
            objParams.setRPI((double)txtRPI.getValue());
        }
    });

This worked fine until I added the following code on a different JPanel with a table:

public class MyPercentRender extends DefaultTableCellRenderer {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private static NumberFormat formatter = NumberFormat.getPercentInstance();

public Component getTableCellRendererComponent(
    JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    // First format the cell value as required
    formatter.setMinimumFractionDigits(1);
    formatter.setMaximumFractionDigits(2);
    formatter.setMaximumIntegerDigits(2);
    value = formatter.format((Number)value);

    // And pass it on to parent class

    return super.getTableCellRendererComponent(
    table, value, isSelected, hasFocus, row, column );
    }

}

Once this had been added the first field's actionListener stopped being fired and thus I could not save the value.

Has anyone seen this behaviour before and knows what is going on?


Thanks for the reply. I have taken static out. As far as being part of the event dispatch thread, it is.

I have tried a few more things and it seems that the percentinstance is a red herring. Even if I take this out the actionlistener is not being fired. I have checked the object after creation to and the listener is registered along with one other of Class<T> .

I have used this code

    txtRPI = new JFormattedTextField();
    GridBagConstraints gbc_txtRPI = new GridBagConstraints();
    gbc_txtRPI.insets = new Insets(0, 0, 5, 5);
    gbc_txtRPI.gridx = 3;
    gbc_txtRPI.gridy = 2;
    gbc_txtRPI.anchor = GridBagConstraints.FIRST_LINE_START;
    txtRPI.setValue(objParams.getRPI());
    txtRPI.setColumns(10);
    txtRPI.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            objParams.setRPI((double)txtRPI.getValue());
        }
    });
    panTop.add(txtRPI, gbc_txtRPI);
    this.add(panTop, BorderLayout.PAGE_START);

But still the same result. All my other actionListeners (on buttons and ComboBox) work. Is there something about Formatted Text?

Upvotes: 0

Views: 128

Answers (2)

user4031188
user4031188

Reputation:

The issue was the formatting enforced by the percentInstance. If you just enter a numeric value without the % it fails the validating and thus does a REVERT and does not fire the actionevent. If you put the % sign in it fires the event.

Upvotes: 1

trashgod
trashgod

Reputation: 205875

Two related problems arise in looking at your fragment:

Upvotes: 1

Related Questions