List of usage examples for javax.swing JTextField setText
@BeanProperty(bound = false, description = "the text of this component") public void setText(String t)
TextComponent
to the specified text. From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); String[] list = { "Hello 1", "Hello 2", "Hello 3", "Hello 4" }; DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(list); JComboBox<String> comboBox = new JComboBox<>(model); frame.add(comboBox, BorderLayout.NORTH); final JTextField textField = new JTextField(30); frame.add(textField, BorderLayout.SOUTH); frame.add(new JLabel("Type something, then press enter", JLabel.CENTER)); textField.addActionListener(ae -> { String text = textField.getText(); model.addElement(text);/*w ww . jav a 2s .c om*/ comboBox.setSelectedItem(text); textField.setText(""); }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }
From source file:LabelSampleSaveText.java
public static void main(String args[]) { JFrame frame = new JFrame("Label Focus Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Name: "); label.setDisplayedMnemonic(KeyEvent.VK_N); JTextField textField = new JTextField(); label.setLabelFor(textField);// w w w .j av a 2 s .c o m panel.add(label, BorderLayout.WEST); panel.add(textField, BorderLayout.CENTER); frame.add(panel, BorderLayout.NORTH); frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); textField.setText("your text"); String filename = "test.txt"; FileWriter writer = null; try { writer = new FileWriter(filename); textField.write(writer); } catch (IOException exception) { System.err.println("Save oops"); } finally { if (writer != null) { try { writer.close(); } catch (IOException exception) { System.err.println("Error closing writer"); exception.printStackTrace(); } } } }
From source file:Main.java
public static void main(String[] args) { String[] items = { "item1", "item2", "item1" }; JList<String> list = new JList<>(items); JTextField output = new JTextField(15); JPanel gui = new JPanel(); gui.add(list);//from ww w. j av a 2 s . c om gui.add(output); list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent lse) { int index = list.getSelectedIndex(); String outputText = "Index: " + index + " Value: " + items[index]; output.setText(outputText); } }); JOptionPane.showMessageDialog(null, gui); }
From source file:Main.java
public static void main(String args[]) throws Exception { JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Name: "); label.setDisplayedMnemonic(KeyEvent.VK_N); JTextField textField = new JTextField(); label.setLabelFor(textField);/* www . j a va 2s.c om*/ panel.add(label, BorderLayout.WEST); panel.add(textField, BorderLayout.CENTER); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel, BorderLayout.NORTH); frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); textField.setText("your text"); String filename = "test.txt"; FileWriter writer = new FileWriter(filename); textField.write(writer); writer.close(); }
From source file:Main.java
public static void main(String[] args) { ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js"); String[] ops = { "+", "-", "*", "/" }; JPanel gui = new JPanel(new BorderLayout(2, 2)); JPanel labels = new JPanel(new GridLayout(0, 1)); gui.add(labels, BorderLayout.WEST); labels.add(new JLabel("a")); labels.add(new JLabel("operand")); labels.add(new JLabel("b")); labels.add(new JLabel("=")); JPanel controls = new JPanel(new GridLayout(0, 1)); gui.add(controls, BorderLayout.CENTER); JTextField a = new JTextField(10); controls.add(a);//from w w w.j ava 2 s .c o m JComboBox operand = new JComboBox(ops); controls.add(operand); JTextField b = new JTextField(10); controls.add(b); JTextField output = new JTextField(10); controls.add(output); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { String expression = a.getText() + operand.getSelectedItem() + b.getText(); try { Object result = engine.eval(expression); if (result == null) { output.setText("Output was 'null'"); } else { output.setText(result.toString()); } } catch (ScriptException se) { output.setText(se.getMessage()); } } }; operand.addActionListener(al); a.addActionListener(al); b.addActionListener(al); JOptionPane.showMessageDialog(null, gui); }
From source file:Main.java
public static void main(String[] args) { JPanel mainPanel = new JPanel(); JTextField field = new JTextField(20); JTextField field1 = new JTextField(20); field.getDocument().addDocumentListener(new DocumentListener() { @Override//from www. j av a2 s.c o m public void changedUpdate(DocumentEvent e) { updateLabel(e); } @Override public void insertUpdate(DocumentEvent e) { updateLabel(e); } @Override public void removeUpdate(DocumentEvent e) { updateLabel(e); } private void updateLabel(DocumentEvent e) { field1.setText(field.getText()); } }); mainPanel.setLayout(new GridLayout(1, 0, 10, 0)); mainPanel.add(field); mainPanel.add(field1); JFrame frame = new JFrame(); frame.getContentPane().add(mainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }
From source file:CaretSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Caret Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = frame.getContentPane(); JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); content.add(scrollPane, BorderLayout.CENTER); final JTextField dot = new JTextField(); dot.setEditable(false);/*from ww w . j a va2 s .co m*/ JPanel dotPanel = new JPanel(new BorderLayout()); dotPanel.add(new JLabel("Dot: "), BorderLayout.WEST); dotPanel.add(dot, BorderLayout.CENTER); content.add(dotPanel, BorderLayout.NORTH); final JTextField mark = new JTextField(); mark.setEditable(false); JPanel markPanel = new JPanel(new BorderLayout()); markPanel.add(new JLabel("Mark: "), BorderLayout.WEST); markPanel.add(mark, BorderLayout.CENTER); content.add(markPanel, BorderLayout.SOUTH); CaretListener listener = new CaretListener() { public void caretUpdate(CaretEvent caretEvent) { dot.setText("" + caretEvent.getDot()); mark.setText("" + caretEvent.getMark()); } }; textArea.addCaretListener(listener); frame.setSize(250, 150); frame.setVisible(true); }
From source file:LoadSave.java
public static void main(String args[]) { final String filename = "text.out"; JFrame frame = new JFrame("Loading/Saving Example"); Container content = frame.getContentPane(); final JTextField textField = new JTextField(); content.add(textField, BorderLayout.NORTH); JPanel panel = new JPanel(); Action loadAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { try { doLoadCommand(textField, filename); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace();/*from w ww. j av a 2s . c o m*/ } } }; loadAction.putValue(Action.NAME, "Load"); JButton loadButton = new JButton(loadAction); panel.add(loadButton); Action saveAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { try { doSaveCommand(textField, filename); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }; saveAction.putValue(Action.NAME, "Save"); JButton saveButton = new JButton(saveAction); panel.add(saveButton); Action clearAction = new AbstractAction() { { putValue(Action.NAME, "Clear"); } public void actionPerformed(ActionEvent e) { textField.setText(""); } }; JButton clearButton = new JButton(clearAction); panel.add(clearButton); content.add(panel, BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); }
From source file:layout.SpringGrid.java
/** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread./* w w w.j ava 2s .c o m*/ */ private static void createAndShowGUI() { //Create the panel and populate it. JPanel panel = new JPanel(new SpringLayout()); for (int i = 0; i < 9; i++) { JTextField textField = new JTextField(Integer.toString(i)); //Make the 4th field extra big. if (i == 4) { textField.setText("This one is extra long."); } panel.add(textField); } //Lay out the panel. SpringUtilities.makeGrid(panel, 3, 3, //rows, cols 5, 5, //initialX, initialY 5, 5);//xPad, yPad //Create and set up the window. JFrame frame = new JFrame("SpringGrid"); 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:com.SCI.centraltoko.utility.UtilityTools.java
private static void convertToUpperCase(final JTextField textField) { textField.setText(textField.getText().toUpperCase()); }