Example usage for javax.swing JFormattedTextField JFormattedTextField

List of usage examples for javax.swing JFormattedTextField JFormattedTextField

Introduction

In this page you can find the example usage for javax.swing JFormattedTextField JFormattedTextField.

Prototype

public JFormattedTextField(AbstractFormatterFactory factory) 

Source Link

Document

Creates a JFormattedTextField with the specified AbstractFormatterFactory.

Usage

From source file:FieldValidator.java

private static JComponent createContent() {
    Dimension labelSize = new Dimension(80, 20);

    Box box = Box.createVerticalBox();

    // A single LayerUI for all the fields.
    LayerUI<JFormattedTextField> layerUI = new ValidationLayerUI();

    // Number field.
    JLabel numberLabel = new JLabel("Number:");
    numberLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    numberLabel.setPreferredSize(labelSize);

    NumberFormat numberFormat = NumberFormat.getInstance();
    JFormattedTextField numberField = new JFormattedTextField(numberFormat);
    numberField.setColumns(16);/*w  w w  .  j  a  va  2 s  . c om*/
    numberField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    numberField.setValue(42);

    JPanel numberPanel = new JPanel();
    numberPanel.add(numberLabel);
    numberPanel.add(new JLayer<JFormattedTextField>(numberField, layerUI));

    // Date field.
    JLabel dateLabel = new JLabel("Date:");
    dateLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    dateLabel.setPreferredSize(labelSize);

    DateFormat dateFormat = DateFormat.getDateInstance();
    JFormattedTextField dateField = new JFormattedTextField(dateFormat);
    dateField.setColumns(16);
    dateField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    dateField.setValue(new java.util.Date());

    JPanel datePanel = new JPanel();
    datePanel.add(dateLabel);
    datePanel.add(new JLayer<JFormattedTextField>(dateField, layerUI));

    // Time field.
    JLabel timeLabel = new JLabel("Time:");
    timeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    timeLabel.setPreferredSize(labelSize);

    DateFormat timeFormat = DateFormat.getTimeInstance();
    JFormattedTextField timeField = new JFormattedTextField(timeFormat);
    timeField.setColumns(16);
    timeField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    timeField.setValue(new java.util.Date());

    JPanel timePanel = new JPanel();
    timePanel.add(timeLabel);
    timePanel.add(new JLayer<JFormattedTextField>(timeField, layerUI));

    // Put them all in the box.
    box.add(Box.createGlue());
    box.add(numberPanel);
    box.add(Box.createGlue());
    box.add(datePanel);
    box.add(Box.createGlue());
    box.add(timePanel);

    return box;
}

From source file:FormatterFactoryDemo.java

