import javax.swing.*; public class JOptionPaneMultiInput { public static void main(String[] args) { JTextField xField = new JTextField(5); JTextField yField = new JTextField(5); JPanel myPanel = new JPanel(); myPanel.add(new JLabel("x:")); myPanel.add(xField); myPanel.add(Box.createHorizontalStrut(15)); // a spacer myPanel.add(new JLabel("y:")); myPanel.add(yField); int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { System.out.println("x value: " + xField.getText()); System.out.println("y value: " + yField.getText()); } } }
private JTextField m_userInputTf = new JTextField(5); private JTextField m_totalTf = new JTextField(20); private JButton m_multiplyBtn = new JButton("Multiply"); private JButton m_clearBtn = new JButton("Clear"); content.add(new JLabel("Input")); content.add(m_userInputTf); content.add(m_multiplyBtn); content.add(new JLabel("Total")); content.add(m_totalTf); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); return m_userInputTf.getText();
mainPanel.add(textAreasPanel, BorderLayout.CENTER); mainPanel.add(buttonsPanel, BorderLayout.PAGE_END); JButton button = new JButton(btnAction); buttonsPanel.add(button); JTextArea textArea = impressionRecMap.get(key); if (textArea != null) { return textArea.getText(); } else { return ""; // throw exception
final JFrame frame = new JFrame(); final JTextField textFieldB = new HintTextField("Another hint here"); frame.add(textFieldA, BorderLayout.NORTH); frame.add(textFieldB, BorderLayout.CENTER); JButton btnGetText = new JButton("Get text"); public void actionPerformed(ActionEvent e) { String message = String.format("textFieldA='%s', textFieldB='%s'", textFieldA.getText(), textFieldB.getText()); JOptionPane.showMessageDialog(frame, message); frame.add(btnGetText, BorderLayout.SOUTH); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); frame.pack();
String[] items = {"One", "Two", "Three", "Four", "Five"}; JComboBox combo = new JComboBox(items); JTextField field1 = new JTextField("1234.56"); JTextField field2 = new JTextField("9876.54"); JPanel panel = new JPanel(new GridLayout(0, 1)); panel.add(combo); panel.add(new JLabel("Field 1:")); panel.add(field1); panel.add(new JLabel("Field 2:")); panel.add(field2); if (result == JOptionPane.OK_OPTION) { System.out.println(combo.getSelectedItem() + " " + field1.getText() + " " + field2.getText()); } else { System.out.println("Cancelled");
= new TableRowSorter<>(jTable.getModel()); private JTextField jtfFilter = new JTextField(); private JButton jbtFilter = new JButton("Filter"); panel.add(new JLabel("Specify a word to match:"), BorderLayout.WEST); panel.add(jtfFilter, BorderLayout.CENTER); add(panel, BorderLayout.SOUTH); add(new JScrollPane(jTable), BorderLayout.CENTER); String text = jtfFilter.getText(); String text = jtfFilter.getText(); SwingUtilities.invokeLater(new Runnable(){ public void run() { JFrame frame = new JFrame("Row Filter"); frame.add(new TestTableSortFilter()); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true);
FieldTitle fieldTitle = FieldTitle.values()[i]; gbc = createGbc(0, i); add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc); gbc = createGbc(1, i); JTextField textField = new JTextField(10); add(textField, gbc); return fieldMap.get(fieldTitle).getText();
JFrame frame = new JFrame(); final JTextArea textArea = new JTextArea(); JScrollPane pane = new JScrollPane(textArea); + "Blabla\nBlabla\nBlabla\nBlabla\nBlabla\nBlabla\n"); textArea.setSelectionColor(Color.RED); frame.add(pane); frame.setSize(300, 120); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); int pos2 = textArea.getText().indexOf(turnToString2); Rectangle startIndex = textArea.modelToView(pos2); textArea.getHighlighter().addHighlight(pos2,
import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class TestDialog { public static void main(String[] args) { Object[] options1 = { "Try This Number", "Choose A Random Number", "Quit" }; JPanel panel = new JPanel(); panel.add(new JLabel("Enter number between 0 and 1000")); JTextField textField = new JTextField(10); panel.add(textField); int result = JOptionPane.showOptionDialog(null, panel, "Enter a Number", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, null); if (result == JOptionPane.YES_OPTION){ JOptionPane.showMessageDialog(null, textField.getText()); } } }
private final JFrame f = new JFrame(); private final JTextField tf = new JTextField(25); private final JTextArea ta = new JTextArea(15, 25); private final JButton send = new JButton("Send"); private volatile PrintWriter out; private Scanner in; this.kind = kind; f.setTitle("Echo " + kind); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getRootPane().setDefaultButton(send); f.add(tf, BorderLayout.NORTH); f.add(new JScrollPane(ta), BorderLayout.CENTER); f.add(send, BorderLayout.SOUTH); f.setLocation(kind.offset, 300); f.pack(); f.setVisible(true); thread.start(); String s = tf.getText(); if (out != null) { out.println(s);
import java.awt.*; import javax.swing.*; public class CopyTextNorthPanel2 extends JPanel { private static final long serialVersionUID = 1L; public JTextField northField; public CopyTextNorthPanel2() { northField = new JTextField("Welcome World"); northField.setFont(new Font("Serif", Font.BOLD, 20)); northField.setPreferredSize(new Dimension(300, 25)); add(northField); } public String getNorthFieldText() { return northField.getText(); } }
final JFrame frame = new JFrame("Text HIGHLIGHT"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); contentPane.add(scrollPane); JButton remHighButton = new JButton("REMOVE HIGHLIGHT"); remHighButton.addActionListener(new ActionListener() JButton button = new JButton("HIGHLIGHT TEXT"); button.addActionListener(new ActionListener() if (text != null && text.length() > 0) int startIndex = tarea.getText().indexOf(text); int endIndex = startIndex + text.length(); Highlighter highlighter = tarea.getHighlighter(); frame.add(remHighButton, BorderLayout.PAGE_START); frame.add(contentPane, BorderLayout.CENTER); frame.add(button, BorderLayout.PAGE_END); frame.setVisible(true);
FieldTitle fieldTitle = FieldTitle.values()[i]; gbc = createGbc(0, i); add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc); gbc = createGbc(1, i); JTextField textField = new JTextField(10); add(textField, gbc); return fieldMap.get(fieldTitle).getText();
JFrame frame = new JFrame("WindowCommunication"); frame.getContentPane().add(new MyFramePanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); private JTextField field = new JTextField(10); private JButton openDialogeBtn = new JButton("Open Dialog"); field.setFocusable(false); add(field); add(openDialogeBtn); private JTextField field = new JTextField(10); private JButton okButton = new JButton("OK"); return field.getText();
import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class OptionPaneTest { public static void main(String[] args) { JPanel myPanel = new JPanel(); JTextField field1 = new JTextField(10); JTextField field2 = new JTextField(10); myPanel.add(field1); myPanel.add(field2); JOptionPane.showMessageDialog(null, myPanel); System.out.println(field1.getText() + field2.getText()); } }
private WebEngine engine; private JFrame frame = new JFrame(); private JPanel panel = new JPanel(new BorderLayout()); private JLabel lblStatus = new JLabel(); private JButton btnGo = new JButton("Go"); private JTextField txtURL = new JTextField(); private JProgressBar progressBar = new JProgressBar(); loadURL(txtURL.getText()); topBar.add(txtURL, BorderLayout.CENTER); topBar.add(btnGo, BorderLayout.EAST); statusBar.add(lblStatus, BorderLayout.CENTER); statusBar.add(progressBar, BorderLayout.EAST); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);
import javax.swing.*; public class OptionEg { public static void main(String[] args) { final JTextField ipField = new JTextField(10); final JTextField portField = new JTextField(10); JPanel panel = new JPanel(); panel.add(new JLabel("IP:")); panel.add(ipField); panel.add(Box.createHorizontalStrut(15)); panel.add(new JLabel("Port:")); panel.add(portField); int result = JOptionPane.showConfirmDialog(null, panel, "Enter Information", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { System.out.println("IP: " + ipField.getText()); System.out.println("Port: " + portField.getText()); } } }
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JPanel bottomPanel = CreateBottomPanel(); frame = new JFrame("Comp Table Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(CompTableScrollpane, BorderLayout.CENTER); frame.add(bottomPanel, BorderLayout.SOUTH); frame.setPreferredSize(new Dimension(800, 400)); frame.setLocation(150, 150); frame.pack(); frame.setVisible(true); addButton = new JButton("Add Comp"); addButton.addActionListener(new ActionListener() { panel.add(addButton); return panel; private JComboBox typeCombo = new JComboBox(new Object[]{"height", "length", "volume"}); private JComboBox relationCombo = new JComboBox(new Object[]{"above", "below", "between"}); private JTextField lowerField = new JTextField(); private JLabel labelAnd = new JLabel(" and "); private JTextField upperField = new JTextField(); private JButton removeButton = new JButton("remove"); return new Comp(typeCombo.getSelectedIndex(), relationCombo.getSelectedIndex(), lowerField.getText(), upperField.getText());
import javax.swing.*; public class JOptionPaneWithInput { public static void main(String[] args) { JTextField xField = new JTextField(5); JTextField yField = new JTextField(5); JPanel myPanel = new JPanel(); myPanel.add(new JLabel("Input1:")); myPanel.add(xField); myPanel.add(Box.createHorizontalStrut(15)); // a spacer myPanel.add(new JLabel("input2:")); myPanel.add(yField); int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter input 1 and 2 Values", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { System.out.println("1 value: " + xField.getText()); System.out.println("2 value: " + yField.getText()); } } }
isEmpty = textfield.getText().length() == 0; foregroundColor = textfield.getForeground(); JFrame frame = new JFrame("Test ghost text"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JTextField textField = new JTextField(); JButton button = new JButton("Grab focus"); GhostText ghostText = new GhostText(textField, "Please enter some text here..."); textField.setPreferredSize(new Dimension(300, 24)); panel.add(textField); panel.add(button); frame.add(panel); frame.pack(); frame.setVisible(true); button.grabFocus();