Results 1 to 6 of 6
  1. #1
    fernandoj is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default How to center a window with Container [SOLVED] [Thanks!]

    Hi I'm using a container class only object. Not using JFrame window which is centered with frame.setLocationRelativeTo(null); sentence.

    I just want the splash screen of my application appears in the center of the desktop not in the upper-left part.

    Any solution? I'm new with Java :D thanks in advance

    This is the code

    public class MenuSwing3 extends JFrame {

    Container contenedor;
    JTextArea areaTexto;

    JMenuBar barraMenu = new JMenuBar();
    JMenu menuFirmar = new JMenu("Firmar");
    JMenu menuValidar = new JMenu("Validar");
    JMenu menuAyuda = new JMenu("Ayuda");
    JMenuItem opcionEnvelope = new JMenuItem("Documento XML en formato Envelope");
    JMenuItem opcionEnveloping = new JMenuItem("Documento XML en formato Enveloping");
    JMenuItem opcionDetached = new JMenuItem("Documento anexo - formato Detached");
    JMenuItem opcionSalirF = new JMenuItem("Salir");

    //Constructor de la clase MenuSwing3
    public MenuSwing3() {

    super("Firma digital de documentos");

    contenedor = getContentPane();
    menuFirmar.add(opcionEnvelope);
    menuFirmar.add(opcionEnveloping);
    menuFirmar.add(opcionDetached);
    menuFirmar.add(opcionSalirF);
    barraMenu.add(menuFirmar);
    barraMenu.add(menuValidar);
    barraMenu.add(menuAyuda);
    setJMenuBar(barraMenu);
    addWindowListener(new WindowHandler());
    opcionEnvelope.addActionListener(new VigilaMenu());
    opcionEnveloping.addActionListener(new VigilaMenu());
    opcionDetached.addActionListener(new VigilaMenu());
    opcionSalirF.addActionListener(new VigilaMenu());
    setSize(512,384);
    areaTexto = new JTextArea();
    contenedor.add("Center",areaTexto);
    setIconImage(Toolkit.getDefaultToolkit().createIma ge("Icono.gif"));
    show();
    }
    Attached Thumbnails Attached Thumbnails How to center a window with Container-sin-t-tulo2.jpg  
    Last edited by fernandoj; 05-09-2010 at 10:57 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    I don't understand your question. You are using a JFrame. You are aware of the setLocationRelativeTo( null ) method. Basic code should be:

    Java Code:
    setSize(...);
    setLocationRelativeTo( null );
    setVisible( true );
    If you need more help post your SSCCE.

  3. #3
    fernandoj is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you very much. You are right and it works!

    I forgot that class extended JFrame. The confusion came because Container is upper in the heritance so I thought I had to use only the methods for Container class.



    By the way, what's better? the show(); method or setVisible( true );

    I hope in a few weeks I can manage better with Java :p

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    By the way, what's better? the show(); method or setVisible( true );
    Read the API description for both methods to see whast the difference is.

  5. #5
    fernandoj is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Read the API description for both methods to see whast the difference is.
    It seems show method is deprecated! I will use setVisible :D

    Java Code:
    setVisible
    
    public void setVisible(boolean b)
    
        Shows or hides this component depending on the value of parameter b.
    
        Parameters:
            b - if true, shows this component; otherwise, hides this component
        Since:
            JDK1.1
        See Also:
            isVisible()
    
    show
    
    @Deprecated
    public void show()
    
        Deprecated. As of JDK version 1.1, replaced by setVisible(boolean).

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    It seems show method is deprecated!
    Exactly. I'm surprised the compiler doesn't give you warning messages. I hope you don't ignore them.

Similar Threads

  1. Replies: 4
    Last Post: 01-10-2010, 07:48 AM
  2. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 12:17 PM
  3. how can i move one frame window to another window
    By santhosh_el in forum AWT / Swing
    Replies: 8
    Last Post: 06-10-2009, 03:36 PM
  4. Replies: 3
    Last Post: 04-21-2009, 11:01 PM
  5. Help Center Live 2.1.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-06-2007, 03:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •