0

Can someone help me with my code, please. I am expiriencing strange behaviour in Eclipse.

Sometimes my application loads correctly and most of times I don't get North and South JPanels loaded properly or not loaded at all.

After loading GUI displays correctly only when I resize JFrame borders, but I would like to load every time correctly. I guess I have some trouble in my code but I can't find it. And btw, I am running Linux Mint 19.1 Tarra with java-11-openjdk-amd64. Here is code for Main class:

package gui;


public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    new Dakijevstina();
}

}

And here is GUI class code:

package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;


@SuppressWarnings("serial")
public class Dakijevstina extends JFrame{

Font font = new Font("TimesNew Roman", Font.PLAIN, 12);

public Dakijevstina() {
    initComponents();
}

public void initComponents() {
    //setting up JFrame     
    setTitle("Market garden financial software");
    setFont(font);
    setLayout(new BorderLayout());
    setSize(640, 480);
    setDefaultCloseOperation(Dakijevstina.EXIT_ON_CLOSE);
    setLocationByPlatform(true);
    setVisible(true);

    //setting up North JPanel
    JPanel pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pnlNorth.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
    add(pnlNorth, BorderLayout.NORTH);

    //setting up StatusBar
    JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pnlSouth.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
    add(pnlSouth, BorderLayout.SOUTH);

    //adding buttons to north JPanel
    //About
    JButton btnAbout = new JButton();
    JButton btnExit = new JButton();
    try {
        Image img = ImageIO.read(getClass().getResource("/images/about.png"));
        Image img2 = ImageIO.read(getClass().getResource("/images/exit.png"));
        btnAbout.setIcon(new ImageIcon(img));
        btnExit.setIcon(new ImageIcon(img2));
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, ex);
    }
    btnAbout.setPreferredSize(new Dimension(40, 40));
    btnAbout.setMinimumSize(new Dimension(40, 40));
    btnAbout.setToolTipText("Information about author");
    btnExit.setPreferredSize(new Dimension(40, 40));
    btnExit.setMinimumSize(new Dimension(40, 40));
    btnExit.setToolTipText("Exit application");
    btnExit.addActionListener(e -> exitApp());
    pnlNorth.add(btnAbout);
    pnlNorth.add(btnExit);

    //adding StatusBar to south JPanel
    JLabel status = new JLabel("Welcome to Dakijevstina software");
    status.setFont(font);
    pnlSouth.add(status);
}

//event handlers
public void exitApp() {
    this.dispose();
}

This is what I get most of time: unwanted behaviour

And this is how it supose to be: this is what I want

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    set Visible in the constructor, that is not good. Try this to start a Swing app: https://stackoverflow.com/questions/18976990/best-practice-to-start-a-swing-application – PeterMmm Dec 23 '18 at 14:31
  • Thanks for answer. I modified my code acording to article you linked and working flawlessly now. – Dalibor Dec 23 '18 at 16:36
  • 1
    Also all swing apps should run in EDT. – George Z. Dec 23 '18 at 16:38

0 Answers0