Argo
Argo

Reputation: 153

New JFrame with TextArea

I'm trying to make a new JFrame that contains a TextArea in my Java Swing application. I've made the JFrame and assigned it to a button but making a TextArea inside the new window so to speak seems a bit more difficult that I imagined. My app looks like this so far: https://i.sstatic.net/FaJa8.jpg

Here's my code:

public class UserInterface extends JFrame {

private JPanel contentPane;
private JTextField brugernavn;

String username = "";
String password = "";   
private JPasswordField adgangskode;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                UserInterface frame = new UserInterface();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */

public UserInterface() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 600, 400);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    brugernavn = new JTextField();
    brugernavn.setBounds(6, 41, 134, 28);
    contentPane.add(brugernavn);
    brugernavn.setColumns(10);

    JLabel lblBrugernavn = new JLabel("Brugernavn");
    lblBrugernavn.setBounds(22, 20, 98, 16);
    contentPane.add(lblBrugernavn);

    JLabel label = new JLabel("Adgangskode");
    label.setBounds(22, 88, 98, 16);
    contentPane.add(label);

    JButton btnFindMitSkema = new JButton("Find mit skema");
    btnFindMitSkema.setBounds(6, 175, 150, 29);
    contentPane.add(btnFindMitSkema);

    adgangskode = new JPasswordField();
    adgangskode.setBounds(6, 116, 134, 28);
    contentPane.add(adgangskode);

    JLabel titel = new JLabel("Skema-checker for Elevplan v1");
    titel.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
    titel.setBounds(151, 11, 298, 28);
    contentPane.add(titel);

    final JFrame LicensInfoFrame = new JFrame("Licens & info");

     final String GPL = "Elevplan checker version \n"
                + "Copyright (C) 2013  Philip Jakobsen \n"
                + "This program is free software: you can redistribute it and/or modify \n"
                + "it under the terms of the GNU General Public License as published by \n"
                + "the Free Software Foundation, either version 3 of the License, or \n"
                + "(at your option) any later version. \n"
                + "This program is distributed in the hope that it will be useful, \n"
                + "but WITHOUT ANY WARRANTY; without even the implied warranty of \n"
                + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the \n"
                + "GNU General Public License for more details. \n"
                + "You should have received a copy of the GNU General Public License \n"
                + "along with this program.  If not, see \n"
                + "http://www.gnu.org/licenses"; 

     final String licensinfoframeheader = "Licens & info";

    JButton btnLicensInfo = new JButton("Licens & info");
    btnLicensInfo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            LicensInfoFrame.pack();
            LicensInfoFrame.setVisible(true);

            LicensInfoFrame.setTitle(licensinfoframeheader);

        }
    });
    btnLicensInfo.setBounds(6, 245, 150, 29);
    contentPane.add(btnLicensInfo);

    final TextArea textArea = new TextArea();
    textArea.setBounds(162, 41, 428, 233);
    contentPane.add(textArea);
    textArea.setBackground(new Color(255, 255, 255));
    textArea.setEditable(false);

    btnFindMitSkema.addActionListener(new ActionListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            username = brugernavn.getText();
            password = adgangskode.getText();
            String output = "";
            new Backend();
            try {
                output = Backend.main(username, password);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            textArea.setText(output);

        }
    });



}
    }

In short, I want to add a TextArea to the JFrame that is triggered by the button labeled "License & info". The JFrame itself works fine but is empty.

Upvotes: 0

Views: 4308

Answers (3)

Tech Nerd
Tech Nerd

Reputation: 832

You can take a look Further you can add a JTextArea to one of your panel make it to setVisible(false) by default and by using JButton actionListener take it back for your user as setVisible(true)

Upvotes: 0

Vishal K
Vishal K

Reputation: 13066

Never use null layout for any swing component. It would not only a nightmare for you to handle the GUI but also will posses problem to display across various platforms. You should let the inbuilt Layouts of Swing to take care of the GUI rendering. You should look at A Visual Guide to Layout Managers to understand how to use the inbuilt layouts in Swing.
Also , never create more than one JFrame in an application. To achieve such tasks you can use JOptionPane or JDialog or JInternalFrames. What I suggest you that you start programming swing application from scratch . First understand the Layouts and how the GUI components work at swing official tutorial. After you get yourself in comfort zone , then create big applications consolidating your knowledge in them.

Upvotes: 2

Mac70
Mac70

Reputation: 320

You should add Text Area to JScrollPane.

Futhermore, I suggest to use Swing editors like default NetBeans Swing editor as these tools are making interface coding much easier and allow you to focus on what is most important.

Upvotes: 2

Related Questions