Example usage for javax.swing JFormattedTextField setColumns

List of usage examples for javax.swing JFormattedTextField setColumns

Introduction

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

Prototype

@BeanProperty(bound = false, description = "the number of columns preferred for display")
public void setColumns(int columns) 

Source Link

Document

Sets the number of columns in this TextField, and then invalidate the layout.

Usage

From source file:MyFormatter.java

public static void main(String[] args) {
    JFormattedTextField tf = new JFormattedTextField(new MyFormatter());
    tf.setColumns(10);
}

From source file:DefaultFormatterFactoryDemo.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Mask Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel("Date");

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DateFormat nullFormat = new SimpleDateFormat("'null'");
    DateFormatter nullFormatter = new DateFormatter(nullFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter, nullFormatter);

    JFormattedTextField input = new JFormattedTextField(factory);
    input.setColumns(30);
    JPanel panel = new JPanel();
    panel.add(label);//  www.j av a  2 s  .c  om
    panel.add(input);
    frame.add(panel, "North");

    frame.add(new JTextField(), "Center");
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(2);//  w  ww  . jav a 2s  . com
    format.setMinimumFractionDigits(2);
    format.setParseIntegerOnly(true);
    format.setRoundingMode(RoundingMode.HALF_UP);

    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMaximum(1000);
    formatter.setMinimum(0.0);
    formatter.setAllowsInvalid(false);
    // formatter.setOverwriteMode(false);

    JFormattedTextField tf = new JFormattedTextField(formatter);
    tf.setColumns(10);
    tf.setValue(123456789.99);
    JFormattedTextField tf1 = new JFormattedTextField(formatter);
    tf1.setValue(1234567890.99);
    JFormattedTextField tf2 = new JFormattedTextField(formatter);
    tf2.setValue(1111.1111);
    JFormattedTextField tf3 = new JFormattedTextField(formatter);
    tf3.setValue(-1111.1111);
    JFormattedTextField tf4 = new JFormattedTextField(formatter);
    tf4.setValue(-56);

    JFrame frame = new JFrame("Test");
    frame.setLayout(new GridLayout(5, 0));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(tf);
    frame.add(tf1);
    frame.add(tf2);
    frame.add(tf3);
    frame.add(tf4);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String... args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPane = new JPanel();

    JFormattedTextField ftf = new JFormattedTextField(NumberFormat.getNumberInstance());
    ftf.setColumns(10);
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
    ftf.setValue(100);/*  w w  w .j  av  a  2s .  c  o  m*/
    lastValidValue = "100";
    ftf.addCaretListener(e -> {
        System.out.println("Last Valid Value : " + lastValidValue);
        if (ftf.isEditValid()) {
            String latestValue = ftf.getText();
            System.out.println("Latest Value : " + latestValue);
            if (!(latestValue.equals(lastValidValue)))
                ftf.setBackground(Color.YELLOW.darker());
            else {
                lastValidValue = ftf.getText();
                ftf.setBackground(Color.WHITE);
            }
        } else {
            System.out.println("Invalid Edit Entered.");
        }
    });
    contentPane.add(ftf);
    frame.setContentPane(contentPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(100, 75);/* www  .j  a v  a 2s . c om*/
    JPanel content = new JPanel(new FlowLayout());
    frame.setContentPane(content);

    MaskFormatter formatter = new MaskFormatter("#");
    formatter.setValidCharacters("123456789");

    JFormattedTextField f1 = new JFormattedTextField(formatter);
    f1.setValue(null);
    f1.setColumns(1);

    content.add(f1);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    DateFormatter[] formatters = new DateFormatter[] { new DateFormatter(new SimpleDateFormat("dd.MM.yyyy")),
            new DateFormatter(new SimpleDateFormat("ddMMyyyy")),
            new DateFormatter(new SimpleDateFormat("yyyy.MM.dd.")),
            new DateFormatter(new SimpleDateFormat("dd.MMM.yyyy")),
            new DateFormatter(new SimpleDateFormat("dd/MM/yyyy")) };
    MultiFormatter<DateFormatter> multiFormatter = new MultiFormatter<>(formatters);

    JFormattedTextField ftf = new JFormattedTextField(multiFormatter);
    ftf.setColumns(10);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(ftf);/*from w ww.  ja va  2 s  .  c om*/
    frame.add(new JButton("OK"));
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat format = new DecimalFormat("####.##");
    format.setMinimumFractionDigits(2);/*  w w  w. ja  v  a  2  s . co  m*/
    final JFormattedTextField field1 = new JFormattedTextField(format);
    final JFormattedTextField field2 = new JFormattedTextField(format);
    field1.setColumns(15);
    field2.setColumns(15);
    JButton btn = new JButton(new AbstractAction("Multiply by 2") {

        @Override
        public void actionPerformed(ActionEvent e) {
            Number value = (Number) field1.getValue();
            if (value != null) {
                field2.setValue(2 * value.doubleValue());
            }
        }
    });

    JPanel panel = new JPanel();
    panel.add(field1);
    panel.add(btn);
    panel.add(field2);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    JFrame frame = new JFrame("Number Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Font font = new Font("SansSerif", Font.BOLD, 16);

    JLabel label;//from www .  ja v a2  s .  co m
    JFormattedTextField input;
    JPanel panel;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    label = new JLabel("Raw Number:");
    input = new JFormattedTextField(2424.50);
    input.setValue(2424.50);
    input.setColumns(20);
    input.setFont(font);
    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel.add(label);
    panel.add(input);
    frame.add(panel);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Number Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Font font = new Font("SansSerif", Font.BOLD, 16);

    JLabel label;//from w ww  .  jav  a 2  s  . c o  m
    JFormattedTextField input;
    JPanel panel;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    label = new JLabel("Raw Number:");
    input = new JFormattedTextField(2424.50);
    input.setValue(2424.50);
    input.setColumns(20);
    input.setFont(font);

    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel.add(label);
    panel.add(input);
    frame.add(panel);

    frame.add(new JTextField());
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Mask Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label;//from ww  w. ja va2 s.  c  o m
    JFormattedTextField input;
    JPanel panel;
    MaskFormatter formatter;

    try {
        label = new JLabel("US Phone");
        formatter = new MaskFormatter("'(###')' ###'-####");
        formatter.setPlaceholderCharacter('*');
        input = new JFormattedTextField(formatter);
        input.setColumns(20);
        panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.add(label);
        panel.add(input);
        frame.add(panel);
    } catch (ParseException e) {
        System.err.println("Unable to add Phone");
    }

    frame.pack();
    frame.setVisible(true);
}