Example usage for javax.swing JTextField JTextField

List of usage examples for javax.swing JTextField JTextField

Introduction

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

Prototype

public JTextField(int columns) 

Source Link

Document

Constructs a new empty TextField with the specified number of columns.

Usage

From source file:net.minder.KnoxWebHdfsJavaClientExamplesTest.java

@BeforeClass
public static void setupSuite() {
    Console console = System.console();
    if (console != null) {
        console.printf("Knox Host: ");
        KNOX_HOST = console.readLine();/*  w  w w  . j a  va 2s .com*/
        console.printf("Topology : ");
        TOPOLOGY_PATH = console.readLine();
        console.printf("Username : ");
        TEST_USERNAME = console.readLine();
        console.printf("Password: ");
        TEST_PASSWORD = new String(console.readPassword());
    } else {
        JLabel label = new JLabel("Enter Knox host, topology, username, password:");
        JTextField host = new JTextField(KNOX_HOST);
        JTextField topology = new JTextField(TOPOLOGY_PATH);
        JTextField username = new JTextField(TEST_USERNAME);
        JPasswordField password = new JPasswordField(TEST_PASSWORD);
        int choice = JOptionPane.showConfirmDialog(null,
                new Object[] { label, host, topology, username, password }, "Credentials",
                JOptionPane.OK_CANCEL_OPTION);
        assertThat(choice, is(JOptionPane.YES_OPTION));
        TEST_USERNAME = username.getText();
        TEST_PASSWORD = new String(password.getPassword());
        KNOX_HOST = host.getText();
        TOPOLOGY_PATH = topology.getText();
    }
    TOPOLOGY_URL = String.format("%s://%s:%d/%s/%s", KNOX_SCHEME, KNOX_HOST, KNOX_PORT, KNOX_PATH,
            TOPOLOGY_PATH);
    WEBHDFS_URL = String.format("%s/%s", TOPOLOGY_URL, WEBHDFS_PATH);
}

From source file:EditorPaneExample3.java

public EditorPaneExample3() {
    super("JEditorPane Example 3");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;/*from   w  w w  .  jav  a2  s  .  com*/
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 1;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;

    textField = new JTextField(32);
    panel.add(textField, c);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);

    getContentPane().add(panel, "South");

    // Change page based on text field
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            String url = textField.getText();
            try {
                // Try to display the page
                loadingState.setText("Loading...");
                loadingState.paintImmediately(0, 0, loadingState.getSize().width,
                        loadingState.getSize().height);
                loadedType.setText("");
                loadedType.paintImmediately(0, 0, loadedType.getSize().width, loadedType.getSize().height);
                pane.setPage(url);

                loadingState.setText("Loaded");
                loadedType.setText(pane.getContentType());
            } catch (IOException e) {
                JOptionPane.showMessageDialog(pane, new String[] { "Unable to open file", url },
                        "File Open Error", JOptionPane.ERROR_MESSAGE);
                loadingState.setText("Failed");
            }
        }
    });
}

From source file:SpringCompactGrid.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 *///from ww  w  .  ja  v a 2 s.c  o m
private static void createAndShowGUI() {
    JPanel panel = new JPanel(new SpringLayout());

    int rows = 10;
    int cols = 10;
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            int anInt = (int) Math.pow(r, c);
            JTextField textField = new JTextField(Integer.toString(anInt));
            panel.add(textField);
        }
    }

    //Lay out the panel.
    SpringUtilities.makeCompactGrid(panel, //parent
            rows, cols, 3, 3, //initX, initY
            3, 3); //xPad, yPad

    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("SpringCompactGrid");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    panel.setOpaque(true); //content panes must be opaque
    frame.setContentPane(panel);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:JTextFieldFilter.java

public Main() {
    JTextField tf1b;//from  ww w . java2s. co  m
    JLabel l1b;

    setLayout(new FlowLayout());

    l1b = new JLabel("only float");
    tf1b = new JTextField(10);
    getContentPane().add(l1b);
    getContentPane().add(tf1b);
    tf1b.setDocument(new JTextFieldFilter(JTextFieldFilter.FLOAT));
    setSize(300, 300);
    setVisible(true);
}

From source file:Wallpaper.java

private static JPanel createPanel() {
    JPanel p = new JPanel();

    ButtonGroup entreeGroup = new ButtonGroup();
    JRadioButton radioButton;/*from   ww w.j  a v a2s. co m*/
    p.add(radioButton = new JRadioButton("Beef", true));
    entreeGroup.add(radioButton);
    p.add(radioButton = new JRadioButton("Chicken"));
    entreeGroup.add(radioButton);
    p.add(radioButton = new JRadioButton("Vegetable"));
    entreeGroup.add(radioButton);

    p.add(new JCheckBox("Ketchup"));
    p.add(new JCheckBox("Mustard"));
    p.add(new JCheckBox("Pickles"));

    p.add(new JLabel("Special requests:"));
    p.add(new JTextField(20));

    JButton orderButton = new JButton("Place Order");
    p.add(orderButton);

    return p;
}

From source file:EditorPaneExample4.java