public FormatterFactoryDemo() {
    super(new BorderLayout());
    setUpFormats();/*from   ww w.ja va  2  s  . c o  m*/
    double payment = computePayment(amount, rate, numPeriods);

    // Create the labels.
    amountLabel = new JLabel(amountString);
    rateLabel = new JLabel(rateString);
    numPeriodsLabel = new JLabel(numPeriodsString);
    paymentLabel = new JLabel(paymentString);

    // Create the text fields and set them up.
    amountField = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(amountDisplayFormat),
            new NumberFormatter(amountDisplayFormat), new NumberFormatter(amountEditFormat)));
    amountField.setValue(new Double(amount));
    amountField.setColumns(10);
    amountField.addPropertyChangeListener("value", this);

    NumberFormatter percentEditFormatter = new NumberFormatter(percentEditFormat) {
        public String valueToString(Object o) throws ParseException {
            Number number = (Number) o;
            if (number != null) {
                double d = number.doubleValue() * 100.0;
                number = new Double(d);
            }
            return super.valueToString(number);
        }

        public Object stringToValue(String s) throws ParseException {
            Number number = (Number) super.stringToValue(s);
            if (number != null) {
                double d = number.doubleValue() / 100.0;
                number = new Double(d);
            }
            return number;
        }
    };
    rateField = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(percentDisplayFormat),
            new NumberFormatter(percentDisplayFormat), percentEditFormatter));
    rateField.setValue(new Double(rate));
    rateField.setColumns(10);
    rateField.addPropertyChangeListener("value", this);

    numPeriodsField = new JFormattedTextField();
    numPeriodsField.setValue(new Integer(numPeriods));
    numPeriodsField.setColumns(10);
    numPeriodsField.addPropertyChangeListener("value", this);

    paymentField = new JFormattedTextField(paymentFormat);
    paymentField.setValue(new Double(payment));
    paymentField.setColumns(10);
    paymentField.setEditable(false);
    paymentField.setForeground(Color.red);

    // Tell accessibility tools about label/textfield pairs.
    amountLabel.setLabelFor(amountField);
    rateLabel.setLabelFor(rateField);
    numPeriodsLabel.setLabelFor(numPeriodsField);
    paymentLabel.setLabelFor(paymentField);

    // Lay out the labels in a panel.
    JPanel labelPane = new JPanel(new GridLayout(0, 1));
    labelPane.add(amountLabel);
    labelPane.add(rateLabel);
    labelPane.add(numPeriodsLabel);
    labelPane.add(paymentLabel);

    // Layout the text fields in a panel.
    JPanel fieldPane = new JPanel(new GridLayout(0, 1));
    fieldPane.add(amountField);
    fieldPane.add(rateField);
    fieldPane.add(numPeriodsField);
    fieldPane.add(paymentField);

    // Put the panels in this panel, labels on left,
    // text fields on right.
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    add(labelPane, BorderLayout.CENTER);
    add(fieldPane, BorderLayout.LINE_END);
}

From source file:FormatTest.java

public FormatTestFrame() {
    setTitle("FormatTest");
    setSize(WIDTH, HEIGHT);/*from  w w  w  .ja v a  2  s.c o  m*/

    JPanel buttonPanel = new JPanel();
    okButton = new JButton("Ok");
    buttonPanel.add(okButton);
    add(buttonPanel, BorderLayout.SOUTH);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(0, 3));
    add(mainPanel, BorderLayout.CENTER);

    JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField.setValue(new Integer(100));
    addRow("Number:", intField);

    JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField2.setValue(new Integer(100));
    intField2.setFocusLostBehavior(JFormattedTextField.COMMIT);
    addRow("Number (Commit behavior):", intField2);

    JFormattedTextField intField3 = new JFormattedTextField(
            new InternationalFormatter(NumberFormat.getIntegerInstance()) {
                protected DocumentFilter getDocumentFilter() {
                    return filter;
                }

                private DocumentFilter filter = new IntFilter();
            });
    intField3.setValue(new Integer(100));
    addRow("Filtered Number", intField3);

    JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField4.setValue(new Integer(100));
    intField4.setInputVerifier(new FormattedTextFieldVerifier());
    addRow("Verified Number:", intField4);

    JFormattedTextField currencyField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
    currencyField.setValue(new Double(10));
    addRow("Currency:", currencyField);

    JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance());
    dateField.setValue(new Date());
    addRow("Date (default):", dateField);

    DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
    format.setLenient(false);
    JFormattedTextField dateField2 = new JFormattedTextField(format);
    dateField2.setValue(new Date());
    addRow("Date (short, not lenient):", dateField2);

    try {
        DefaultFormatter formatter = new DefaultFormatter();
        formatter.setOverwriteMode(false);
        JFormattedTextField urlField = new JFormattedTextField(formatter);
        urlField.setValue(new URL("http://java.sun.com"));
        addRow("URL:", urlField);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        MaskFormatter formatter = new MaskFormatter("###-##-####");
        formatter.setPlaceholderCharacter('0');
        JFormattedTextField ssnField = new JFormattedTextField(formatter);
        ssnField.setValue("078-05-1120");
        addRow("SSN Mask:", ssnField);
    } catch (ParseException exception) {
        exception.printStackTrace();
    }

    JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter());
    ipField.setValue(new byte[] { (byte) 130, 65, 86, 66 });
    addRow("IP Address:", ipField);
}

From source file:components.TextSamplerDemo.java

