views:

157

answers:

5

When I am running this, JLabel is not visible, but when I resize window (with mouse) JLabel is showed. Why?

import javax.swing.*;
import java.awt.*;

public class FrmTaoLogin extends JFrame {

  private JPanel pnlLeft = new JPanel();

  public FrmTaoLogin() {

    super();

    pnlLeft.setBorder(BorderFactory.createEtchedBorder());
    pnlLeft.add(new JLabel("test1"));
    getContentPane().add(pnlLeft,BorderLayout.SOUTH);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(200, 200); 

  }

  public static void main(String[] args) {

    FrmTaoLogin FrmLogin = new FrmTaoLogin();
    FrmLogin.setVisible(true);

  }

}
+3  A: 

IIRC, this happens when you don't call Frame.pack(). It should work if you call 'pack()' as the last line of the constructor.

Gazzonyx
Actually, `setSize()` should have the effect of calling `pack()` and forcing the frame to lay itself out, shouldn't it?
Joe Carnahan
`setSize()` just tells the frame what size it should be; you need to call `invalidate()` to tell it to recompute its components. However, calling `pack()` as the last step of frame construction is a better habit.
kdgregory
I take that back - `setSize()` does not (in)directly call `pack()`. However, the OP's code worked for me on the first try without adding a call to `pack()`. Adding a call to `pack()` after the `setSize()` actually had the *undesirable* effect of overriding the given size settings, making the window smaller than the 200x200 that the OP wished for.
Joe Carnahan
With pack() all is ok, but window size is not 200x200. :(
rodion
with invalidate I have same effect. :(
rodion
@Joe Good question. I'm not sure now and the Javadocs aren't very clear. JFrame inherits pack() and setSize() from Window. setSize() doesn't specify and pack() says:"If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated."I think the validate method (inherited from Container in the case of JFrame) isn't called from setSize() (it's an expensive call, IIRC), but now I'm not so sure.You might be right about the threading.
Gazzonyx
@rodion Make sure to set minimum (or preferred depending on the LayoutManager) sizes for all of the Components in the JFrame.
Gazzonyx
+1  A: 

I suspect that the problem here may have to do with trying to build and show your GUI components outside of the Swing thread.

What if you change main() to invoke your GUI code on the Swing thread, like this?

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            FrmTaoLogin FrmLogin = new FrmTaoLogin();
            FrmLogin.setVisible(true);
        }
    });
}
Joe Carnahan
with invokeLater I have same effect :(
rodion
If you still have the same problem after putting everything into the Swing thread, then I would try putting components in a JPanel and setting that JPanel as the content pane for your toplevel container instead of adding them to the toplevel container directly, as @kdgregory suggested in the comments above.
Joe Carnahan
Thanx, but it is first, what i am trying :)
rodion
A: 

Very thans to all.

that's is another sample:

//File FrmTaoLogin.java  
package tao;
import javax.swing.*;
import java.awt.*;

import tao.icons.*;

public class FrmTaoLogin extends JFrame {

  private Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

  private JPanel 
    pnlMain = new JPanel(),
    pnlLeft = new JPanel(),
    pnlRight = new JPanel();

  private JLabel 
    lbIco = new JLabel(),
    lbLogin = new JLabel("Login"),
    lbPassword = new JLabel("Password");

  public FrmTaoLogin(String caption, int width, int height) {

    super(caption);

    int x = (dim.width - width) >> 1;
    int y = (dim.height - height) >> 1;

    lbIco.setIcon(Nuvola.actions.lll_kgpg_identity);

    pnlLeft.setPreferredSize(new Dimension(60,height));
    pnlLeft.setMinimumSize(new Dimension(60,height));
    pnlLeft.add(lbIco, BorderLayout.CENTER);
    pnlLeft.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 8));

    pnlRight.setPreferredSize(new Dimension(60,height));
    pnlRight.setMinimumSize(new Dimension(60,height));
    pnlRight.setLayout(new GridLayout(2,1));
    pnlRight.add(lbPassword);
    pnlRight.setBorder(BorderFactory.createEtchedBorder());

    pnlMain.setLayout(new BorderLayout());
    pnlMain.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    pnlMain.add(pnlLeft,BorderLayout.WEST);
    pnlMain.add(pnlRight,BorderLayout.CENTER);
    pnlMain.invalidate();

    this.setContentPane(pnlMain);

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    this.setResizable(false);
    this.setLocation(x, y);     
    this.setSize(width, height);


  }

}

//File vector.java
import tao.FrmTaoLogin;

import javax.swing.*;

public class VectorApp {

  public FrmTaoLogin FrmLogin;

  public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            FrmTaoLogin FrmLogin = new FrmTaoLogin("Login", 300,150);
            FrmLogin.setVisible(true);
        }
    });

  }
}

Result of this code: Basic result

When I am adding pack to end of constructor result is:

With pack() result

When I am remark //this.setResizable(false); result not change, but when i am resizing window manually result is: Without setResizable(false) + manual resizing

When I remark: //pnlRight.add(lbPassword); result is: without pnlRight.add(lbPassword)

Any idea?

Thaks a lot.

rodion
Set minimum size and set preferred size on the JLabels and JPanels before calling pack().
Gazzonyx
A: 

This look like some of the L&F bugs in older Java VMs on newer OS. For example on Windows 7 the most problems are solved first with 1.6.0_17. You should start your program with a console. If you see some stacktraces in the event thread then it is a problem of an L&F bug.

Horcrux7
Thanx to all, problem resolved. I change Windows theme and all working fine. I think that's Windows Aero and my NVIDIA GeForce FX5500 problem. This card official not working with windows Aero.
rodion
A: 

Thanx to all, problem resolved. I change Windows theme and all working fine. I think that's Windows Aero and my NVIDIA GeForce FX5500 problem. This card official not working with windows Aero.

rodion