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:FocusTraversalDemo.java

public FocusTraversalDemo() {
    super(new BorderLayout());
    newPolicy = new MyOwnFocusTraversalPolicy();

    tf1 = new JTextField("Field 1");
    tf2 = new JTextField("A Bigger Field 2");
    tf3 = new JTextField("Field 3");
    tf4 = new JTextField("A Bigger Field 4");
    tf5 = new JTextField("Field 5");
    tf6 = new JTextField("A Bigger Field 6");
    table = new JTable(4, 3);
    togglePolicy = new JCheckBox("Custom FocusTraversalPolicy");
    togglePolicy.setActionCommand("toggle");
    togglePolicy.addActionListener(this);
    togglePolicy.setFocusable(false); //Remove it from the focus cycle.
    //Note that HTML is allowed and will break this run of text
    //across two lines.
    label = new JLabel(
            "<html>Use Tab (or Shift-Tab) to navigate from component to component.<p>Control-Tab (or Control-Shift-Tab) allows you to break out of the JTable.</html>");

    JPanel leftTextPanel = new JPanel(new GridLayout(3, 2));
    leftTextPanel.add(tf1, BorderLayout.PAGE_START);
    leftTextPanel.add(tf3, BorderLayout.CENTER);
    leftTextPanel.add(tf5, BorderLayout.PAGE_END);
    leftTextPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 5));
    JPanel rightTextPanel = new JPanel(new GridLayout(3, 2));
    rightTextPanel.add(tf2, BorderLayout.PAGE_START);
    rightTextPanel.add(tf4, BorderLayout.CENTER);
    rightTextPanel.add(tf6, BorderLayout.PAGE_END);
    rightTextPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 5));
    JPanel tablePanel = new JPanel(new GridLayout(0, 1));
    tablePanel.add(table, BorderLayout.CENTER);
    tablePanel.setBorder(BorderFactory.createEtchedBorder());
    JPanel bottomPanel = new JPanel(new GridLayout(2, 1));
    bottomPanel.add(togglePolicy, BorderLayout.PAGE_START);
    bottomPanel.add(label, BorderLayout.PAGE_END);

    add(leftTextPanel, BorderLayout.LINE_START);
    add(rightTextPanel, BorderLayout.CENTER);
    add(tablePanel, BorderLayout.LINE_END);
    add(bottomPanel, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:com.emental.mindraider.ui.dialogs.AddTripletJDialog.java

/**
 * Constructor./*from  w  w w  .j  a v a 2  s .c o  m*/
 */
public AddTripletJDialog() {
    super("Add Triplet");

    JPanel framePanel = new JPanel();
    framePanel.setLayout(new GridLayout(4, 1));

    JPanel p = new JPanel();
    p.setLayout(new FlowLayout(FlowLayout.RIGHT));
    p.add(new JLabel(" Predicate: "));

    predicateNs = new JTextField(30);
    predicateNs.setText(MindRaiderConstants.MR_RDF_PREDICATE_NS);
    predicateNs.selectAll();
    p.add(predicateNs);
    p.add(new JLabel("#"));
    predicateLocalName = new JTextField(15);
    p.add(predicateLocalName);
    framePanel.add(p);

    p = new JPanel();
    p.setLayout(new FlowLayout(FlowLayout.RIGHT));
    p.add(new JLabel(" Object: "));

    objectNs = new JTextField(30);
    objectNs.setText(MindRaiderConstants.MR_RDF_PREDICATE_NS);
    objectNs.selectAll();
    p.add(objectNs);
    p.add(new JLabel("#"));
    objectLocalName = new JTextField(15);
    objectLocalName.grabFocus();
    objectLocalName.selectAll();
    objectLocalName.addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent keyEvent) {
            if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
                createTriplet();
            }
        }

        public void keyReleased(KeyEvent keyEvent) {
        }

        public void keyTyped(KeyEvent keyEvent) {
        }
    });

    p.add(objectLocalName);
    framePanel.add(p);

    p = new JPanel();
    p.setLayout(new FlowLayout(FlowLayout.RIGHT));
    literalCheckBox = new JCheckBox("literal", false);
    p.add(literalCheckBox);
    framePanel.add(p);

    p = new JPanel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton addButton = new JButton("Add");
    p.add(addButton);
    addButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            createTriplet();
        }
    });

    JButton cancelButton = new JButton("Cancel");
    p.add(cancelButton);
    cancelButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            AddTripletJDialog.this.dispose();
        }
    });
    framePanel.add(p);

    getContentPane().add(framePanel, BorderLayout.CENTER);

    // show
    pack();
    Gfx.centerAndShowWindow(this);

    addWindowListener(new WindowAdapter() {

        public void windowActivated(WindowEvent e) {
            getObjectLocalName().requestFocusInWindow();
        }
    });
}

