Adjust to code

HuaMin Chen
HuaMin Chen used Ask the Experts™
on
Hi,
Please help and show details to disallow change to Text area below
public class Main {
      public static void main( String[] args )
      {
            try
            {
                  final JFrame frame = new JFrame( "Information" );
                  final JPanel mainPanel = new JPanel();
                  mainPanel.setPreferredSize ( new Dimension( 800, 700 ) );
                  frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                  frame.getContentPane().add ( mainPanel );
                  frame.setLocationRelativeTo( null );

                  // JTextArea supports a subset of HTML and with that you
                  // can change fonts within the text.
                  final JTextArea textArea = new JTextArea(
                              "This is an editable JTextArea. "
                                          + "A text area is a \"plain\" text component, "
                                          + "which means that although it can display text "
                                          + "in any font, all of the text is in the same font." );
                  textArea.setFont( new Font( "Serif", Font.BOLD, 12 ) );
                  textArea.setLineWrap( true );
                  textArea.setWrapStyleWord( true );

                  mainPanel.add( textArea );

                  frame.pack();
                  frame.setVisible( true );      // should be last

                  beep();
            }
            catch( final Exception e )
            {
                  final JPanel panel2 = new JPanel();

                  JOptionPane.showMessageDialog( panel2, e, "Error",
                              JOptionPane.ERROR_MESSAGE );
            }

      }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
Try adding this.
textArea.setEditable(false);

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
How to have bigger Textarea?
Top Expert 2016

Commented:
                  mainPanel.setPreferredSize ( new Dimension( 800, 700 ) );

Open in new window

Change that to
                  mainPanel.setLayout(new BorderLayout());

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

HuaMin ChenProblem resolver

Author

Commented:
Sorry to that, I have got "BorderLayout cannot be resolved to a type" due to this line

mainPanel.setLayout(new BorderLayout());

Open in new window

Top Expert 2016

Commented:
import java.awt.BorderLayout;

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
Hi,
I can run the code you suggested. But now the window is very small and how to enlarge it?
Top Expert 2016

Commented:
Please post complete new code
Top Expert 2016
Commented:
Try
    public static void main(String[] args) {
        try {
            final JFrame frame = new JFrame("Information");
            final JPanel mainPanel = new JPanel();
            frame.getContentPane().setLayout(new BorderLayout());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);

            // JTextArea supports a subset of HTML and with that you
            // can change fonts within the text.
            final JTextArea textArea = new JTextArea("This is an editable JTextArea. "
                    + "A text area is a \"plain\" text component, " + "which means that although it can display text "
                    + "in any font, all of the text is in the same font.");
            textArea.setFont(new Font("Serif", Font.BOLD, 12));
            // textArea.setEditable(false);
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);

            mainPanel.setLayout(new BorderLayout());
            mainPanel.add(textArea);
            frame.getContentPane().add(mainPanel);

            //frame.pack();
            frame.setSize(200, 200);
            frame.setVisible(true); // should be last

            //beep();
        } catch (final Exception e) {
            final JPanel panel2 = new JPanel();

            JOptionPane.showMessageDialog(panel2, e, "Error", JOptionPane.ERROR_MESSAGE);
        }

    }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial