Example usage for javax.swing JTextArea JTextArea

List of usage examples for javax.swing JTextArea JTextArea

Introduction

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

Prototype

public JTextArea(Document doc) 

Source Link

Document

Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).

Usage

From source file:Main.java

public static void main(String[] args) {
    String text = "one two three four five six seven eight nine ten ";
    JTextArea textArea = new JTextArea(text);
    textArea.setColumns(30);/*from www .j a va2s .  c om*/
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.setSize(textArea.getPreferredSize().width, 1);
    JOptionPane.showMessageDialog(null, new JScrollPane(textArea), "Not Truncated!",
            JOptionPane.WARNING_MESSAGE);
}

From source file:FlowLayoutExample.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    JTextArea area = new JTextArea("text area");
    area.setPreferredSize(new Dimension(100, 100));

    JButton button = new JButton("button");
    panel.add(button);// w w  w .  j av  a  2 s .c  om

    panel.add(new JScrollPane(area));

    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:TextAreaExample.java

public static void main(String[] args) {
    JFrame f = new JFrame("Text Area Examples");
    JPanel upperPanel = new JPanel();
    JPanel lowerPanel = new JPanel();
    f.getContentPane().add(upperPanel, "North");
    f.getContentPane().add(lowerPanel, "South");

    upperPanel.add(new JTextArea(content));
    upperPanel.add(new JTextArea(content, 6, 10));
    upperPanel.add(new JTextArea(content, 3, 8));

    lowerPanel.add(new JScrollPane(new JTextArea(content, 6, 8)));
    JTextArea ta = new JTextArea(content, 6, 8);
    ta.setLineWrap(true);/*from  w  w w  .  j a  v  a2 s .  c om*/
    lowerPanel.add(new JScrollPane(ta));

    ta = new JTextArea(content, 6, 8);
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    lowerPanel.add(new JScrollPane(ta));

    f.pack();
    f.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    final String START_STRING = "Start\n";
    final int START_STRING_LENGTH = START_STRING.length();

    JFrame frame = new JFrame("Navigation Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextArea textArea = new JTextArea(START_STRING);
    textArea.setCaretPosition(START_STRING_LENGTH);
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane, BorderLayout.CENTER);

    NavigationFilter filter = new NavigationFilter() {
        public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
            if (dot < START_STRING_LENGTH) {
                fb.setDot(START_STRING_LENGTH, bias);
            } else {
                fb.setDot(dot, bias);//w w  w  . j  ava2 s .  com
            }
        }

        public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
            if (dot < START_STRING_LENGTH) {
                fb.setDot(START_STRING_LENGTH, bias);
            } else {
                fb.setDot(dot, bias);
            }
        }
    };

    textArea.setNavigationFilter(filter);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:DnDDemo2.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new JPanel());

    JTextField textField = new JTextField(25);
    textField.setText("Let's swing higher");
    frame.add(textField);/*from w w  w .  jav a  2  s .c o  m*/

    JTextArea textArea = new JTextArea("Demonstrating\ndrag and drop");
    textArea.setForeground(Color.red);
    frame.add(new JScrollPane(textArea));

    textArea.setDragEnabled(true);
    textField.setDragEnabled(true);
    TextColorTransferHandler transferHandler = new TextColorTransferHandler();
    textArea.setTransferHandler(transferHandler);
    textField.setTransferHandler(transferHandler);
    frame.pack();
    frame.setVisible(true);
}

From source file:GuiScreens.java

public static void main(String[] args) {
    Rectangle virtualBounds = new Rectangle();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    JFrame frame[][] = new JFrame[gs.length][];
    for (int j = 0; j < gs.length; j++) {
        GraphicsDevice gd = gs[j];
        System.out.println("Device " + j + ": " + gd);
        GraphicsConfiguration[] gc = gd.getConfigurations();
        frame[j] = new JFrame[gc.length];

        for (int i = 0; i < gc.length; i++) {
            System.out.println("  Configuration " + i + ": " + gc[i]);
            System.out.println("    Bounds: " + gc[i].getBounds());
            virtualBounds = virtualBounds.union(gc[i].getBounds());
            frame[j][i] = new JFrame("Config: " + i, gc[i]);
            frame[j][i].setBounds(50, 50, 400, 100);
            frame[j][i].setLocation((int) gc[i].getBounds().getX() + 50, (int) gc[i].getBounds().getY() + 50);
            frame[j][i].getContentPane().add(new JTextArea("Config:\n" + gc[i]));
            frame[j][i].setVisible(true);
        }//ww w .  j  a  va 2 s.c  om
        System.out.println("Overall bounds: " + virtualBounds);
    }
}

From source file:com.edduarte.protbox.Protbox.java

public static void main(String... args) {

    // activate debug / verbose mode
    if (args.length != 0) {
        List<String> argsList = Arrays.asList(args);
        if (argsList.contains("-v")) {
            Constants.verbose = true;//from   ww w  . j  a v a 2  s  . c om
        }
    }

    // use System's look and feel
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
        // If the System's look and feel is not obtainable, continue execution with JRE look and feel
    }

    // check this is a single instance
    try {
        new ServerSocket(1882);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null,
                "Another instance of Protbox is already running.\n" + "Please close the other instance first.",
                "Protbox already running", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }

    // check if System Tray is supported by this operative system
    if (!SystemTray.isSupported()) {
        JOptionPane.showMessageDialog(null,
                "Your operative system does not support system tray functionality.\n"
                        + "Please try running Protbox on another operative system.",
                "System tray not supported", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }

    // add PKCS11 providers
    FileFilter fileFilter = new AndFileFilter(new WildcardFileFilter(Lists.newArrayList("*.config")),
            HiddenFileFilter.VISIBLE);

    File[] providersConfigFiles = new File(Constants.PROVIDERS_DIR).listFiles(fileFilter);

    if (providersConfigFiles != null) {
        for (File f : providersConfigFiles) {
            try {
                List<String> lines = FileUtils.readLines(f);
                String aliasLine = lines.stream().filter(line -> line.contains("alias")).findFirst().get();
                lines.remove(aliasLine);
                String alias = aliasLine.split("=")[1].trim();

                StringBuilder sb = new StringBuilder();
                for (String s : lines) {
                    sb.append(s);
                    sb.append("\n");
                }

                Provider p = new SunPKCS11(new ReaderInputStream(new StringReader(sb.toString())));
                Security.addProvider(p);

                pkcs11Providers.put(p.getName(), alias);

            } catch (IOException | ProviderException ex) {
                if (ex.getMessage().equals("Initialization failed")) {
                    ex.printStackTrace();

                    String s = "The following error occurred:\n" + ex.getCause().getMessage()
                            + "\n\nIn addition, make sure you have "
                            + "an available smart card reader connected before opening the application.";
                    JTextArea textArea = new JTextArea(s);
                    textArea.setColumns(60);
                    textArea.setLineWrap(true);
                    textArea.setWrapStyleWord(true);
                    textArea.setSize(textArea.getPreferredSize().width, 1);

                    JOptionPane.showMessageDialog(null, textArea, "Error loading PKCS11 provider",
                            JOptionPane.ERROR_MESSAGE);
                    System.exit(1);
                } else {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null,
                            "Error while setting up PKCS11 provider from configuration file " + f.getName()
                                    + ".\n" + ex.getMessage(),
                            "Error loading PKCS11 provider", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }

    // adds a shutdown hook to save instantiated directories into files when the application is being closed
    Runtime.getRuntime().addShutdownHook(new Thread(Protbox::exit));

    // get system tray and run tray applet
    tray = SystemTray.getSystemTray();
    SwingUtilities.invokeLater(() -> {

        if (Constants.verbose) {
            logger.info("Starting application");
        }

        //Start a new TrayApplet object
        trayApplet = TrayApplet.getInstance();
    });

    // prompts the user to choose which provider to use
    ProviderListWindow.showWindow(Protbox.pkcs11Providers.keySet(), providerName -> {

        // loads eID token
        eIDTokenLoadingWindow.showPrompt(providerName, (returnedUser, returnedCertificateData) -> {
            user = returnedUser;
            certificateData = returnedCertificateData;

            // gets a password to use on the saved registry files (for loading and saving)
            final AtomicReference<Consumer<SecretKey>> consumerHolder = new AtomicReference<>(null);
            consumerHolder.set(password -> {
                registriesPasswordKey = password;
                try {
                    // if there are serialized files, load them if they can be decoded by this user's private key
                    final List<SavedRegistry> serializedDirectories = new ArrayList<>();
                    if (Constants.verbose) {
                        logger.info("Reading serialized registry files...");
                    }

                    File[] registryFileList = new File(Constants.REGISTRIES_DIR).listFiles();
                    if (registryFileList != null) {
                        for (File f : registryFileList) {
                            if (f.isFile()) {
                                byte[] data = FileUtils.readFileToByteArray(f);
                                try {
                                    Cipher cipher = Cipher.getInstance("AES");
                                    cipher.init(Cipher.DECRYPT_MODE, registriesPasswordKey);
                                    byte[] registryDecryptedData = cipher.doFinal(data);
                                    serializedDirectories.add(new SavedRegistry(f, registryDecryptedData));
                                } catch (GeneralSecurityException ex) {
                                    if (Constants.verbose) {
                                        logger.info("Inserted Password does not correspond to " + f.getName());
                                    }
                                }
                            }
                        }
                    }

                    // if there were no serialized directories, show NewDirectory window to configure the first folder
                    if (serializedDirectories.isEmpty() || registryFileList == null) {
                        if (Constants.verbose) {
                            logger.info("No registry files were found: running app as first time!");
                        }
                        NewRegistryWindow.start(true);

                    } else { // there were serialized directories
                        loadRegistry(serializedDirectories);
                        trayApplet.repaint();
                        showTrayApplet();
                    }

                } catch (AWTException | IOException | GeneralSecurityException | ReflectiveOperationException
                        | ProtboxException ex) {

                    JOptionPane.showMessageDialog(null,
                            "The inserted password was invalid! Please try another one!", "Invalid password!",
                            JOptionPane.ERROR_MESSAGE);
                    insertPassword(consumerHolder.get());
                }
            });
            insertPassword(consumerHolder.get());
        });
    });
}

From source file:Main.java

public static void showInfo(Component parent, String message) {
    Component cmp = new JScrollPane(new JTextArea(message));
    cmp.setPreferredSize(new Dimension(400, 400));
    JOptionPane.showMessageDialog(parent, cmp, "Information", JOptionPane.INFORMATION_MESSAGE);
}

From source file:Main.java

public Main() {
    // Create a text area with some initial text
    JTextArea textarea = new JTextArea("Initial Text");

    int rows = 20;
    int cols = 30;
    textarea = new JTextArea("Initial Text", rows, cols);
    this.getContentPane().add(new JScrollPane(textarea));

}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);/*from   w ww. ja  v  a  2s  .c  o m*/

    JTextArea textArea = new JTextArea("drag it...");
    textArea.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("Mouse Dragged...");
        }

        public void mouseMoved(MouseEvent e) {
            System.out.println("Mouse Moved...");
        }
    });

    getContentPane().add(textArea);
}