public TextSamplerDemo() {
    setLayout(new BorderLayout());

    //Create a regular text field.
    JTextField textField = new JTextField(10);
    textField.setActionCommand(textFieldString);
    textField.addActionListener(this);

    //Create a password field.
    JPasswordField passwordField = new JPasswordField(10);
    passwordField.setActionCommand(passwordFieldString);
    passwordField.addActionListener(this);

    //Create a formatted text field.
    JFormattedTextField ftf = new JFormattedTextField(java.util.Calendar.getInstance().getTime());
    ftf.setActionCommand(textFieldString);
    ftf.addActionListener(this);

    //Create some labels for the fields.
    JLabel textFieldLabel = new JLabel(textFieldString + ": ");
    textFieldLabel.setLabelFor(textField);
    JLabel passwordFieldLabel = new JLabel(passwordFieldString + ": ");
    passwordFieldLabel.setLabelFor(passwordField);
    JLabel ftfLabel = new JLabel(ftfString + ": ");
    ftfLabel.setLabelFor(ftf);//from  w w  w .j a v a  2s  .c o m

    //Create a label to put messages during an action event.
    actionLabel = new JLabel("Type text in a field and press Enter.");
    actionLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));

    //Lay out the text controls and the labels.
    JPanel textControlsPane = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

    textControlsPane.setLayout(gridbag);

    JLabel[] labels = { textFieldLabel, passwordFieldLabel, ftfLabel };
    JTextField[] textFields = { textField, passwordField, ftf };
    addLabelTextRows(labels, textFields, gridbag, textControlsPane);

    c.gridwidth = GridBagConstraints.REMAINDER; //last
    c.anchor = GridBagConstraints.WEST;
    c.weightx = 1.0;
    textControlsPane.add(actionLabel, c);
    textControlsPane.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Text Fields"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Create a text area.
    JTextArea textArea = new JTextArea("This is an editable JTextArea. "
            + "A text area is a \"plain\" text component, " + "which means that although it can display text "
            + "in any font, all of the text is in the same font.");
    textArea.setFont(new Font("Serif", Font.ITALIC, 16));
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    JScrollPane areaScrollPane = new JScrollPane(textArea);
    areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    areaScrollPane.setPreferredSize(new Dimension(250, 250));
    areaScrollPane.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Plain Text"),
                    BorderFactory.createEmptyBorder(5, 5, 5, 5)),
            areaScrollPane.getBorder()));

    //Create an editor pane.
    JEditorPane editorPane = createEditorPane();
    JScrollPane editorScrollPane = new JScrollPane(editorPane);
    editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    editorScrollPane.setPreferredSize(new Dimension(250, 145));
    editorScrollPane.setMinimumSize(new Dimension(10, 10));

    //Create a text pane.
    JTextPane textPane = createTextPane();
    JScrollPane paneScrollPane = new JScrollPane(textPane);
    paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    paneScrollPane.setPreferredSize(new Dimension(250, 155));
    paneScrollPane.setMinimumSize(new Dimension(10, 10));

    //Put the editor pane and the text pane in a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, editorScrollPane, paneScrollPane);
    splitPane.setOneTouchExpandable(true);
    splitPane.setResizeWeight(0.5);
    JPanel rightPane = new JPanel(new GridLayout(1, 0));
    rightPane.add(splitPane);
    rightPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Styled Text"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Put everything together.
    JPanel leftPane = new JPanel(new BorderLayout());
    leftPane.add(textControlsPane, BorderLayout.PAGE_START);
    leftPane.add(areaScrollPane, BorderLayout.CENTER);

    add(leftPane, BorderLayout.LINE_START);
    add(rightPane, BorderLayout.LINE_END);
}

From source file:components.ConversionPanel.java

