public LabeledTextField(boolean isIndentedLabel, String label, String format) { super(); super.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); tag = new JLabel(label); if (isIndentedLabel) { JPanel tag_panel = new JPanel(); tag_panel.setLayout(new BoxLayout(tag_panel, BoxLayout.X_AXIS)); tag_panel.add(Box.createHorizontalStrut(Const.LABEL_INDENTATION)); tag_panel.add(tag); tag_panel.add(Box.createHorizontalGlue()); tag_panel.setAlignmentX(Component.LEFT_ALIGNMENT); super.add(tag_panel); } else { tag.setAlignmentX(Component.LEFT_ALIGNMENT); super.add(tag); } fld = new ActableTextField(); tag.setLabelFor(fld); fld.setAlignmentX(Component.LEFT_ALIGNMENT); super.add(fld); // preferred_height = fld.getPreferredSize().height + this.TEXT_HEIGHT; if (format != null) { int num_col; fmt = (DecimalFormat) NumberFormat.getInstance(); fmt.applyPattern(format); num_col = Routines.getAdjNumOfTextColumns(fld, format.length()); fld.setColumns(num_col); } else fmt = null; /* No self DocumentListener by default */ self_listener = null; // tag.setBorder( BorderFactory.createEtchedBorder() ); fld.setBorder(BorderFactory.createEtchedBorder()); if (FONT != null) { tag.setFont(FONT); fld.setFont(FONT); } }
public float getFloat() { String float_str = null; if (self_listener != null) float_str = self_listener.getLastUpdatedText(); else float_str = fld.getText(); try { return fmt.parse(float_str).floatValue(); } catch (ParseException perr) { perr.printStackTrace(); return Float.MIN_VALUE; } }
public double getDouble() { String double_str = null; if (self_listener != null) double_str = self_listener.getLastUpdatedText(); else double_str = fld.getText(); try { return fmt.parse(double_str).doubleValue(); } catch (ParseException perr) { perr.printStackTrace(); return Double.MIN_VALUE; } }
public int getInteger() { String int_str = null; if (self_listener != null) int_str = self_listener.getLastUpdatedText(); else int_str = fld.getText(); try { return fmt.parse(int_str).intValue(); } catch (ParseException perr) { perr.printStackTrace(); return Integer.MIN_VALUE; } }
public short getShort() { String short_str = null; if (self_listener != null) short_str = self_listener.getLastUpdatedText(); else short_str = fld.getText(); try { return fmt.parse(short_str).shortValue(); } catch (ParseException perr) { perr.printStackTrace(); return Short.MIN_VALUE; } }
// BoxLayout respects component's maximum size public Dimension getMaximumSize() { // return new Dimension( Short.MAX_VALUE, preferred_height ); return new Dimension( Short.MAX_VALUE, fld.getPreferredSize().height + LabeledTextField.TEXT_HEIGHT); }
public void addSelfDocumentListener() { self_listener = new FieldDocumentListener(); fld.getDocument().addDocumentListener(self_listener); }
public void addActionListener(ActionListener listener) { fld.addActionListener(listener); }
public void setEnabled(boolean flag) { fld.setEnabled(flag); }
public void setEditable(boolean flag) { fld.setEditable(flag); }
public boolean getBoolean() { String bool_str = null; if (self_listener != null) bool_str = self_listener.getLastUpdatedText(); else bool_str = fld.getText(); return bool_str.equalsIgnoreCase("true") || bool_str.equalsIgnoreCase("yes"); }
public void setFieldFont(Font font) { fld.setFont(font); }
public void setFloat(float fval) { fld.setText(fmt.format(fval)); }
public void setInteger(int ival) { // fld.setText( Integer.toString( ival ) ); fld.setText(fmt.format(ival)); }
public void setShort(short sval) { fld.setText(fmt.format(sval)); }
public void fireActionPerformed() { fld.fireActionPerformed(); }
public String getText() { if (self_listener != null) return self_listener.getLastUpdatedText(); else return fld.getText(); }
public void setDouble(double dval) { fld.setText(fmt.format(dval)); }
public void setBoolean(boolean bval) { fld.setText(String.valueOf(bval)); }
public void setHorizontalAlignment(int alignment) { fld.setHorizontalAlignment(alignment); }
public void setText(String str) { fld.setText(str); }