This is my first post on here, so please forgive me if do some formatting error and tell me how to do it.
I'm kinda new in coding with the GridBagLayout, so I've managed to understand the basic concept of it. From time to time it occurs that the formatting is completely vanished.
How it should be, a big nice TextPane:
How it is sometimes, TP really small:
The only way to trigger this problem is by calling the window from a JButton.
I pasted the code to call the window (Error.showDialog(new Exception());
) at a bunch of locations and it seems to occur whenever there's a ActionListener calling it.
This is my dialog printing the error message.
public static void showDialog(Exception myException) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
myException.printStackTrace(ps);
try {
ErrorString = os.toString("UTF8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JFrame window = new JFrame("Error");
window.setSize(550, 600);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
c.gridy = 0;
tpError = new JTextPane();
tpError.setPreferredSize(new Dimension(500, 500));
tpError.setEditable(false);
tpError.setText(ErrorString);
JScrollPane scrollPane = new JScrollPane(tpError, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scrollPane, c);
c.gridy++;
JPanel panelBtns = new JPanel(new FlowLayout());
JButton btnClose = new JButton("Schließen");
btnClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
window.setVisible(false);
window.dispose();
}
});
panelBtns.add(btnClose);
JButton btnSend = new JButton("Bericht");
btnSend.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent AE) {
try {
Desktop.getDesktop().browse(new URI("mailto:[email protected]?subject=Fehler%20im%20Converter"));
} catch (IOException | URISyntaxException ex) {
ex.printStackTrace();
}
}
});
panelBtns.add(btnSend);
panel.add(panelBtns,c );
window.add(panel);
window.setVisible(true);
window.repaint();
}
As shown in the images the formatting resets sometimes. I got this problem multiple times before, but I just ignored it. This time it's important that it does what I want.
tpError.setPreferredSize(new Dimension(500, 500));
Don't be hardcoding preferred sizes.
When you use a GridBagLayout a component will be displayed at its "minimum size" when there is not enough space to display the component at its preferred size. Instead you need to use other constraints like the "fill" constraint to control how the component grows/shrinks as the space available changes.
However, a better solution is to NOT use the GridBagLayout. Instead just use the default layot of the frame which is a BorderLayout
. Then the basic code would be:
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane( textPane );
add(scrollPane, BorderLayout.CENTER);
add(panelButtons, BorderLayout.PAGE_END);
Much simpler when you don't have to worry about more complex constraints.