public EditorPaneExample4() {
    super("JEditorPane Example 4");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;/*from   w  w  w  .jav a 2  s . c  o  m*/
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 1;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;

    textField = new JTextField(32);
    panel.add(textField, c);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);

    getContentPane().add(panel, "South");

    // Change page based on text field
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            String url = textField.getText();

            try {
                // Check if the new page and the old
                // page are the same.
                URL newURL = new URL(url);
                URL loadedURL = pane.getPage();
                if (loadedURL != null && loadedURL.sameFile(newURL)) {
                    return;
                }

                // Try to display the page
                textField.setEnabled(false); // Disable input
                textField.paintImmediately(0, 0, textField.getSize().width, textField.getSize().height);
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                // Busy cursor
                loadingState.setText("Loading...");
                loadingState.paintImmediately(0, 0, loadingState.getSize().width,
                        loadingState.getSize().height);
                loadedType.setText("");
                loadedType.paintImmediately(0, 0, loadedType.getSize().width, loadedType.getSize().height);
                pane.setPage(url);

                loadedType.setText(pane.getContentType());
            } catch (Exception e) {
                System.out.println(e);
                JOptionPane.showMessageDialog(pane, new String[] { "Unable to open file", url },
                        "File Open Error", JOptionPane.ERROR_MESSAGE);
                loadingState.setText("Failed");
                textField.setEnabled(true);
                setCursor(Cursor.getDefaultCursor());
            }
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadingState.setText("Page loaded.");
                textField.setEnabled(true); // Allow entry of new URL
                setCursor(Cursor.getDefaultCursor());
            }
        }
    });
}

From source file:JTextFieldFilter.java

public Main() {
    JTextField tf1c;/*from  www  . ja  v  a2s .c  om*/
    JLabel l1c;

    setLayout(new FlowLayout());
    l1c = new JLabel("only float(can be negative)");
    tf1c = new JTextField(10);
    getContentPane().add(l1c);
    getContentPane().add(tf1c);
    JTextFieldFilter jtff = new JTextFieldFilter(JTextFieldFilter.ALPHA_NUMERIC);
    jtff.setNegativeAccepted(true);
    tf1c.setDocument(jtff);

    setSize(300, 300);
    setVisible(true);
}

From source file:components.TextDemo.java

public TextDemo() {
    super(new GridBagLayout());

    textField = new JTextField(20);
    textField.addActionListener(this);

    textArea = new JTextArea(5, 20);
    textArea.setEditable(false);/*from   w w  w. j a  v  a2 s  . co m*/
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Add Components to this panel.
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;

    c.fill = GridBagConstraints.HORIZONTAL;
    add(textField, c);

    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    add(scrollPane, c);
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    listModel.addElement("A");
    listModel.addElement("B");
    listModel.addElement("C");

    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);// w  w  w .  j a v  a2 s.c  om
    list.addListSelectionListener(this);
    list.setVisibleRowCount(5);
    JScrollPane listScrollPane = new JScrollPane(list);

    JButton hireButton = new JButton(addCommand);
    HireListener hireListener = new HireListener(hireButton);
    hireButton.setActionCommand(addCommand);
    hireButton.addActionListener(hireListener);
    hireButton.setEnabled(false);

    fireButton = new JButton(deleteCommand);
    fireButton.setActionCommand(deleteCommand);
    fireButton.addActionListener(new FireListener());

    employeeName = new JTextField(10);
    employeeName.addActionListener(hireListener);
    employeeName.getDocument().addDocumentListener(hireListener);
    String name = listModel.getElementAt(list.getSelectedIndex()).toString();
    System.out.println(name);

    // Create a panel that uses BoxLayout.
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.add(fireButton);
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(employeeName);
    buttonPane.add(hireButton);
    buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    add(listScrollPane, BorderLayout.CENTER);
    add(buttonPane, BorderLayout.PAGE_END);
}

From source file:StringDistance.java

public StringDistance(String[] strs) throws InlineJavaException {
    frame = new JFrame("StringDistance");
    Container p = frame.getContentPane();
    p.setLayout(new GridLayout(0, 2));

    // The input fields, including labels:
    tf = new JTextField[2];
    for (int i = 0; i < 2; i++) {
        p.add(new JLabel("String " + i + ":"));
        tf[i] = new JTextField(20);
        if ((strs != null) && (i < strs.length))
            tf[i].setText(strs[i]);/*from  w w w. j  ava  2  s .  co  m*/
        p.add(tf[i]);
    }

    // The output field, including label:
    p.add(new JLabel("Distance:"));
    dist = new JTextField(5);
    dist.setEditable(false);
    p.add(dist);

    // The main action button:
    go = new JButton("Compute distance");
    go.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            dist.setText(Integer.toString(match(tf[0].getText(), tf[1].getText())));
        }
    });
    p.add(go);

    // To finish off:
    exit = new JButton("Exit");
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            frame.dispose();
            System.exit(0);
        }
    });
    p.add(exit);

    if ((strs != null) && (strs.length > 1))
        dist.setText(Integer.toString(match(tf[0].getText(), tf[1].getText())));
    frame.pack();
}