From source file:FocusEventDemo.java

public void addComponentsToPane(final Container pane) {
    GridBagLayout gridbag = new GridBagLayout();
    pane.setLayout(gridbag);// w ww . j  ava 2  s.  com

    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0; // Make column as wide as possible.
    JTextField textField = new JTextField("A TextField");
    textField.setMargin(new Insets(0, 2, 0, 2));
    textField.addFocusListener(this);
    gridbag.setConstraints(textField, c);
    add(textField);

    c.weightx = 0.1; // Widen every other column a bit, when possible.
    c.fill = GridBagConstraints.NONE;
    JLabel label = new JLabel("A Label");
    label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    label.addFocusListener(this);
    gridbag.setConstraints(label, c);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector<String> vector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    gridbag.setConstraints(comboBox, c);
    add(comboBox);

    c.gridwidth = GridBagConstraints.REMAINDER;
    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    gridbag.setConstraints(button, c);
    add(button);

    c.weightx = 0.0;
    c.weighty = 0.1;
    c.fill = GridBagConstraints.BOTH;
    String listPrefix = "List Item #";
    Vector<String> listVector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); // It's easier to see the focus change
    // if an item is selected.
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    gridbag.setConstraints(listScrollPane, c);
    add(listScrollPane);

    c.weighty = 1.0; // Make this row as tall as possible.
    c.gridheight = GridBagConstraints.REMAINDER;
    // Set up the area that reports focus-gained and focus-lost events.
    display = new JTextArea();
    display.setEditable(false);
    // The setRequestFocusEnabled method prevents a
    // component from being clickable, but it can still
    // get the focus through the keyboard - this ensures
    // user accessibility.
    display.setRequestFocusEnabled(false);
    display.addFocusListener(this);
    JScrollPane displayScrollPane = new JScrollPane(display);

    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);
    setPreferredSize(new Dimension(450, 450));
    ((JPanel) pane).setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:FocusConceptsDemo.java

