Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing layout issue on migration to java 8

My swing application in java 5 which had a display like

this

After migrating to java 8, zoomed up and displays only a part of it like this

I saw this and tried setting J2D_D3D as environment variable and also tried passing it as a vm parameter. But didn't solved the issue. Any idea what this could be?

like image 259
RBz Avatar asked Oct 19 '22 08:10

RBz


1 Answers

For reference, here's an example the doesn't have the problem. It uses a GridLayout(0, 1) with congruent gaps and border. Resize the enclosing frame to see the effect. Experiment with Box(BoxLayout.Y_AXIS) as an alternative.

I suspect the original code (mis-)uses some combination of setXxxSize() or setBounds(), which will display the effect shown if the chosen Look & Feel has different geometry specified by the buttons' UI delegate.

image

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/a/31078625/230513 */
public class Buttons {

    private void display() {
        JFrame f = new JFrame("Buttons");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel(new GridLayout(0, 1, 10, 10));
        p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        for (int i = 0; i < 3; i++) {
            p.add(new JButton("Button " + (i + 1)));
        }
        f.add(p);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Buttons()::display);
    }
}
like image 195
trashgod Avatar answered Nov 04 '22 02:11

trashgod