Search in sources :

Example 41 with JTextField

use of javax.swing.JTextField in project GNS by MobilityFirst.

the class Sudo method main.

/**
   *
   * @param arg
   */
public static void main(String[] arg) {
    try {
        JSch jsch = new JSch();
        Session session = SSHClient.authenticateWithKey(jsch, null, null, null);
        UserInfo ui = new UserInfoPrompted();
        session.setUserInfo(ui);
        session.connect();
        String command = JOptionPane.showInputDialog("Enter command, execed with sudo", "printenv SUDO_USER");
        String sudo_pass = null;
        {
            JTextField passwordField = new JPasswordField(8);
            Object[] ob = { passwordField };
            int result = JOptionPane.showConfirmDialog(null, ob, "Enter password for sudo", JOptionPane.OK_CANCEL_OPTION);
            if (result != JOptionPane.OK_OPTION) {
                System.exit(-1);
            }
            sudo_pass = passwordField.getText();
        }
        Channel channel = session.openChannel("exec");
        // man sudo
        //   -S  The -S (stdin) option causes sudo to read the password from the
        //       standard input instead of the terminal device.
        //   -p  The -p (prompt) option allows you to override the default
        //       password prompt and use a custom one.
        ((ChannelExec) channel).setCommand("sudo -S -p '' " + command);
        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();
        ((ChannelExec) channel).setErrStream(System.err);
        ((ChannelExec) channel).setPty(true);
        channel.connect();
        out.write((sudo_pass + "\n").getBytes());
        out.flush();
        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) {
                    break;
                }
                System.out.print(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
            }
        }
        channel.disconnect();
        session.disconnect();
    } catch (JSchException | HeadlessException | IOException e) {
        System.out.println(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) UserInfoPrompted(edu.umass.cs.aws.networktools.UserInfoPrompted) HeadlessException(java.awt.HeadlessException) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) UserInfo(com.jcraft.jsch.UserInfo) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) JTextField(javax.swing.JTextField) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) HeadlessException(java.awt.HeadlessException) JSchException(com.jcraft.jsch.JSchException) JPasswordField(javax.swing.JPasswordField) Session(com.jcraft.jsch.Session)

Example 42 with JTextField

use of javax.swing.JTextField in project nhin-d by DirectProject.

the class PKCS11SecretKeyManager method addTextKey.