public FocusConceptsDemo() {
    super(new BorderLayout());

    b1 = new JButton("JButton");
    b2 = new JButton("JButton");
    b3 = new JButton("JButton");
    b4 = new JButton("JButton");
    JPanel buttonPanel = new JPanel(new GridLayout(1, 1));
    buttonPanel.add(b1);//from w w w  .  j a va  2  s.  c o  m
    buttonPanel.add(b2);
    buttonPanel.add(b3);
    buttonPanel.add(b4);

    text1 = new JTextArea("JTextArea", 15, 40);
    JPanel textAreaPanel = new JPanel(new BorderLayout());
    textAreaPanel.add(text1, BorderLayout.CENTER);

    t1 = new JTextField("JTextField");
    t2 = new JTextField("JTextField");
    t3 = new JTextField("JTextField");
    t4 = new JTextField("JTextField");
    JPanel textFieldPanel = new JPanel(new GridLayout(1, 1));
    textFieldPanel.add(t1);
    textFieldPanel.add(t2);
    textFieldPanel.add(t3);
    textFieldPanel.add(t4);

    add(buttonPanel, BorderLayout.PAGE_START);
    add(textAreaPanel, BorderLayout.CENTER);
    add(textFieldPanel, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:EditorPaneExample9.java

public EditorPaneExample9() {
    super("JEditorPane Example 9");
    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  ww. ja  v  a  2 s.c om
    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.gridy = 3;
    panel.add(new JLabel(LOAD_TIME), c);

    c.gridy = 4;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    onlineLoad = new JCheckBox("Online Load");
    panel.add(onlineLoad, c);
    onlineLoad.setSelected(true);
    onlineLoad.setForeground(typeLabel.getForeground());

    c.gridx = 1;
    c.gridy = 0;
    c.anchor = GridBagConstraints.EAST;
    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);
    timeLabel = new JLabel("");
    c.gridy = 3;
    panel.add(timeLabel, 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);

                timeLabel.setText("");
                timeLabel.paintImmediately(0, 0, timeLabel.getSize().width, timeLabel.getSize().height);

                startTime = System.currentTimeMillis();

                // Choose the loading method
                if (onlineLoad.isSelected()) {
                    // Usual load via setPage
                    pane.setPage(url);
                    loadedType.setText(pane.getContentType());
                } else {
                    pane.setContentType("text/html");
                    loadedType.setText(pane.getContentType());
                    if (loader == null) {
                        loader = new HTMLDocumentLoader();
                    }
                    HTMLDocument doc = loader.loadDocument(new URL(url));
                    loadComplete();
                    pane.setDocument(doc);
                    displayLoadTime();
                }
            } 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")) {
                loadComplete();
                displayLoadTime();
            }
        }
    });
}

From source file:net.sf.housekeeper.swing.FoodEditorView.java

/**
 * Initializes a new View. It will be shown centered on the
 * <code>owner</code>./* w  w w.j a  v  a 2 s. co  m*/
 * 
 * @param owner The owner of this View.
 */
FoodEditorView(final Frame owner) {
    final String dialogTitle = LocalisationManager.INSTANCE.getText("gui.foodEditor.dialogTitle");
    dialog = new JDialog(owner, dialogTitle, true);

    nameField = new JTextField(20);
    quantityField = new JTextField(10);
    dateSpinner = createDateSpinner();
    checkbox = createExpiryEnabledCheckbox();

    final String okButtonText = LocalisationManager.INSTANCE.getText("gui.okButton");
    okButton = new JButton(okButtonText);
    okButton.setActionCommand("OK");

    final String cancelButtonText = LocalisationManager.INSTANCE.getText("gui.cancelButton");
    cancelButton = new JButton(cancelButtonText);
    cancelButton.setActionCommand("Cancel");

    initDialog();
}

From source file:net.sf.housekeeper.swing.FoodItemEditorView.java

/**
 * Initializes a new View. It will be shown centered on the
 * <code>owner</code>.//  www  . j  a  v  a 2s  . co m
 * 
 * @param owner The owner of this View.
 */
FoodItemEditorView(final Frame owner) {
    final String dialogTitle = LocalisationManager.INSTANCE.getText("gui.foodItemEditor.dialogTitle");
    dialog = new JDialog(owner, dialogTitle, true);

    nameField = new JTextField(20);
    quantityField = new JTextField(10);
    dateSpinner = createDateSpinner();
    checkbox = createExpiryEnabledCheckbox();

    final String okButtonText = LocalisationManager.INSTANCE.getText("gui.okButton");
    okButton = new JButton(okButtonText);
    okButton.setActionCommand("OK");

    final String cancelButtonText = LocalisationManager.INSTANCE.getText("gui.cancelButton");
    cancelButton = new JButton(cancelButtonText);
    cancelButton.setActionCommand("Cancel");

    initDialog();
}

From source file:XPathTest.java

