Louisdewar
Louisdewar

Reputation: 77

Java - How to make a Jtextfield fill the whole frame

I'm trying to make a JTextArea fill the whole frame so it looks somewhat like notepad or textedit. Also would it be possible to have a scroll pane. Thanks in advance! Edit : JTextArea

Upvotes: 3

Views: 7549

Answers (3)

Paul Samsotha
Paul Samsotha

Reputation: 208964

What you want to to do is set the lines and character widths. You can use this constructor

JTextArea jta = new JTextArea(numberOfRows, numberOdCharacter);

                                   ^^               ^^
                                  Heigth          Width

jta.setLineWrap(true);  // wraps the words
jta.setWrapStyleWord(true);  // wraps by word instead of character

And yes you can use a JScrollPane

JScrollPane jsp = new JScrollPane(jta);
panel.add(jsp);

Then pack the frame, so it "fills" the frame

frame.pack()

See this example

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class NoteBookEditor extends JFrame {

    JTextArea jta = new JTextArea(40, 100);
    JMenuBar menuBar;
    JMenu menu;
    public NoteBookEditor(){

        menuBar = new JMenuBar();
        menu = new JMenu("hey");
        menuBar.add(menu);
        setJMenuBar(menuBar);


        add(new JScrollPane(jta));
        jta.setLineWrap(true);
        jta.setWrapStyleWord(true);
    }

    public static void createAndShowGui(){
        JFrame frame = new NoteBookEditor();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                createAndShowGui();
            }
        });
    }
}

enter image description here

Upvotes: 2

peter.petrov
peter.petrov

Reputation: 39437

I would use

http://docs.oracle.com/javase/7/docs/api/java/awt/GridBagLayout.html

http://docs.oracle.com/javase/7/docs/api/java/awt/GridBagConstraints.html#fill

and specify

GridBagConstraints.BOTH

But judging from the other answers, there
seem to be simpler ways of doing this.

Upvotes: 0

user3115056
user3115056

Reputation: 1280

Try this---

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;


public class TextFieldTest {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        JTextField tf = new JTextField();
        f.getContentPane().add(BorderLayout.EAST, tf);
        f.pack();
        f.setVisible(true);
    }
}

Upvotes: 6

Related Questions