0

I developed a GUI based application using Java. I used GridBagLayout to build UI. And when I run the jar file on my system the UI is looking perfect as I need. (Please see the below image)

enter image description here

But when I run the same jar file on other machines the UI is like below,

  1. Jtextfield became like [] this and not allowing the user to enter the input.
  2. Window size changed. You can see the title on the frame is truncated.

enter image description here

Below is the code what I have used in my test pane.

        public TestPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        try {
            URL url = getClass().getResource("alarm5.jpg");
            image = ImageIO.read(url);
        } catch (IOException ex) {
            System.out.println("Error: "+ex);
        }

        Font myFont = new Font("Calibri", Font.PLAIN | Font.BOLD, 16);
        Font myFont2 = new Font("Calibri", Font.PLAIN | Font.BOLD, 14);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        selectall = new JCheckBox("Select all");
        selectall.setForeground(Color.WHITE);
        selectall.setOpaque(false);
        selectall.setFont(myFont);
        selectall.addActionListener(this);
        add(selectall, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        monday = new JCheckBox("Monday");
        monday.setForeground(Color.WHITE);
        monday.setOpaque(false);
        monday.setFont(myFont2);
        monday.addActionListener(this);
        add(monday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        tuesday = new JCheckBox("Tuesday");
        tuesday.setForeground(Color.WHITE);
        tuesday.setOpaque(false);
        tuesday.addActionListener(this);
        tuesday.setFont(myFont2);
        add(tuesday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        wednesday = new JCheckBox("Wednesday");
        wednesday.setForeground(Color.WHITE);
        wednesday.setOpaque(false);
        wednesday.addActionListener(this);
        wednesday.setFont(myFont2);
        add(wednesday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.WEST;
        thursday = new JCheckBox("Thursday");
        thursday.setForeground(Color.WHITE);
        thursday.setOpaque(false);
        thursday.addActionListener(this);
        thursday.setFont(myFont2);
        add(thursday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        friday = new JCheckBox("Friday");
        friday.setForeground(Color.WHITE);
        friday.setOpaque(false);
        friday.addActionListener(this);
        friday.setFont(myFont2);
        add(friday, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        saturday = new JCheckBox("Saturday");
        saturday.setForeground(Color.WHITE);
        saturday.setOpaque(false);
        saturday.addActionListener(this);
        saturday.setFont(myFont2);
        add(saturday, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        sunday = new JCheckBox("Sunday");
        sunday.setForeground(Color.WHITE);
        sunday.setOpaque(false);
        sunday.addActionListener(this);
        sunday.setFont(myFont2);
        add(sunday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(10, 0, 0, 0);
        JLabel env = new JLabel("At what time, should i remind you? ");
        env.setForeground(Color.WHITE);
        env.setFont(myFont);
        add(env, gbc);      

        gbc.gridx = 3;
        gbc.gridy = 7;
        gbc.insets = new Insets(10, 0, 10, 0);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        final JTextField tm = new JTextField(5);
        tm.setText("21:00:00");
        tm.setToolTipText("Input should be in HH:MM:SS format and time format should be 24 hours.");
        add(tm, gbc);

        gbc.gridx = 1;
        gbc.gridy = 8;
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridwidth = 2;
        Set = new JButton(" Set ");
        Set.setFocusable(false);
        add(Set, gbc);
        }

Below is the code what I have used for frame.

                frame2 = new JFrame("Select days and time");
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame2.setLayout(new BorderLayout());
                frame2.add(new TestPane());
                frame2.pack();
                frame2.setResizable(false);
                frame2.setLocationRelativeTo(null);
                frame2.setVisible(true);
                frame2.setAlwaysOnTop(true);

Both systems have the same resolution settings. Can you please help me here? how can I make this look same on any system? And how can I fix the JTextfield issue?

Alarm image:

enter image description here

Nandan A
  • 2,702
  • 1
  • 12
  • 23
  • I suggest manually setting the frame size instead of using `JFrame#pack`. Make sure to use `JFrame#setMinimumSize` as well. You can also take a look at `Toolkit.getDefaultToolkit().getScreenSize()`. This can help you set the frame size based off of the screen size (e.i. you can use division to get a certain ratio). – Cardinal System Jun 08 '20 at 17:16
  • @Cardinal-ReinstateMonica You should post an answer. – Code-Apprentice Jun 08 '20 at 17:16
  • 1
    Out of the problem: Java Swing it's a pain. I recommend to use JavaFx. – KunLun Jun 08 '20 at 17:17
  • 1
    @KunLun it is never the right solution solve someone else's swing problem with JavaFX. it is also worth noting that [JavaFX will be removed in Java 11](https://www.oracle.com/technetwork/java/javase/11-relnote-issues-5012449.html). I am not saying that swing is better, or that JavaFX is not a viable alternative. I am just saying that the OP's question is about swing, which leaves JavaFX out of question. – Cardinal System Jun 08 '20 at 17:22
  • @Cardinal-ReinstateMonica So is Oracle going back to Swing? Or is there something else new and shiny for Java GUI programming? – Code-Apprentice Jun 08 '20 at 17:25
  • @NandanA could you please upload your `alarm5.jpg` to this question? – Cardinal System Jun 08 '20 at 17:29
  • @Code-Apprentice again, I am not saying that we should all go back to swing. I am saying that JavaFX is irrelevant because it is completely unrelated to the OP's issue. As for your latter question, (apparently) [the new thing is HTML5](https://www.infoworld.com/article/3261066/javafx-will-be-removed-from-the-java-jdk.html). Not that it really concerns me. I am looking to OpenGL for future UI's. – Cardinal System Jun 08 '20 at 17:37
  • @Cardinal-ReinstateMonica uploaded the alarm5.jpg – Nandan A Jun 08 '20 at 17:49
  • *Both systems have the same resolution settings.* - how about "scaling" settings? Make sure the display works properly without scaling. In any case when you as a question post a proper [mre] that demonstrates the problem. We don't know how you are using the image. We should be able to copy/paste/compile and test the code from a single source file. – camickr Jun 08 '20 at 19:56

1 Answers1

2

The code you posted is not a minimal, reproducible example. I assume you are setting the image as the background for your TestPane class but the code that does that is missing. The Alarm image you posted is HUGE. I scaled it down to 30% of its original size. After that you just need to call setPreferredSize() in the constructor of class TestPane. For me, a width of 450 and a height of 260 worked well, i.e.

public TestPane() {
    setPreferredSize(new Dimension(450, 260);
    // Rest of your code unchanged.
}

This is how it looks on my computer.

alarm

Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    Abra, thank you and I will try this solution. And If you run the code with the dimensions what I have mentioned then are you seeing ```JTextfield``` as []? – Nandan A Jun 08 '20 at 18:57
  • I was using `public Dimension getPreferredSize() {return new Dimension(300, 190);}` in `TestPane` class not in constructor. – Nandan A Jun 08 '20 at 19:15