void updateTheLabel(DocumentEvent e) { Document doc = (Document) e.getDocument(); String text = null; try { text = doc.getText(0, doc.getLength()); doc = null; } catch (BadLocationException ex) { text = null; } if (text != null) { try { double number = Double.parseDouble(text); if (number > 1) { label.setText(labelPair.getPlural()); } else { label.setText(labelPair.getSingular()); } } catch (NumberFormatException ex) { // Do nothing } finally { text = null; } } }
public void addForDisplay(CTalkativeTextPane pane, LabelPair<String, String> lp) { GridBagLayout gridbag = (GridBagLayout) getLayout(); labelPair = lp; label = new JLabel(labelPair.getSingular(), JLabel.LEFT); pane.getDocument().addDocumentListener(this); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0.1; c.weighty = 0.1; c.anchor = GridBagConstraints.LINE_START; c.insets = new Insets(0, 20, 0, 00); gridbag.setConstraints(pane, c); add(pane); c = new GridBagConstraints(); c.gridx = 1; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0.1; c.weighty = 0.1; c.anchor = GridBagConstraints.LINE_START; c.insets = new Insets(0, 0, 0, 20); gridbag.setConstraints(label, c); add(label); JPanel filler = new JPanel(); filler.setBackground(white); c = new GridBagConstraints(); c.gridx = 2; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0.8; c.weighty = 0.8; c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.LINE_END; gridbag.setConstraints(filler, c); add(filler); filler = null; c = null; }