mehrere jtextpanes in einem jpanel mit Zeilenumbruch

memgen

Grünschnabel
Hallo,

wir wollen in einem boxlayout mehrere jtextpanes in ein jpanel einfügen. Diese jtextpanes sollen
html enthalten und einen automatischen Zeilenumbruch am Zeilenende ausführen.
Außerdem sollen nach und nach neue jtextpanes in das jlabel integriert werden.

Unser Problem liegt nun darin, dass bei längeren Texten der Zeilenumbruch zwar stattfindet, aber plötzlich rechts und links zwei graue Bereiche entstehen und nur in der Mitte der formatierte Text steht. Wie kriegen wir die grauen Balken weg, sodass der Text ganz normal auf der linken Seite angezeigt wird?
problem.png

Wisst ihr irgendeine Lösung?
Hier ist der Code dazu:



Java:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;


public class Test extends JFrame {

    private static final long serialVersionUID = -2165717127504007034L;

    private JButton add;

    private JTextField input;

    private JPanel outputPane;

    private JScrollPane scrollPane;

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

            public void run() {
                Test test = new Test();
                test.setLocationRelativeTo(null);
                test.setVisible(true);
            }
        });
    }

    public Test() {
        this.initGUI();
    }

    public void appendToOutput() {
        String msg = "<html><body><p style=\"margin-top:10px\"; align=\"left\"> <font size=\"3\" face=\"arial\" color=\"red\">" + this.input.getText() + "</p></body></html>";
        this.input.setText("");
        this.input.requestFocus();
        JTextPane out = new JTextPane();
        out.setMaximumSize(new Dimension(379, 200));
        out.setContentType("text/html");
        out.setEditable(false);
        out.setText(msg);
        this.outputPane.add(out);
        this.outputPane.updateUI();
    }

    private void initGUI() {
        try {
            GroupLayout thisLayout = new GroupLayout(this.getContentPane());
            this.getContentPane().setLayout(thisLayout);
            {
                this.outputPane = new JPanel();
                BoxLayout outputPaneLayout = new BoxLayout(this.outputPane, javax.swing.BoxLayout.Y_AXIS);
                this.outputPane.setLayout(outputPaneLayout);

                this.outputPane.setMaximumSize(new Dimension(379, 32767));
                this.outputPane.setAutoscrolls(true);

                {
                    this.scrollPane = new JScrollPane(this.outputPane);
                    this.scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                    this.scrollPane.setMaximumSize(new java.awt.Dimension(379, 32767));
                }
            }
            {
                this.add = new JButton();
                this.add.setText("add");
                this.add.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Test.this.appendToOutput();

                    }
                });

                this.add.addKeyListener(new KeyAdapter() {

                    @Override
                    public void keyPressed(KeyEvent event) {
                        if ((event.getKeyCode() == KeyEvent.VK_ENTER)) {
                            Test.this.appendToOutput();
                        }
                    }
                });
            }
            {
                this.input = new JTextField();
                this.input.addKeyListener(new KeyAdapter() {

                    @Override
                    public void keyPressed(KeyEvent event) {
                        if ((event.getKeyCode() == KeyEvent.VK_ENTER)) {
                            Test.this.appendToOutput();
                        }
                    }
                });
            }
            thisLayout.setVerticalGroup(thisLayout
                    .createSequentialGroup()
                    .addComponent(this.scrollPane, GroupLayout.PREFERRED_SIZE, 213, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(
                            thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                    .addComponent(this.add, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(this.input, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(20, 20));
            thisLayout.setHorizontalGroup(thisLayout
                    .createParallelGroup()
                    .addComponent(this.scrollPane, GroupLayout.Alignment.LEADING, 0, 394, Short.MAX_VALUE)
                    .addGroup(
                            GroupLayout.Alignment.LEADING,
                            thisLayout.createSequentialGroup().addGap(7).addComponent(this.input, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE).addGap(27)
                                    .addComponent(this.add, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE).addContainerGap(37, Short.MAX_VALUE)));
            {
                this.setSize(400, 300);
                this.setResizable(false);
                this.pack();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
 

Neue Beiträge

Zurück