private void addTextKey() {
    final JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    final JPanel topPanel = new JPanel();
    final JLabel aliasLabel = new JLabel("Alias:");
    aliasLabel.setSize(60, 30);
    final JTextField aliasField = new JTextField(40);
    topPanel.add(aliasLabel);
    topPanel.add(aliasField);
    final JPanel bottomPanel = new JPanel();
    final JLabel keyLabel = new JLabel("Key:");
    keyLabel.setSize(60, 30);
    final JTextField keyField = new JTextField(40);
    bottomPanel.add(keyLabel);
    bottomPanel.add(keyField);
    panel.add(topPanel, BorderLayout.NORTH);
    panel.add(bottomPanel, BorderLayout.SOUTH);
    final String[] options = new String[] { "OK", "Cancel" };
    int option = JOptionPane.showOptionDialog(null, panel, "Generate New Text Based Secret Key ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
    if (option == JOptionPane.OK_OPTION) {
        final String alias = aliasField.getText();
        final String keyText = keyField.getText();
        if ((alias != null && !alias.trim().isEmpty()) && keyText != null && !keyText.trim().isEmpty()) {
            // generate a new random secret key
            try {
                mgr.clearKey(alias);
                mgr.setKey(alias, new SecretKeySpec(keyText.getBytes(), ""));
                updateKeyTableData();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Failed to add new text based secret key: " + e.getMessage(), "Add Key Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
Also used : JPanel(javax.swing.JPanel) BorderLayout(java.awt.BorderLayout) SecretKeySpec(javax.crypto.spec.SecretKeySpec) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) Point(java.awt.Point) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException)

Example 43 with JTextField

use of javax.swing.JTextField in project jdk8u_jdk by JetBrains.

the class DeadKeyMacOSXInputText method createAndShowGUI.

static void createAndShowGUI() {
    Frame frame = new Frame();
    frame.setSize(300, 300);
    Panel panel = new Panel(new BorderLayout());
    JTextField textField = new JTextField();
    textField.addKeyListener(new DeadKeyListener());
    panel.add(textField, BorderLayout.CENTER);
    frame.add(panel);
    frame.setVisible(true);
    toolkit.realSync();
    textField.requestFocusInWindow();
    toolkit.realSync();
}
Also used : JTextField(javax.swing.JTextField)

Example 44 with JTextField

use of javax.swing.JTextField in project jdk8u_jdk by JetBrains.

the class Test8015300 method main.

public static void main(String[] args) throws Exception {
    UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
    for (UIManager.LookAndFeelInfo info : array) {
        UIManager.setLookAndFeel(info.getClassName());
        System.err.println("L&F: " + info.getName());
        invokeAndWait(new Runnable() {

            @Override
            public void run() {
                combo = new JComboBox<>(ITEMS);
                combo.addItemListener(new ItemListener() {

                    @Override
                    public void itemStateChanged(ItemEvent event) {
                        if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
                            ComboBoxEditor editor = combo.getEditor();
                            Object component = editor.getEditorComponent();
                            if (component instanceof JTextField) {
                                JTextField text = (JTextField) component;
                                boolean selected = null != text.getSelectedText();
                                StringBuilder sb = new StringBuilder();
                                sb.append(" - ").append(combo.getSelectedIndex());
                                sb.append(": ").append(event.getItem());
                                if (selected) {
                                    sb.append("; selected");
                                }
                                System.err.println(sb);
                                if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
                                    throw new Error("unexpected state of text selection");
                                }
                            }
                        }
                    }
                });
                JFrame frame = new JFrame(getClass().getSimpleName());
                frame.add(combo);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        for (int i = 0; i < ITEMS.length; ++i) {
            select(i, true);
            select(1, false);
        }
        invokeAndWait(new Runnable() {

            @Override
            public void run() {
                windowForComponent(combo).dispose();
            }
        });
    }
}
Also used : ItemEvent(java.awt.event.ItemEvent) JComboBox(javax.swing.JComboBox) UIManager(javax.swing.UIManager) JTextField(javax.swing.JTextField) ComboBoxEditor(javax.swing.ComboBoxEditor) WindowsComboBoxEditor(com.sun.java.swing.plaf.windows.WindowsComboBoxUI.WindowsComboBoxEditor) WindowsComboBoxEditor(com.sun.java.swing.plaf.windows.WindowsComboBoxUI.WindowsComboBoxEditor) JFrame(javax.swing.JFrame) ItemListener(java.awt.event.ItemListener)

Example 45 with JTextField

use of javax.swing.JTextField in project jdk8u_jdk by JetBrains.

the class InputContextMemoryLeakTest method init.

public static void init() throws Throwable {
    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            frame = new JFrame();
            frame.setLayout(new FlowLayout());
            JPanel p1 = new JPanel();
            button = new JButton("Test");
            p1.add(button);
            frame.add(p1);
            text = new WeakReference<JTextField>(new JTextField("Text"));
            p = new WeakReference<JPanel>(new JPanel(new FlowLayout()));
            p.get().add(text.get());
            frame.add(p.get());
            frame.setBounds(500, 400, 200, 200);
            frame.setVisible(true);
        }
    });
    Util.focusComponent(text.get(), 500);
    Util.clickOnComp(button, new Robot());
    //References to objects testes for memory leak are stored in Util.
    //Need to clean them
    Util.cleanUp();
    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            frame.remove(p.get());
        }
    });
    Util.waitForIdle(null);
    //After the next caret blink it automatically TextField references
    Thread.sleep(text.get().getCaret().getBlinkRate() * 2);
    Util.waitForIdle(null);
    assertGC();
}
Also used : JPanel(javax.swing.JPanel) FlowLayout(java.awt.FlowLayout) JFrame(javax.swing.JFrame) WeakReference(java.lang.ref.WeakReference) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) Robot(java.awt.Robot)

Aggregations

JTextField (javax.swing.JTextField)450 JLabel (javax.swing.JLabel)270 JPanel (javax.swing.JPanel)236 JButton (javax.swing.JButton)135 BorderLayout (java.awt.BorderLayout)115 GridBagLayout (java.awt.GridBagLayout)94 Insets (java.awt.Insets)94 GridBagConstraints (java.awt.GridBagConstraints)93 Dimension (java.awt.Dimension)91 JCheckBox (javax.swing.JCheckBox)89 ActionEvent (java.awt.event.ActionEvent)84 ActionListener (java.awt.event.ActionListener)75 JScrollPane (javax.swing.JScrollPane)64 BoxLayout (javax.swing.BoxLayout)50 JTextArea (javax.swing.JTextArea)40 FlowLayout (java.awt.FlowLayout)39 JComboBox (javax.swing.JComboBox)39 JRadioButton (javax.swing.JRadioButton)37 ButtonGroup (javax.swing.ButtonGroup)35 JTable (javax.swing.JTable)34