Resize JPanel

1

Holaaa. I have a problem wanting to resize a JPanel inside a JPanel that has a Layout of the BoxLayout type. When I add to the JPanel of the main class a JPanel that contains a JTextPane, the JPanel that I add, occupies a large space. My question is how I would have to do so that the JPanel that contains the JTextPane has the size of the JTextPane.

In the following image I show the window.

https://s23.postimg.org/vrj4nzyy3/Sin_t_tulo.png

What is in red, would be the main JPanel. What is blue would be the JPanel that I add and that contains the JTextPane. What is in yellow is the JTextPane.

The JPanel marked in blue, should be the size of the JTextPane it contains. As the next image.

https://s23.postimg.org/53wq5lsx7/Sin_t_tulo_copia_copia.png

That means that the JPanel has the size of the JTextPane, so that they are together. Or at least that the first has the size of the JTextPane and the second does not, but when adding a third JPanel with the JTextPane if it has it. In other words:

https://s23.postimg.org/jbmeu95m3/Sin_t_tulo_copia.png

Then I leave the main class with the JPanel, which would be marked in red.         public FrameTest () {             initialize ();         }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(10, 11, 414, 240);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        frame.getContentPane().add(panel);      

        PanelText p1 = new PanelText ("aaaaa");
        p1.setSize((new PanelText("aaaaa")).getPreferredSize());
        panel.add(p1);
        PanelText p2 = new PanelText ("bbbbb");
        panel.add(p2);
    }
}

The next class is the JPanel that contains the JTextPane, what is commented are the tests I did, with the only one that brings me closer to what I want to do is with setMaximumSize (content.getPreferredSize ()); but the problem is that it puts it in the middle and cut it:

import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.border.EmptyBorder;

public class PanelText extends JPanel {

    private TextPane content;

    public PanelText(String message) {
        setBorder(new EmptyBorder(5, 5, 5, 5));
        setOpaque(false);
        setLayout(null);
        setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));



        this.content = new TextPane (message);
   //   setMaximumSize(content.getPreferredSize());
   //   setMaximumSize(content.getSize());
   //   setMaximumSize(content.getMaximumSize());
   //   setMaximumSize(content.getMinimumSize());
   //   setSize(content.getSize());
   //   setSize(content.getWidth(), content.getHeight());
   //   setPreferredSize(content.getPreferredSize());

        add(content);
    }

    public TextPane getContent() {
        return this.content;
    }
}

and finally the JTextPane

import java.awt.*;
import javax.swing.JTextPane; 
import javax.swing.border.EmptyBorder;
import javax.swing.text.*;

public class TextPane extends JTextPane {

private String message;

public TextPane (String message) {
    this.message = message;
    this.setEditable(false);
    this.setOpaque(false);
    this.setBorder(new EmptyBorder(7, 7, 7, 7));       

    this.setText(message);  
}

public void paintComponent(Graphics g) {
    Dimension arcs = new Dimension(15,15);
    int width = getWidth();
    int height = getHeight();
    Graphics2D graphics = (Graphics2D) g;
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setColor(Color.WHITE);
    graphics.fillRoundRect(0, 0, width-1, height-1, arcs.width, arcs.height);
    graphics.setColor(getForeground());

    super.paintComponent(g);
}

}
    
asked by Alex 18.12.2016 в 02:39
source

1 answer

0

From what I understand, you want to use a design in which your TextPane are aligned in a column and the text is pasted to the left. If it is what you want you can get it by marking the maximum width of your TextPane as that of your container:

p1.setMaximumSize(new Dimension(p1.getParent().getWidth(), 0));

This you must do after adding it to the frame leaving the part where you add p1 as follows:

PanelText p1 = new PanelText ("aaaaa");
p1.setSize((new PanelText("aaaaa")).getPreferredSize());
panel.add(p1);//Después de añadirlo
p1.setMaximumSize(new Dimension(p1.getParent().getWidth(), 0));
    
answered by 18.12.2016 / 09:50
source