ConversionPanel(Converter myController, String myTitle, Unit[] myUnits, ConverterRangeModel myModel) {
    if (MULTICOLORED) {
        setOpaque(true);/*from  w  ww . j av a 2 s .co m*/
        setBackground(new Color(0, 255, 255));
    }
    setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(myTitle),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Save arguments in instance variables.
    controller = myController;
    units = myUnits;
    title = myTitle;
    sliderModel = myModel;

    //Create the text field format, and then the text field.
    numberFormat = NumberFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(2);
    NumberFormatter formatter = new NumberFormatter(numberFormat);
    formatter.setAllowsInvalid(false);
    formatter.setCommitsOnValidEdit(true);//seems to be a no-op --
    //aha -- it changes the value property but doesn't cause the result to
    //be parsed (that happens on focus loss/return, I think).
    //
    textField = new JFormattedTextField(formatter);
    textField.setColumns(10);
    textField.setValue(new Double(sliderModel.getDoubleValue()));
    textField.addPropertyChangeListener(this);

    //Add the combo box.
    unitChooser = new JComboBox();
    for (int i = 0; i < units.length; i++) { //Populate it.
        unitChooser.addItem(units[i].description);
    }
    unitChooser.setSelectedIndex(0);
    sliderModel.setMultiplier(units[0].multiplier);
    unitChooser.addActionListener(this);

    //Add the slider.
    slider = new JSlider(sliderModel);
    sliderModel.addChangeListener(this);

    //Make the text field/slider group a fixed size
    //to make stacked ConversionPanels nicely aligned.
    JPanel unitGroup = new JPanel() {
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        public Dimension getPreferredSize() {
            return new Dimension(150, super.getPreferredSize().height);
        }

        public Dimension getMaximumSize() {
            return getPreferredSize();
        }
    };
    unitGroup.setLayout(new BoxLayout(unitGroup, BoxLayout.PAGE_AXIS));
    if (MULTICOLORED) {
        unitGroup.setOpaque(true);
        unitGroup.setBackground(new Color(0, 0, 255));
    }
    unitGroup.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
    unitGroup.add(textField);
    unitGroup.add(slider);

    //Create a subpanel so the combo box isn't too tall
    //and is sufficiently wide.
    JPanel chooserPanel = new JPanel();
    chooserPanel.setLayout(new BoxLayout(chooserPanel, BoxLayout.PAGE_AXIS));
    if (MULTICOLORED) {
        chooserPanel.setOpaque(true);
        chooserPanel.setBackground(new Color(255, 0, 255));
    }
    chooserPanel.add(unitChooser);
    chooserPanel.add(Box.createHorizontalStrut(100));

    //Put everything together.
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    add(unitGroup);
    add(chooserPanel);
    unitGroup.setAlignmentY(TOP_ALIGNMENT);
    chooserPanel.setAlignmentY(TOP_ALIGNMENT);
}

From source file:FormattedTextFieldDemo.java

public FormattedTextFieldDemo() {
    super(new BorderLayout());
    setUpFormats();/*  w  ww. ja  v  a2  s. c  o m*/
    double payment = computePayment(amount, rate, numPeriods);

    // Create the labels.
    amountLabel = new JLabel(amountString);
    rateLabel = new JLabel(rateString);
    numPeriodsLabel = new JLabel(numPeriodsString);
    paymentLabel = new JLabel(paymentString);

    // Create the text fields and set them up.
    amountField = new JFormattedTextField(amountFormat);
    amountField.setValue(new Double(amount));
    amountField.setColumns(10);
    amountField.addPropertyChangeListener("value", this);

    rateField = new JFormattedTextField(percentFormat);
    rateField.setValue(new Double(rate));
    rateField.setColumns(10);
    rateField.addPropertyChangeListener("value", this);

    numPeriodsField = new JFormattedTextField();
    numPeriodsField.setValue(new Integer(numPeriods));
    numPeriodsField.setColumns(10);
    numPeriodsField.addPropertyChangeListener("value", this);

    paymentField = new JFormattedTextField(paymentFormat);
    paymentField.setValue(new Double(payment));
    paymentField.setColumns(10);
    paymentField.setEditable(false);
    paymentField.setForeground(Color.red);

    // Tell accessibility tools about label/textfield pairs.
    amountLabel.setLabelFor(amountField);
    rateLabel.setLabelFor(rateField);
    numPeriodsLabel.setLabelFor(numPeriodsField);
    paymentLabel.setLabelFor(paymentField);

    // Lay out the labels in a panel.
    JPanel labelPane = new JPanel(new GridLayout(0, 1));
    labelPane.add(amountLabel);
    labelPane.add(rateLabel);
    labelPane.add(numPeriodsLabel);
    labelPane.add(paymentLabel);

    // Layout the text fields in a panel.
    JPanel fieldPane = new JPanel(new GridLayout(0, 1));
    fieldPane.add(amountField);
    fieldPane.add(rateField);
    fieldPane.add(numPeriodsField);
    fieldPane.add(paymentField);

    // Put the panels in this panel, labels on left,
    // text fields on right.
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    add(labelPane, BorderLayout.CENTER);
    add(fieldPane, BorderLayout.LINE_END);
}

