Swing component's margin in a frame

Hello all,
When you set the bounds of a Swing component in a frame, it will automatically apply those bounds to the VISIBLE part of the frame, so that, a Y value of 0, for example, will not be at the very top of the frame, but right underneath the title bar of the frame. If you give a regular AWT a value of 0 to it's Y coordinate, part (or all) of it will be covered by the title bar of the frame. This does not happen with Swing components, which automatically add the height of the title bar to the value you give to their Y coordinate.
Now, what I want to know is, how can I retrieve the value of that "automatic margin"?. How can I know the number of pixels that the Swing component is being slided down, in order to save the title bar of the frame? This value can not be constant, since it will differ from one platform to another, and from one Look 'n Feel to another, but I have not been able to find out the way to get the value that it's being applied in each case.
As allways, many thanks in advance for your help.

Don't know if there is any direct way to get this information. But, assuming the border width/height is equal on all four sides you could try something like:
Dimension frameD = frame.getSize();
Dimension contentD = frame.getContentPane().getSize();
int borderWidth = frameD.width - contentD.width;
int titleHeight = frameD.height - contentD.height - borderWidth;

Similar Messages

  • How to set a Swing component as Modal window on a SWT Component.?

    How to set a Swing component as Modal on a SWT Component.I mean, I have a window (SWT Container) with some menu items.
    When I click on one menu item then i will get one new Swing Window.
    When creating the new frame I had passed Modal value as TRUE in the constructer.Now child is not behaving as Modal Window.I mean I am able to go to parent window even though the child window is opened as a Modal Window.This is only happened when I double click on the Title Bar of the Parent Window.
    If I try to click on any other part of the Parent Window except Title bar ... the child window is working as a Modal Window.
    Can any one suggest me to solve this one !!

    int this case, do this :
    JFrame f = new JFrame("Authentification");
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(myJPanel, BorderLayout.CENTER);
    f.setBounds(x,y,w,h);
    f.setVisible(true);
    Where myJPanel is the Panel you developped.

  • Swing component's opaque property

    Recenetly I read an article about "Painting in AWT and Swing" and feel confused about some points in the article.
    http://java.sun.com/products/jfc/tsc/articles/painting/index.html
    The article mentioned several Swing Painting Guidelines, I have question about the following two paragraphs:
    6. If a Swing component's opaque property is set to true, then it is agreeing to paint all of the bits contained within its bounds (this includes clearing it's own background within paintComponent()), otherwise screen garbage may result.
    7. Setting either the opaque or optimizedDrawingEnabled properties to false on a component will cause more processing on each paint operation, therefore we recommend judicious use of both transparency and overlapping components.
    For the No.7, it's said setting the opaque to false will cause more process on each paint operation, I think it should changed to ture, since if set opaque property to true, it will paint all the bits contained within bounds as described in No.6.
    So is this an error? What on earth is efficient for the opaque property value?
    Thanks!

    For the No.7, it's said setting the opaque to false will cause more process on each paint operationWhen you create an application you do something like this:
    create a frame
    add a panel to the frame
    add a component to the panel. (lets say a JButton)
    When you click on a button you need to change the state of the button to look 'pressed'. So you need to repaint area on the screen where the button is painted.
    If the button is opaque(true) then you simply paint the button in its new state because the button is responsible for painting its background and any custom painting.
    if the button is opaque(false) then you don't paint the background which means you need to search for the parent of the button (in our example the panel). Once you find the parent you need to paint that area of the panel that the button overlays and then you do the custom painting of the button.
    So either way you have to repaint the same area. Hopefully it makes sense that painting a single component would be more efficient than painting parts of two separate components and determining which two components to repaint and what area to repaint of each component.

  • Flicker with heavyweight swing component and setVisible(false) on Linux

    Hi All,
    I'm using Java 6 update 14 on Ubuntu Jaunty.
    I'm developing a GUI for a system running linux. I'm using the Swing framework for the GUI but need to include 3D animation accelerated in hardware. I'm using the JOGL framework for applying the 3D animations which supplies one with two swing-like components, the GLJPanel and GLCanvas. These two components are lightweight and heavyweight swing components respectively.
    The difficulty arises when adding the heavyweight GLCanvas component into the gui. Often I need to do a setVisible(false) on the component in which case the GLCanvas hides correctly, but shows a flicker before the background is displayed. On digging a little deeper I found that because the GLCanvas is heavyweight, on linux it calls down to the sun.awt.X11 classes to do the setVisible(false) on hide. The very deepest call, the xSetVisible(false) method of the XBaseWindow class, causes the component to be replaced by first a grey backgound, and then the correct background. This intermediate grey background is causing the flicker.
    The source for the sun.awt.X11 packages was not available with the standard JDK 6 but I've found the open jdk source which matches the steps taken by the call. The xSetVisible method is as follows:
    0667:            public void xSetVisible(boolean visible) {
    0668:                if (log.isLoggable(Level.FINE))
    0669:                    log.fine("Setting visible on " + this  + " to " + visible);
    0670:                XToolkit.awtLock();
    0671:                try {
    0672:                    this .visible = visible;
    0673:                    if (visible) {
    0674:                        XlibWrapper.XMapWindow(XToolkit.getDisplay(),
    0675:                                getWindow());
    0676:                    } else {
    0677:                        XlibWrapper.XUnmapWindow(XToolkit.getDisplay(),
    0678:                                getWindow());
    0679:                    }
    0680:                    XlibWrapper.XFlush(XToolkit.getDisplay());
    0681:                } finally {
    0682:                    XToolkit.awtUnlock();
    0683:                }
    0684:            }I've found that the XlibWrapper.XFlush(XToolkit.getDisplay()) line (680) causes the grey to appear and then the XToolkit.awtUnlock(); line repaints the correct background.
    Using the lightwieght GLJPanel resolves this issue as it is a light weight component but unfortunately I'm unable to use it as the target system does not have the GLX 1.3 libraries required because of intel chipset driver issues (it has GLX 1.2). I cannot enable the opengl pipline either (-Dsun.java2d.opengl=True) for the same reason.
    Is there a way to configure a heavyweight component to avoid the operation in line 680? As far as I can tell, the flicker would disappear and the display would still be correctly rendered had this line not been executed. This problem is not present in windows.
    I've put together a minimal example of the problem. It requires the JOGL 1.1.1 libraries in the classpath. They can be found here: [JOGL-download|https://jogl.dev.java.net/servlets/ProjectDocumentList?folderID=11509&expandFolder=11509&folderID=11508]
    I've also found that running the program with the -Dsun.awt.noerasebackground=true vm argument reduces the flicker to just one incorrect frame.
    The program creates a swing app with a button to switch between the GLCanvas and a normal JPanel. When switching from the GLCanvas to the JPanel a grey flicker is noticeable on Linux. Step through the code in debug mode to the classes mentioned above to see the grey flicker frame manifest.
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.media.opengl.GLCanvas;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    import javax.swing.border.EmptyBorder;
    public class SwitchUsingJInternalFrameSwappedExample {
         private static JPanel glPanel;
         private static JPanel mainPanel;
         private static JPanel swingPanel1;
         private static boolean state;
         private static JInternalFrame layerFrame;
         private static GLCanvas glCanvas;
         public static void main(String[] args) {
              state = true;
              JFrame frame = new JFrame();
              frame.setSize(800, 800);
              frame.setContentPane(getMainPanel());
              frame.setVisible(true);
              frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              frame.setBackground(Color.GREEN);
         // The main component.
         public static JPanel getMainPanel() {
              mainPanel = new JPanel();
              mainPanel.setBackground(new Color(0, 0, 255));
              mainPanel.setBorder(new EmptyBorder(new Insets(20, 20, 20, 20)));
              mainPanel.setLayout(new BorderLayout());
              mainPanel.add(getButton(), BorderLayout.NORTH);
              mainPanel.add(getAnimationPanel(), BorderLayout.CENTER);
              return mainPanel;
         // Switch button.
         private static JButton getButton() {
              JButton button = new JButton("Switch");
              button.addActionListener(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                        switchToOpenGl();
              return button;
         // Do the switch.
         protected static void switchToOpenGl() {
              // Make the OpenGL overlay visible / invisible.
              if (!state) {
                   glCanvas.setVisible(false);
              } else {
                   glCanvas.setVisible(true);
              state = !state;
         // Normal swing panel with buttons
         public static JPanel getNormalPanel() {
              if (swingPanel1 == null) {
                   swingPanel1 = new JPanel();
                   for (int i = 0; i < 10; i++) {
                        swingPanel1.add(new JButton("Asdf" + i));
                   swingPanel1.setBackground(Color.black);
              return swingPanel1;
         // Extra panel container just for testing whether background colors show through.
         public static JPanel getAnimationPanel() {
              if (glPanel == null) {
                   glPanel = new JPanel(new BorderLayout());
                   glPanel.setBorder(new EmptyBorder(new Insets(20, 20, 20, 20)));
                   glPanel.setBackground(Color.YELLOW);
                   glPanel.add(getLayerFrame(), BorderLayout.CENTER);
              return glPanel;
         // A component that has two layers (panes), one containing the swing components and one containing the OpenGl animation.
         private static JInternalFrame getLayerFrame() {
              if (layerFrame == null) {
                   // Create a JInternalFrame that is not movable, iconifyable etc.
                   layerFrame = new JInternalFrame(null, false, false, false, false);
                   layerFrame.setBackground(new Color(155, 0, 0));
                   layerFrame.setVisible(true);     
                   // Remove the title bar and border of the JInternalFrame.
                   ((javax.swing.plaf.basic.BasicInternalFrameUI) layerFrame.getUI())
                             .setNorthPane(null);
                   layerFrame.setBorder(null);
                   // Add a normal swing component
                   layerFrame.setContentPane(getNormalPanel());
                   // Add the OpenGL animation overlay on a layer over the content layer.
                   layerFrame.setGlassPane(getGLCanvas());
              return layerFrame;
         // OpenGL component.
         public static GLCanvas getGLCanvas() {
              if (glCanvas == null) {
                   glCanvas = new GLCanvas();
                   glCanvas.setBackground(Color.CYAN);
              return glCanvas;
    }

    Oracle employees typically don't read these forums, you can report bugs here: http://bugs.sun.com/bugdatabase/

  • Changing custom AWT component to Swing component

    I created my own AWT component. I'd like to change it to Swing component.
    Does anyone know how to change or create a custom Swing component?
    Please also provide some framework to develop a custom Swing component.

    First try to know the difference between SWING & AWT. You can either adopt to SWING or AWT but be specific of what you want to do !
    If you like to migrate from AWT to SWING ,then make the necessary changes in your coding.(ie, re frame things like , Frame to JFrame , Button to JButton ,JTextField to JTextField etc..)
    For creating custom components ,say a component called MyLabel and you should design a class which extends JLabel and do whatever you like...
    eg import javax.swing.*;
    public class MyLabel extends JLabel
      MyLabel(String title)
         this.setTitle(title);
         this.setBackground(Color.white);
         this.setForeground(Color.blue);
    }For more details go to java tutorials.

  • Help-using document as a java swing component

    Hi,
    can i use a document excel file or a doc file as a swing component.
    i have few templates in in excel and .doc format.
    i use them to produce bills by just entering data.
    Now i want to develop a swing application and use the same templates in jpanel or jtextpane or jeditorpane.
    its very hectic to make templates in swing using tables and all.
    is there any api which can help me out???
    thanks

    as a Swing component? No.
    There's probably libraries for reading those types of files, and maybe displaying them. POI, perhaps?

  • How to set a Swing component as Modal on a SWT Component?

    How to set a Swing component as Modal on a SWT Componen?.I mean, I have a SWt Component window and from that window I am displaying a SWING Component Modal window.
    The problem is my swing window is not working as Modal always.
    When I opened the swing window from SWT window,swing window is working as Modal.But if Idouble click on Menubar or Title bar of the SWT Window then my Swing window goes back.If I click on any other part of the SWT window.. then once again Swing window is working as modal.
    Can any one suggest me to solve this?
    I think this is isuue related with Swing over SWT..

    pradeep.rajadurai wrote:
    I want set the JTable as a background of jframe or jinternalframe , how it is possible One way that may work, you can always extract the image from a rendered JTable and then paint the same image into the JPanel that holds your components via it's paintComponent method, then added the JPanel to the app's contentPane.
    give me simple pgmIs English your second language? Because if so, please understand that this demand can be taken by some as rude.

  • Support for Multilingual Numeral Input in JTextField swing component

    When the User Locale is changed from the regional & language options in the control panel and the standard digits are customized to a non Latin character set, all the windows applications adhere to the changes made. HTML also respects the changes in effect and displays any numeric values using the new character set, which are the national digits for many Eastern Locales such as Chinese, Arabic(Saudi Arabia), Urdu and many more. The JTextField swing component given by java, however, does not show any support to the new settings. Any numeric input is displayed in the Latin character set even when the input locale of the system is also changed respectively. Any text input for this case is correctly displayed in the desired literals and appropriate glyphs. However, unlike the AWT components, numeral input and display is not catered as per the user/developer's requirements. This behavior was first noticed in 2007, as far as i have found out, and was reported once again on the same thread in 2010. The thread is given below as a reference. No action or response has been taken. Kindly look into this matter and please let me know if this bug has been reported before and if there is any intent of providing a fix for it.
    Reference: http://www.coderanch.com/t/344075/GUI/java/Multilingual-support-JTextField
    Regards,
    Aitzaz Ahmad
    Software Engineer
    SENSYS

    I too had an itch to reply with something like this
    This is a sign of work well done!
    The WD has so strong UI abstraction, that hides client-server nature applications almost completely. If you search forum, you will even find posts where developers try to upgrade value of ProgressMeter in <b>for</b> loop )
    VS

  • Swing component for awt.canvas

    Hi,
    Can anyone tell me the equivalent SWING component for awt CANVAS. I need it because i need to display only Images (nothing else) on it. If anyone can tell how I can do it.

    JPanel is the Swing-equivalent of java.awt.Canvas, but if you are just displaying images, you can use the JLabel class, which supports use of images as icons (see also ImageIcon class).
    Mitch Goldstein
    Author, Hardcore JFC (Cambridge Univ Press)
    [email protected]

  • Swing component fires an event to non-GUI code

    Hi all -- this is my first post in forums.sun.com.
    Question to get me started -
    I have a Swing component that fires an ActionEvent. I would like that ActionEvent to trigger code that does not run on the AWT Event Queue thread (some code that will take some time without impacting GUI rendering performance.)
    I know I could put that Event's action command into a synchronized Queue, and have a worker thread checking the queue and taking action when it finds a command there. Likewise, I could flag a volatile boolean as true, and have a worker thread check the flag and take action when true.
    Or (and I suspect this is best), I could create a new SwingWorker thread right in actionPerformed().
    Any opinions on what makes sense?

    pkwooster: Yep. That's what I meant.
    As for the design pattern, Observer was the pattern I was intending to use (and have already implemented). Would anyone argue that another method is more efficient or "correct"?
    My sample project is:
    Business Object -> Main Dialog -> Embedded Dialog
    The Main Dialog contains no business logic - just simple navigation events. The Embedded Dialog contains the real controls and reports any interaction using Swing events, which the Main Dialog picks up.
    I then set up the Observer pattern between the Main Dialog and the Business Object.

  • Make a PJC Swing component Modal?

    Does anyone know how to keep a PJC that is a Swing component in the same focus as the form?
    In other worsd, when you invoke a spell checker in MS Word the spell check window is in focus when the word window is in focus.
    If a swing component is non modal then it can get lost in the shuffle. Is it possible to force a PJC to be Modal?
    null

    For instance, it looks as though the calendarWidget is non-modal

  • Searching a calendar swing component

    Hello friends.
    I'm looking for a calendar swing component, to make a schedule aplication (like Outlook).
    I found "Mig Java Calendar" (http://www.migcalendar.com/index.php) but it isn't free, i need one like this free.
    Do you know someone free?
    Thank you.

    And if you think, as I do, that a date picker is
    standard component and should be part of the jdk,
    please vote here:
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=473
    0041
    -Pucethat is really helpful. I voted for it.

  • How to call Ocx in swing Component

    Hi i want to play Flash4 file xxx.swf in swing Component
    JMF Api Supports upto Flash2 only i downloadled Flash4 player which is a ocx file .how can i integrate this ocx in swing Component
    help me please
    regards
    Mahesh.B

    I have used the tool JacoZoom by InfoZoom (http://www.infozoom.de).
    The tool can create JCWs (java callable wrappers) for COM and ActiveX objects.
    If they are visual, you can create and display them within your Java app
    either as AWT or in Swing.
    In general, JacoZoom is the best tool I have found so far, it is my
    favorite.
    Sylvia

  • My Swing component's style is changed!

    Hi all. I am using Linux openSuse and from a week I see that my swing component's style is changed! JButton, JPanel, JMenu, JCheckBox and other have a different style and I don't know why... this is an example: http://img527.imageshack.us/img527/715/screen1lg1.png
    How can I return on the original swing component's style? Please help :(

    Anybody can help me? :'(

  • Swing component to use as time editor

    what is the Swing component that we can use to immitate clock component editor in windows?
    you know, the list where you can select/edit hours,minutes seconds .... is it a JList
    can somebody give a small example please
    thanks

    Read the tutorial: [How to Use Spinners|http://java.sun.com/docs/books/tutorial/uiswing/components/spinner.html]
    You will find some examples there.

Maybe you are looking for

  • How could I find which i-views/pages are being used

    Hello I am pritty new in portal area. I have to administrate an ESS/MSS portal during hollydays. I am trying to find everywhere the way. How could I find which i-views/pages are being used. Let say I can see which group I belong and which roles are b

  • How to add an attribute to a notification message ?

    Hi, I want help in adding an attribute to a PO Approval notification message. The attribute is from the PO Header screen , it is the Ship-To field (See screenshot) : http://i48.tinypic.com/2wh35v5.jpg Kindly help me and tell me how i can add this att

  • Can I replace my router (9100EM)?

    I have the 9100EM router and would like to replace it with NETGEAR - N750 Dual Band Wireless-N+ Gigabit Route. Is this possible and if not what can I replace it with that will give me better wireless coverage in my house? Thanks, Mark

  • Read SOAP Header tag

    Hi, I'm using with great success JDEV9.0.3prod for create and deploy web services. I maked all with wizard. All work, but now I would read and manage SOAP header tag. This is the question: how is possible to read the header tag of the SOAP Message, u

  • Where is compatibility view settings in Firefox 3.6.17?

    Windows I.E. has Compatibility View Settings, where as Firefox 3.6.17 does not. Many web pages do not view the same in Firefox as they do with I.E. 8 & 9. The same web page in I.E. is dead on correct, however with Firefox 3.6.17, the same page extend