Primm
Primm

Reputation: 1387

How can I Give a JTextPane a different width than the main JFrame?

So, when I create a JTextPane and add it to a JFrame, it fills up the frame, moving with it. How do I stop this so I can do pane.setSize?

Upvotes: 0

Views: 112

Answers (3)

namenamename
namenamename

Reputation: 195

The easiest way would be to create a JPanel and add the JTextPane to the panel. Then you can add the panel to the frame.

JPanel panel = new JPanel();
panel.setLayout(null);

JTextPane pane = new JTextPane();

panel.add(pane);

this.add(panel); //assuming that this extends JFrame

You should now be able to resize the text pane appropriately.

Upvotes: 1

Jabrown207
Jabrown207

Reputation: 225

Create a new gridBagLayout and set the GridBagConstraints accordingly.

Container pane;
pane.setLayout(new GridBagLayout());
    GridBagConstraints c;
    c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.ipady = 0;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.insets = new Insets(2, 2, 2, 2);
    c.gridx = 0;
    c.gridy = 0;
    pane.add(jsp, c);
     pane.setBackground(Color.cyan);
    getContentPane().add(pane);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setTitle("TITLE");
    setLocation(150, 50);
    pack();
    setSize(600, 800);

Upvotes: 1

mKorbel
mKorbel

Reputation: 109813

JFrame has implemented BorderLayout in the API, then

JFrame.add(JTextPane) equals JFrame.add(JTextPane, BorderLayout.CENTER)

if is there only one JComponent placed into JFrame, then (center area) fills all available Dimension that returns container

no idea what do you rally to want to do

  1. use proper LayoutManager

  2. use JScrollPane

Upvotes: 1

Related Questions