From source file:FormattedTextFieldDemo.java

public FormattedTextFieldDemo() {
    super(new BorderLayout());
    setUpFormats();//w w w .j av  a2  s.co m
    double payment = computePayment(amount, rate, numPeriods);

    //Create the labels.
    amountLabel = new JLabel(amountString);
    rateLabel = new JLabel(rateString);
    numPeriodsLabel = new JLabel(numPeriodsString);
    paymentLabel = new JLabel(paymentString);

    //Create the text fields and set them up.
    amountField = new JFormattedTextField(amountFormat);
    amountField.setValue(new Double(amount));
    amountField.setColumns(10);
    amountField.addPropertyChangeListener("value", this);

    rateField = new JFormattedTextField(percentFormat);
    rateField.setValue(new Double(rate));
    rateField.setColumns(10);
    rateField.addPropertyChangeListener("value", this);

    numPeriodsField = new JFormattedTextField();
    numPeriodsField.setValue(new Integer(numPeriods));
    numPeriodsField.setColumns(10);
    numPeriodsField.addPropertyChangeListener("value", this);

    paymentField = new JFormattedTextField(paymentFormat);
    paymentField.setValue(new Double(payment));
    paymentField.setColumns(10);
    paymentField.setEditable(false);
    paymentField.setForeground(Color.red);

    //Tell accessibility tools about label/textfield pairs.
    amountLabel.setLabelFor(amountField);
    rateLabel.setLabelFor(rateField);
    numPeriodsLabel.setLabelFor(numPeriodsField);
    paymentLabel.setLabelFor(paymentField);

    //Lay out the labels in a panel.
    JPanel labelPane = new JPanel(new GridLayout(0, 1));
    labelPane.add(amountLabel);
    labelPane.add(rateLabel);
    labelPane.add(numPeriodsLabel);
    labelPane.add(paymentLabel);

    //Layout the text fields in a panel.
    JPanel fieldPane = new JPanel(new GridLayout(0, 1));
    fieldPane.add(amountField);
    fieldPane.add(rateField);
    fieldPane.add(numPeriodsField);
    fieldPane.add(paymentField);

    //Put the panels in this panel, labels on left,
    //text fields on right.
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    add(labelPane, BorderLayout.CENTER);
    add(fieldPane, BorderLayout.LINE_END);
}

From source file:dbseer.gui.panel.DBSeerMiddlewarePanel.java

