1

I prepared 2 test cases in the JUnit style (sry for the weird kind of test I dont realyl know how to assert UI) for the 2nd example I selectively cut the string but I want to be able to paste the long string and have the UI cut it correctly

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.junit.jupiter.api.Test;

public class testing {

    @Test
    public void howItsSupposedToLook() {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        frame.setVisible(true);
        frame.setSize(1000, 900);

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.GREEN);
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

// this is how it should look
        JLabel fileLabel3 = new JLabel(
                "<html><div style=\"padding-left:1px;padding-top:10px\"><div style=\"border:1px;font-size: 10px;line-height: 11px; height:23px;overflow:hidden;width:"
                        + (100) + "px;white-space:nowrap;\">" + "fsdfesfevvveeeg" + "</div></div></html>");
        leftPanel.add(fileLabel3);
//no more differences between the two from here
        frame.add(leftPanel, BorderLayout.LINE_START);

        JPanel centerPAnel = new JPanel();
        centerPAnel.setBackground(Color.RED);

        frame.add(centerPAnel, BorderLayout.CENTER);
        frame.validate();
        frame.repaint();

        boolean open = true;
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent winEvt) {

                System.exit(0);
            }
        });
        while (open) {
        }

    }

    @Test
    public void failedTest() {

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        frame.setVisible(true);
        frame.setSize(1000, 900);

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.GREEN);
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

 // this is how it actually looks

        JLabel fileLabel = new JLabel(
                "<html><div style=\"padding-left:1px;padding-top:10px\"><div style=\"border:1px;font-size: 10px;line-height: 11px; height:23px;overflow:hidden;width:"
                        + (100) + "px;white-space:nowrap;\">"
                        + "fsdfesfevvveeegggggggggggggggggggggggevevsdffffffffffffffffffffffffffffffffffffffffffffffffffffffffh"
                        + "</div></div></html>");
        fileLabel.setMaximumSize(new Dimension(100, 40));
//no more differences between the two from here
        leftPanel.add(fileLabel);

        frame.add(leftPanel, BorderLayout.LINE_START);

        JPanel centerPAnel = new JPanel();
        centerPAnel.setBackground(Color.RED);

        frame.add(centerPAnel, BorderLayout.CENTER);
        frame.validate();
        frame.repaint();

        boolean open = true;
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent winEvt) {

                System.exit(0);
            }
        });
        while (open) {
        }

    }

}

how its supposed to look (first (manufactured ) example) : enter image description here

how it actually looks:enter image description here

to me it feels like the Layout doesnt correclty recognize the length of the JPanel does anyone have an idea on what to do about that ?

jonathan Heindl
  • 844
  • 7
  • 16
  • Lets start with the fact that Swing's HTML/CSS support is limited and doesn't support the current standard (I think HTML is 3ish, can't remember it's CSS support) – MadProgrammer Apr 06 '19 at 22:37
  • yeah I noticed that (I tried overflow hidden and ellipsis mode at the start but I had to give up on that) – jonathan Heindl Apr 06 '19 at 22:41
  • Your problem is one of cascading issues. Using `BoxLayout` in a `BorderLayout` and the fact that much of the CSS is probably been ignored. You're also going to have issues as the text won't wrap because it want's to wrap on the word boundary, not the character boundry – MadProgrammer Apr 06 '19 at 22:51
  • Suggestion - Use a non-editable `JTextArea` instead, set to word wrap, as the second [example here](https://stackoverflow.com/questions/48481118/how-to-use-an-entire-jlabel-for-text-holding/48481317#48481317) demonstrates – MadProgrammer Apr 06 '19 at 22:54
  • @MadProgrammer the text not being able to wrap is intended the idea is to have a list of elements and to fit them as many of them into the view as possible only display the first line of each end cut them if necessary (yeah its probably bad UX but I dont think thers gonna be a User aside from me for the near to far future :D) – jonathan Heindl Apr 06 '19 at 22:58
  • 1
    Then don't use a `BorderLayout`, it will give as much space as the component's `preferredSize` requests. Playing with `setPreferredSize` is also a bad idea, as there are to many factors which affect this result. A "different" idea might be to use a `JList` and define a `prototype` cell value which matches the character length restrictions, but this will be affected by the font settings (so when the font size changes the width will change) – MadProgrammer Apr 06 '19 at 23:03

1 Answers1

0

Simply use

fileLabel.setPreferredSize(new Dimension(100, 40));

Instead of

fileLabel.setMaximumSize(new Dimension(100, 40));

This setter tells what size is must be used as "preferred" for this component when it placed in container with layout manager

PS: Sorry for my english

Stranger in the Q
  • 3,668
  • 2
  • 21
  • 26
  • The problem arises from using numbers other than the actual preferred size, which is calculated at run-time and may vary among platforms. See also [*Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?*](https://stackoverflow.com/q/7229226/230513) – trashgod Apr 07 '19 at 10:46