1

I was testing my Java code with CDE/Motif LookAndFeel.

I was viewing the performance/behavior of JButton when PreferredSize is established.

NOTE: I was viewing this post Fit size of an ImageIcon to a JButton, but I do't want to change dinamically the size (JButton/ImageIcon), only Want the "same" appareance between LAF's.

My code:

First I created an image at fly to test porpouses.

    BufferedImage bi = new BufferedImage(13, 13, BufferedImage.TYPE_INT_ARGB);

    Graphics gr = bi.createGraphics();
    gr.setColor(Color.RED);
    gr.drawLine(6, 0, 6, 13);
    gr.setColor(Color.GREEN);
    gr.drawLine(0, 6, 13, 6);
    gr.dispose();
    gr.drawImage(bi, 0, 0, null);

The Button!

    Font inFont = new Font("Monospaced", Font.PLAIN, 10);

    //Creating the Button!
    JButton jbt = new JButton(new ImageIcon(bi));
    jbt.setPreferredSize(new Dimension(30, 21)); // I want to use this Dimension!!!
    jbt.setSize(new Dimension(30, 21));
    jbt.setFont(inFont);

Now the panels...

    JPanel jpHorz = new JPanel();
    GroupLayout jpLAFLayout = new GroupLayout(jpHorz);
    jpHorz.setLayout(jpLAFLayout);

    //Code to obtain this appareance inserting THE BUTTON 'jbt'
    jpLAFLayout.setHorizontalGroup(
      jpLAFLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jpLAFLayout.createSequentialGroup()
      .addGap(26, 26, 26)
      .addComponent(jbt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      .addContainerGap(26, Short.MAX_VALUE))
    );
    jpLAFLayout.setVerticalGroup(
      jpLAFLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jpLAFLayout.createSequentialGroup()
      .addGap(6, 6, 6)
      .addComponent(jbt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)
      .addContainerGap(12, Short.MAX_VALUE))
    );

More code...

    JPanel jpVert = new JPanel();
    jpVert.setBackground(new Color(208, 208, 208));
    jpVert.setLayout(new BoxLayout(jpVert, BoxLayout.PAGE_AXIS));
    jpVert.add(jpHorz);
    frame.add(jpVert, BorderLayout.NORTH);
    frame.setPreferredSize(new Dimension(480, 120));

As you can see, the image is not full using CDE/Motif LookAndFeel

With jbt.setMargin(new Insets(0, 0, 0, 0)); I don't get the effects.

Is there method to change this bad appareance of BUTTON with Motif LAF?

Here an Image comparation.

enter image description here

Qstnr_La
  • 263
  • 1
  • 7

0 Answers0