private void initializeGUI() {
    this.setLayout(new MigLayout());

    JLabel ipAddressLabel = new JLabel("IP Address:");
    JLabel portLabel = new JLabel("Port:");
    JLabel idLabel = new JLabel("ID:");
    JLabel passwordLabel = new JLabel("Password:");

    ipField = new JTextField(20);

    DecimalFormat portFormatter = new DecimalFormat();

    portFormatter.setMaximumFractionDigits(0);
    portFormatter.setMaximumIntegerDigits(5);
    portFormatter.setMinimumIntegerDigits(1);
    portFormatter.setDecimalSeparatorAlwaysShown(false);
    portFormatter.setGroupingUsed(false);

    portField = new JFormattedTextField(portFormatter);
    portField.setColumns(6);/*from ww w.j  a  va 2s.com*/
    portField.setText("3555"); // default port.
    idField = new JTextField(20);
    passwordField = new JPasswordField(20);

    logInOutButton = new JButton("Login");
    logInOutButton.addActionListener(this);
    startMonitoringButton = new JButton("Start Monitoring");
    startMonitoringButton.addActionListener(this);
    stopMonitoringButton = new JButton("Stop Monitoring");
    stopMonitoringButton.addActionListener(this);

    startMonitoringButton.setEnabled(true);
    stopMonitoringButton.setEnabled(false);

    ipField.setText(DBSeerGUI.userSettings.getLastMiddlewareIP());
    portField.setText(String.valueOf(DBSeerGUI.userSettings.getLastMiddlewarePort()));
    idField.setText(DBSeerGUI.userSettings.getLastMiddlewareID());

    NumberFormatter formatter = new NumberFormatter(NumberFormat.getIntegerInstance());
    formatter.setMinimum(1);
    formatter.setMaximum(120);
    formatter.setAllowsInvalid(false);

    refreshRateLabel = new JLabel("Monitoring Refresh Rate:");
    refreshRateField = new JFormattedTextField(formatter);
    JLabel refreshRateRangeLabel = new JLabel("(1~120 sec)");

    refreshRateField.setText("1");
    applyRefreshRateButton = new JButton("Apply");
    applyRefreshRateButton.addActionListener(this);

    this.add(ipAddressLabel, "cell 0 0 2 1, split 4");
    this.add(ipField);
    this.add(portLabel);
    this.add(portField);
    this.add(idLabel, "cell 0 2");
    this.add(idField, "cell 1 2");
    this.add(passwordLabel, "cell 0 3");
    this.add(passwordField, "cell 1 3");
    this.add(refreshRateLabel, "cell 0 4");
    this.add(refreshRateField, "cell 1 4, growx, split 3");
    this.add(refreshRateRangeLabel);
    this.add(applyRefreshRateButton, "growx, wrap");
    //      this.add(logInOutButton, "cell 0 2 2 1, growx, split 3");
    this.add(startMonitoringButton);
    this.add(stopMonitoringButton);
}

From source file:net.sf.mzmine.util.dialogs.AxesSetupDialog.java

/**
 * Constructor/*from w w  w . j a  va  2  s . c  o  m*/
 */