public XPathFrame() {
        setTitle("XPathTest");

        JMenu fileMenu = new JMenu("File");
        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                openFile();// w  w w. java 2s  . com
            }
        });
        fileMenu.add(openItem);

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        fileMenu.add(exitItem);

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                evaluate();
            }
        };
        expression = new JTextField(20);
        expression.addActionListener(listener);
        JButton evaluateButton = new JButton("Evaluate");
        evaluateButton.addActionListener(listener);

        typeCombo = new JComboBox(new Object[] { "STRING", "NODE", "NODESET", "NUMBER", "BOOLEAN" });
        typeCombo.setSelectedItem("STRING");

        JPanel panel = new JPanel();
        panel.add(expression);
        panel.add(typeCombo);
        panel.add(evaluateButton);
        docText = new JTextArea(10, 40);
        result = new JTextField();
        result.setBorder(new TitledBorder("Result"));

        add(panel, BorderLayout.NORTH);
        add(new JScrollPane(docText), BorderLayout.CENTER);
        add(result, BorderLayout.SOUTH);

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            JOptionPane.showMessageDialog(this, e);
        }

        XPathFactory xpfactory = XPathFactory.newInstance();
        path = xpfactory.newXPath();
        pack();
    }

From source file:FocusEventDemo.java

public FocusEventDemo() {
    super(new GridBagLayout());
    GridBagLayout gridbag = (GridBagLayout) getLayout();
    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0; //Make column as wide as possible.
    JTextField textField = new JTextField("A TextField");
    textField.setMargin(new Insets(0, 2, 0, 2));
    textField.addFocusListener(this);
    gridbag.setConstraints(textField, c);
    add(textField);//  w  w  w.  ja  v a 2 s . co  m

    c.weightx = 0.1; //Widen every other column a bit, when possible.
    c.fill = GridBagConstraints.NONE;
    JLabel label = new JLabel("A Label");
    label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    label.addFocusListener(this);
    gridbag.setConstraints(label, c);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    gridbag.setConstraints(comboBox, c);
    add(comboBox);

    c.gridwidth = GridBagConstraints.REMAINDER;
    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    gridbag.setConstraints(button, c);
    add(button);

    c.weightx = 0.0;
    c.weighty = 0.1;
    c.fill = GridBagConstraints.BOTH;
    String listPrefix = "List Item #";
    Vector listVector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); //It's easier to see the focus change
    //if an item is selected.
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);
    //We want to prevent the list's scroll bars
    //from getting the focus - even with the keyboard.
    //Note that in general we prefer setRequestFocusable
    //over setFocusable for reasons of accessibility,
    //but this is to work around bug #4866958.
    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    gridbag.setConstraints(listScrollPane, c);
    add(listScrollPane);

    c.weighty = 1.0; //Make this row as tall as possible.
    c.gridheight = GridBagConstraints.REMAINDER;
    //Set up the area that reports focus-gained and focus-lost events.
    display = new JTextArea();
    display.setEditable(false);
    //The method setRequestFocusEnabled prevents a
    //component from being clickable, but it can still
    //get the focus through the keyboard - this ensures
    //user accessibility.
    display.setRequestFocusEnabled(false);
    display.addFocusListener(this);
    JScrollPane displayScrollPane = new JScrollPane(display);

    //Work around for bug #4866958.
    displayScrollPane.getHorizontalScrollBar().setFocusable(false);
    displayScrollPane.getVerticalScrollBar().setFocusable(false);
    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);

    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:de.tbuchloh.kiskis.gui.SearchDialog.java

@Override
protected Component createMainPanel() {
    _query = new JTextField(25);
    _query.addActionListener(_findNextAction);
    _query.addKeyListener(new KeyAdapter() {
        @Override/*from   w  w  w.  ja  va 2  s.c o  m*/
        public void keyTyped(final KeyEvent e) {
            _findNextAction.setEnabled(_query.getText().length() > 0);
        }
    });
    final JPanel panel = new JPanel();
    panel.add(new JLabel(Messages.getString("SearchDialog.queryLabel_title"))); //$NON-NLS-1$
    panel.add(_query);
    return panel;
}