public AxesSetupDialog(XYPlot plot) {

    // Make dialog modal
    super(MZmineCore.getDesktop().getMainWindow(), true);

    xAxis = plot.getDomainAxis();
    yAxis = plot.getRangeAxis();

    NumberFormat defaultFormatter = NumberFormat.getNumberInstance();
    NumberFormat xAxisFormatter = defaultFormatter;
    if (xAxis instanceof NumberAxis)
        xAxisFormatter = ((NumberAxis) xAxis).getNumberFormatOverride();
    NumberFormat yAxisFormatter = defaultFormatter;
    if (yAxis instanceof NumberAxis)
        yAxisFormatter = ((NumberAxis) yAxis).getNumberFormatOverride();

    // Create labels and fields
    JLabel lblXTitle = new JLabel(xAxis.getLabel());
    JLabel lblXAutoRange = new JLabel("Auto range");
    JLabel lblXMin = new JLabel("Minimum");
    JLabel lblXMax = new JLabel("Maximum");
    JLabel lblXAutoTick = new JLabel("Auto tick size");
    JLabel lblXTick = new JLabel("Tick size");

    JLabel lblYTitle = new JLabel(yAxis.getLabel());
    JLabel lblYAutoRange = new JLabel("Auto range");
    JLabel lblYMin = new JLabel("Minimum");
    JLabel lblYMax = new JLabel("Maximum");
    JLabel lblYAutoTick = new JLabel("Auto tick size");
    JLabel lblYTick = new JLabel("Tick size");

    checkXAutoRange = new JCheckBox();
    checkXAutoRange.addActionListener(this);
    checkXAutoTick = new JCheckBox();
    checkXAutoTick.addActionListener(this);
    fieldXMin = new JFormattedTextField(xAxisFormatter);
    fieldXMax = new JFormattedTextField(xAxisFormatter);
    fieldXTick = new JFormattedTextField(xAxisFormatter);

    checkYAutoRange = new JCheckBox();
    checkYAutoRange.addActionListener(this);
    checkYAutoTick = new JCheckBox();
    checkYAutoTick.addActionListener(this);
    fieldYMin = new JFormattedTextField(yAxisFormatter);
    fieldYMax = new JFormattedTextField(yAxisFormatter);
    fieldYTick = new JFormattedTextField(yAxisFormatter);

    // Create a panel for labels and fields
    JPanel pnlLabelsAndFields = new JPanel(new GridLayout(0, 2));

    pnlLabelsAndFields.add(lblXTitle);
    pnlLabelsAndFields.add(new JPanel());
    pnlLabelsAndFields.add(lblXAutoRange);
    pnlLabelsAndFields.add(checkXAutoRange);
    pnlLabelsAndFields.add(lblXMin);
    pnlLabelsAndFields.add(fieldXMin);
    pnlLabelsAndFields.add(lblXMax);
    pnlLabelsAndFields.add(fieldXMax);
    if (xAxis instanceof NumberAxis) {
        pnlLabelsAndFields.add(lblXAutoTick);
        pnlLabelsAndFields.add(checkXAutoTick);
        pnlLabelsAndFields.add(lblXTick);
        pnlLabelsAndFields.add(fieldXTick);
    }

    // Empty row
    pnlLabelsAndFields.add(new JPanel());
    pnlLabelsAndFields.add(new JPanel());

    pnlLabelsAndFields.add(lblYTitle);
    pnlLabelsAndFields.add(new JPanel());
    pnlLabelsAndFields.add(lblYAutoRange);
    pnlLabelsAndFields.add(checkYAutoRange);
    pnlLabelsAndFields.add(lblYMin);
    pnlLabelsAndFields.add(fieldYMin);
    pnlLabelsAndFields.add(lblYMax);
    pnlLabelsAndFields.add(fieldYMax);
    if (yAxis instanceof NumberAxis) {
        pnlLabelsAndFields.add(lblYAutoTick);
        pnlLabelsAndFields.add(checkYAutoTick);
        pnlLabelsAndFields.add(lblYTick);
        pnlLabelsAndFields.add(fieldYTick);
    }

    // Create buttons
    JPanel pnlButtons = new JPanel();
    btnOK = new JButton("OK");
    btnOK.addActionListener(this);
    btnApply = new JButton("Apply");
    btnApply.addActionListener(this);
    btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(this);

    pnlButtons.add(btnOK);
    pnlButtons.add(btnApply);
    pnlButtons.add(btnCancel);

    // Put everything into a main panel
    JPanel pnlAll = new JPanel(new BorderLayout());
    pnlAll.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    add(pnlAll);

    pnlAll.add(pnlLabelsAndFields, BorderLayout.CENTER);
    pnlAll.add(pnlButtons, BorderLayout.SOUTH);

    pack();

    setTitle("Please set ranges for axes");
    setResizable(false);
    setLocationRelativeTo(MZmineCore.getDesktop().getMainWindow());

    getValuesToControls();

}

From source file:guineu.util.dialogs.AxesSetupDialog.java

/**
 * Constructor/*from ww  w .  j  av a  2 s .  co  m*/
 */
public AxesSetupDialog(XYPlot plot) {

    // Make dialog modal
    super(GuineuCore.getDesktop().getMainFrame(), true);

    xAxis = plot.getDomainAxis();
    yAxis = plot.getRangeAxis();

    NumberFormat defaultFormatter = NumberFormat.getNumberInstance();
    NumberFormat xAxisFormatter = defaultFormatter;
    if (xAxis instanceof NumberAxis) {
        xAxisFormatter = ((NumberAxis) xAxis).getNumberFormatOverride();
    }
    NumberFormat yAxisFormatter = defaultFormatter;
    if (yAxis instanceof NumberAxis) {
        yAxisFormatter = ((NumberAxis) yAxis).getNumberFormatOverride();
    }

    // Create labels and fields
    JLabel lblXTitle = new JLabel(xAxis.getLabel());
    JLabel lblXAutoRange = new JLabel("Auto range");
    JLabel lblXMin = new JLabel("Minimum");
    JLabel lblXMax = new JLabel("Maximum");
    JLabel lblXAutoTick = new JLabel("Auto tick size");
    JLabel lblXTick = new JLabel("Tick size");

    JLabel lblYTitle = new JLabel(yAxis.getLabel());
    JLabel lblYAutoRange = new JLabel("Auto range");
    JLabel lblYMin = new JLabel("Minimum");
    JLabel lblYMax = new JLabel("Maximum");
    JLabel lblYAutoTick = new JLabel("Auto tick size");
    JLabel lblYTick = new JLabel("Tick size");

    checkXAutoRange = new JCheckBox();
    checkXAutoRange.addActionListener(this);
    checkXAutoTick = new JCheckBox();
    checkXAutoTick.addActionListener(this);
    fieldXMin = new JFormattedTextField(xAxisFormatter);
    fieldXMax = new JFormattedTextField(xAxisFormatter);
    fieldXTick = new JFormattedTextField(xAxisFormatter);

    checkYAutoRange = new JCheckBox();
    checkYAutoRange.addActionListener(this);
    checkYAutoTick = new JCheckBox();
    checkYAutoTick.addActionListener(this);
    fieldYMin = new JFormattedTextField(yAxisFormatter);
    fieldYMax = new JFormattedTextField(yAxisFormatter);
    fieldYTick = new JFormattedTextField(yAxisFormatter);

    // Create a panel for labels and fields
    JPanel pnlLabelsAndFields = new JPanel(new GridLayout(0, 2));

    pnlLabelsAndFields.add(lblXTitle);
    pnlLabelsAndFields.add(new JPanel());
    pnlLabelsAndFields.add(lblXAutoRange);
    pnlLabelsAndFields.add(checkXAutoRange);
    pnlLabelsAndFields.add(lblXMin);
    pnlLabelsAndFields.add(fieldXMin);
    pnlLabelsAndFields.add(lblXMax);
    pnlLabelsAndFields.add(fieldXMax);
    if (xAxis instanceof NumberAxis) {
        pnlLabelsAndFields.add(lblXAutoTick);
        pnlLabelsAndFields.add(checkXAutoTick);
        pnlLabelsAndFields.add(lblXTick);
        pnlLabelsAndFields.add(fieldXTick);
    }

    // Empty row
    pnlLabelsAndFields.add(new JPanel());
    pnlLabelsAndFields.add(new JPanel());

    pnlLabelsAndFields.add(lblYTitle);
    pnlLabelsAndFields.add(new JPanel());
    pnlLabelsAndFields.add(lblYAutoRange);
    pnlLabelsAndFields.add(checkYAutoRange);
    pnlLabelsAndFields.add(lblYMin);
    pnlLabelsAndFields.add(fieldYMin);
    pnlLabelsAndFields.add(lblYMax);
    pnlLabelsAndFields.add(fieldYMax);
    if (yAxis instanceof NumberAxis) {
        pnlLabelsAndFields.add(lblYAutoTick);
        pnlLabelsAndFields.add(checkYAutoTick);
        pnlLabelsAndFields.add(lblYTick);
        pnlLabelsAndFields.add(fieldYTick);
    }

    // Create buttons
    JPanel pnlButtons = new JPanel();
    btnOK = new JButton("OK");
    btnOK.addActionListener(this);
    btnApply = new JButton("Apply");
    btnApply.addActionListener(this);
    btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(this);

    pnlButtons.add(btnOK);
    pnlButtons.add(btnApply);
    pnlButtons.add(btnCancel);

    // Put everything into a main panel
    JPanel pnlAll = new JPanel(new BorderLayout());
    pnlAll.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    add(pnlAll);

    pnlAll.add(pnlLabelsAndFields, BorderLayout.CENTER);
    pnlAll.add(pnlButtons, BorderLayout.SOUTH);

    pack();

    setTitle("Please set ranges for axes");
    setResizable(false);
    setLocationRelativeTo(GuineuCore.getDesktop().getMainFrame());

    getValuesToControls();

}