GlassPane problem

Hi all,
I've tried to put a glasspane over some JButtons/JTable/JComboBox in order to better control the focus & field validation. Here is the Sun official tutorial I am looking at:
http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
Here they emphasis to redispatch mouseDrag event:
"... If a mouse event occurs over the check box or menu bar, then the glass pane redispatches the event so that the check box or menu component receives it. So that the check box and menu behave properly, they also receive all drag events that started with a press in the button or menu bar ..."
Surprise me much, they know the problem but their sample code does not work. When the glasspane is on (visiable), I try to click on the check box, keep mouse pressed and drag off the check box, the check box still stays grayed. Similar thing happens on Jbutton, scroll bar.
Anyone has any idea on this ? Your help will be greatly appreicated.
KC

If you would explain what you are trying to do, then maybe we could actually help you. Post your question in the form of a requirement.
You may be using the wrong approach to solve the problem, but if we don't know what the problem is then we can't offer alternatives.
As a wild guess maybe crwoods DimOverlay example in this posting will help:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=791415

Similar Messages

  • Glasspane event handling problem when window is deactivated - activated

    Hello everyone!
    I have a strange problem concerning glasspane used to block user input. The situation is the following:
    I use a JDesktopPane inside a JFrame. In the desktop pane there are placed multiple JInternalFrames. I use a JPanel as glasspane with dummy listeners to block the controls in one of the internal frames. This all works without problems.
    Now if the JFrame becomes inactive (for example the user switches to a browser window) and the I go back to the java application (frame becoming active again), the blocking no longer works. This also happens if a JDialog pops up - so in general this happens if the DesktopPanes host window becomes inactive and active again.
    The glass pane is still there (see it as it is translucent coloured), gets the events (I can see this from debug output in the dummy listeners) but also the controls underneath the glass panes do react now to user input.
    I already tried to watch the JFrame with a WindowListener and reinstall (a new) glass pane when it becomes active again - no effect. Now I am really helpless - maybe there is something very obvious I am doing wrong?
    I appreciate any hint :-) Thank you in advance!

    New problem :-) Also this worked to block the user input, I now get these:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
         at javax.swing.plaf.basic.BasicInternalFrameUI$Handler.forwardMouseEvent(BasicInternalFrameUI.java:1375)
         at javax.swing.plaf.basic.BasicInternalFrameUI$Handler.mouseEntered(BasicInternalFrameUI.java:1327)
         at java.awt.Component.processMouseEvent(Component.java:5497)
         at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
         at java.awt.Component.processEvent(Component.java:5253)
         at java.awt.Container.processEvent(Container.java:1966)
         at java.awt.Component.dispatchEventImpl(Component.java:3955)
         at java.awt.Container.dispatchEventImpl(Container.java:2024)
         at java.awt.Component.dispatchEvent(Component.java:3803)
         at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
         at java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4017)
         at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3874)
         at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
         at java.awt.Container.dispatchEventImpl(Container.java:2010)
         at java.awt.Window.dispatchEventImpl(Window.java:1774)
         at java.awt.Component.dispatchEvent(Component.java:3803)
         at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
         at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
         at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    Exceptions when I open another JInternalFrame afterwards and then move the mouse into the first internal frame (whose input was blocked by twith the button on glasspane) area.
    Any ideas?

  • Toolbar actions & glasspane display problem.

    Hi,
    I have weird problem. I wrote a calendar application. In the weekly grid, i draw Appointments. I also have a menuBar and a toolBar that have items/buttons for New Appt, Edit Appt, Delete Appt.
    the buttons on the toolbar and the items in the menuBar are linked to the same AbstractAction code.
    when I click on Delete in the menu, it works fine. but when i click on the delete button in the toolbar, it will delete the appointment, but all the other appointments that were on the page are no longer displayed, i have to refresh the applet to see them again.
    i don't understand how the actions in a menubar and a toolbar will lead to different results if they reference the same block of code.
    while screwing around with everything, i've noticed that if i do a glassPane.setVisible(false) (inside of the actionPerformed() in DeleteAction class) before executing the delete code, the other appointments don't disappear. but if i do a glassPane.setVisible(true) after deleting the appointment, they disappear again.
    anybody got some ideas of how to resolve this problem or what may be the cause of it? thanks
    Chris

    I ran your code on Java8b91 (OS X 10.8) and it seemed to work fine.
    Maybe I don't know what kind of error I'm looking for.
    The site you point at is pretty big, so it's hard for me to say what may or may not be wrong with it.
    When posting code, it is best to use code tags: https://forums.oracle.com/forums/ann.jspa?annID=1622
    If you could replace the webEngine.load method with a webEngine.loadContent string and in the string just place the content which you are having issues with, it might help someone help you track down any issues you have - otherwise I don't think you will get any help.
    http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebEngine.html#loadContent(java.lang.String,%20java.lang.String)

  • Glasspane repainting problem

    Hi All,
    I am facing a strange problem with a semi-transparent glass pane. The code is pasted below. Every time the window repaints(by switching between applications), the window becomes darker and darker. and is completely dark after some time. Any solution ?
    Here is the code.
    package glass;
    import java.awt.Color;
    import java.awt.GridBagLayout;
    import java.awt.event.KeyAdapter;
    import java.awt.event.MouseAdapter;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class Class1 extends JFrame{
        public Class1() {
            super();
            initUI();
            pack();
            this.setSize(100,100);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        private void initUI() {
        public static void main(String[] args) {
                Class1 cs = new Class1();
            JTest tst = cs.new JTest();
            cs.setGlassPane(tst);
            tst.setVisible(true);
                cs.setVisible(true);
        private class JTest extends JPanel {
        public JTest() {
            setLayout(new GridBagLayout());
            //setOpaque(false);
            setBackground(new Color(10, 10, 10, 20));
            // Block events
            addMouseListener(new MouseAdapter() {});
            addKeyListener(new KeyAdapter() {});
       

    camickr wrote:
    There are two key points:
    a) the component is opaque, which means the parents background must be painted.
    b) because the component is opaque, we needed to override the paintComponent() method to force the painting of the background using the transparent color
    The concept should work for a JPanel as well as a JComponent.Thank you so much!
    I just wanted to note that this worked for me with some slight modification:
    a) Use setOpaque(false)
    b) Since the component is no longer opaque, it's background color will not be displayed, so overriding the paintComponent method does indeed force painting of the background. But you can't just setBackground(some_color), you actually need to use fillRect to fill the components area with that transluscent color, since the background color won't be displayed if the component isn't set to be opaque.
    -Kyle

  • Repaint-problem with glasspane and mediumweight popup ?!?

    Hi all!
    I hope somebody can give me a push in the right direction.
    Situation is: we have a glasspane for some reason. This glasspane is whole time activated.
    Furthermore we have a special datefield that opens a popup with a calendar-view. Both mechanisms works very well. But if you try to use the datefield under the glasspane then the calendar-view popups but doesn't repaint anymore!
    I have tried a little testprogram and... yes whenever the glasspane is visible the popup does not repaint! Can anybody tells me why?!?
    Here is the example. Left button activates/deactivates the glasspane. When activated the whole panel is shaded red. Right button opens the popup. The popup is again a button. This button doesn't repaint, when the glasspane is active!
    I have tested on Windows XP and used JDK1.4.2_06
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class GlassPaneTest extends JFrame
        private static class GlassPane extends JPanel {
            public GlassPane() {
                setOpaque(false);
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(new Color(255, 0, 0, 10));
                Dimension dim = getSize();
                g.fillRect(0, 0, dim.width, dim.height);
        private GlassPane _glassPane = null;
        private Popup _popup = null;
        public GlassPaneTest() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocation(100, 100);
            setSize(600, 400);
            getContentPane().setLayout(new BorderLayout());
            final JToggleButton buttonGlass = new JToggleButton("GlassPane active");
            buttonGlass.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    if(buttonGlass.isSelected() && _glassPane == null)
                        activateGlassPane(true);
                    else if(!buttonGlass.isSelected() && _glassPane != null)
                        activateGlassPane(false);
            JButton button = new JButton("doIt");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    doIt();
            JPanel topPanel = new JPanel();
            topPanel.setLayout(new GridLayout(1, 0));
            topPanel.add(buttonGlass);
            topPanel.add(button);
            getContentPane().add(topPanel, BorderLayout.NORTH);
            getContentPane().add(new JPanel(), BorderLayout.CENTER);
        private void doIt() {
            hidePopupIfVisible();
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
            JButton button = new JButton("Popupbutton");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    hidePopupIfVisible();
            content.add(button, BorderLayout.CENTER);
            Point p = getLocation();
            p.move(80, 80);
            SwingUtilities.convertPointToScreen(p, this);
            _popup  = PopupFactory.getSharedInstance().getPopup(this, content, p.x, p.y);
            _popup.show();
        private void activateGlassPane(boolean activate) {
            if(activate) {
                _glassPane = new GlassPane();
                getRootPane().setGlassPane(_glassPane);
                _glassPane.setVisible(true);
            else if(_glassPane != null) {
                _glassPane.setVisible(false);
                _glassPane = null;
        private void hidePopupIfVisible() {
            if(_popup != null) {
                _popup.hide();
                _popup = null;
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            catch (Exception e) {
                e.printStackTrace();
            GlassPaneTest gpt = new GlassPaneTest();
            gpt.setVisible(true);
    }

    Thank you Maciej.
    I use L_TO_CREATE_DN to other cases, but, I need indicate the storage unit and L_TO_CREATE_DN proposed me this units.
    I saw in the code that in the popup there isn't the sy-binpt and and so, launch the popup.
    Thank you very much.

  • Problems setting a glasspane on top of JMF

    Hi,
    I've been working on an application that uses JMF for streaming video over the Internet.
    The video is displayed on the client side, and the client should be able to draw a rectangle over the video without stopping it.
    I'm using Swing Components for the GUI, and tried setting a glasspane over the Frame that holds the video (JInternalFrame). For some reason, when I draw the rectangle over the video, it seems to appear behind it. Isn't a glasspane supposed to be on top of everything ?
    Is there a way to force the glasspane to be on top of the JMF video ?
    I tried looking for answers in the forum, but haven't had any luck...
    Thanks in advance for your help,
    tlampo

    Hi tlampo,
    By default, JMF uses heavy weight components.
    You can set JMF to use a light weight component with:
    Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
    Gido..

  • Problems with glassPane

    The glass pane in my application isn't working (painting/there or intercepting events) so I made a small example, I'm sure its something simple, but i can't get the glass pane to show:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class MyGlassPane extends JPanel{
      static public void main(String[] args)
        JFrame f = new JFrame();
        f.setSize(400,400);
        MyGlassPane p = new MyGlassPane();
        p.setVisible(true);
        f.setGlassPane(p);
        f.setVisible(true);
      public void paintComponent(Graphics g)
        g.setColor(Color.red);
        g.fillOval(0, 0, 500, 500);
    }I expect a red oval, but the frame is blank. Can anyone help me find out whats wrong?
    Thanks for your help!
    Edited by: hanvyj on May 15, 2011 6:51 AM

    When you set the glass pane, its <tt>visible</tt> property is copied from that of the pre-existing glass pane. So you have two options:
    -- set the frame's glass pane visible before (edit: or after) substituting your custom glass pane    f.getGlassPane().setVisible(true);
        f.setGlassPane(p);OR
    -- set the custom glass pane visible after setting it to the frame.    f.setGlassPane(p);
        p.setVisible(true);db
    Edited by: Darryl Burke

  • Problem with setOpaque() and transparency.

    Hi,
    I want to use the glass pane of my JFrame to indicate that my application is busy doing something. I want to make the JFrame go a "grey" color, have the busy cursor and be unresponsive to clicks until the task is finished.
    I have included test code to show what I am trying to do. If you click on the "Start Busy" button, you will see what I am trying to achieve.
    My problem is that when my glass pane is painting, I seem to get some underlying components painted in unusual positions. In this example, the "Start Busy" button gets drawn at the top left-hand corner of the JFrame when the glass pane is displayed.
    H:\>java -version
    java version "1.4.2_04"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04
    Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Cursor;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EtchedBorder;
    public class Test {
        public static void main(String args[]) {
            final JFrame test = new JFrame("Transparency Test");
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(new JLabel("NORTH", JLabel.CENTER), BorderLayout.NORTH);
            panel.add(new JLabel("SOUTH", JLabel.CENTER), BorderLayout.SOUTH);
            panel.add(new JLabel("EAST", JLabel.CENTER), BorderLayout.EAST);
            panel.add(new JLabel("WEST", JLabel.CENTER), BorderLayout.WEST);
            JPanel glassPanel = new JPanel();
            glassPanel.setOpaque(true);
            glassPanel.setBackground(new Color(200, 200, 200, 100));
            glassPanel.setLayout(new GridBagLayout());
            glassPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
            JButton busyButton = new JButton("Start Busy");
            busyButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Runnable busy = new Runnable() {
                        public void run() {
                            Component gp = test.getGlassPane();
                            gp.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                            gp.setVisible(true);
                            gp.requestFocus();
                            try {
                                Thread.sleep(5000);
                            } catch (Throwable t) {
                                // Ignore...
                            gp.setVisible(false);
                            gp.setCursor(null);
                    Thread t = new Thread(busy);
                    t.start();
            panel.add(busyButton, BorderLayout.CENTER);
            test.setGlassPane(glassPanel);
            test.getContentPane().add(panel);
            test.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
            test.pack();
            test.show();
    }

    You need to make the glass pane non-opaque so it will repaint the components underneath it. But when you do this the background doesn't get painted, so you need to create a custom component and paint the background yourself:
            JComponent glassPane = new JComponent()
                public void paintComponent(Graphics g)
                    g.setColor( getBackground() );
                    g.fillRect(0, 0, getSize().width, getSize().height);
            glassPane.setOpaque( false );
            glassPane.setBackground( new Color(240, 220, 220, 100) );
            setGlassPane( glassPane );

  • Want a GlassPane for a JPanel! Or do I go MDI?

    Hello,
    We have an Swing application that has been using to the
    the GlassPane in JFrame. Since the application used
    the complete JFrame, it was ok.
    The problem is the new release, requires the effect we
    are using the GlassPane to be localized to a JPanel.
    We are using a gridbaglayout that has other JPanel doing
    other activities.
    The problem is that the GlassPane is in the JFrame.
    So it covers all the other JPanels, when it's used.
    At present the only solution I have come up with is to
    go MDI and have multple JFrames. And redo the entire
    application and layout as a MDI.
    Is there anyway, to just localize a GlassPane to a single
    JPanel.
    Or is the solution MDI approach...
    thanks for your feedback..
    larry

    Here is an example I made :
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class OverlayPanel extends JPanel {
         private GlassPanel glassPanel;
         public JCheckBox check;
         public OverlayPanel() {
              setLayout(new OverlayLayout(this));
              glassPanel = new GlassPanel();
              add(glassPanel);
              add(new SubPanel());
              setPreferredSize(new Dimension(500,500));
              check = new JCheckBox("Enable glass pane for the panel");
              check.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        glassPanel.setVisible(((JCheckBox)e.getSource()).isSelected());
         static class SubPanel extends JPanel {
              Point p = new Point(0,0);
              public SubPanel() {
                   setBackground(Color.white);
                   MouseHandler mh = new MouseHandler();
                   addMouseListener(mh);
                   addMouseMotionListener(mh);
              final class MouseHandler extends MouseInputAdapter {
                   public void mousePressed(MouseEvent e) {
                        p.setLocation(e.getPoint());
                        repaint();
                   public void mouseDragged(MouseEvent e) {
                        p.setLocation(e.getPoint());
                        repaint();
              public void paintComponent(Graphics g) {
                   super.paintComponent(g);
                   g.setColor(Color.red);
                   g.fillOval(p.x-10,p.y-10,20,20);
         static class GlassPanel extends JPanel {
              Point p = new Point(0,0);
              public GlassPanel() {
                   setOpaque(false);
                   MouseHandler mh = new MouseHandler();
                   addMouseListener(mh);
                   addMouseMotionListener(mh);
                   setVisible(false);
              final class MouseHandler extends MouseInputAdapter {
                   public void mousePressed(MouseEvent e) {
                        p.setLocation(e.getPoint());
                        repaint();
                   public void mouseDragged(MouseEvent e) {
                        p.setLocation(e.getPoint());
                        repaint();
              public void paintComponent(Graphics g) {
                   g.setColor(Color.green);
                   g.fillOval(p.x-10,p.y-10,20,20);
         public static void main(String[] args) {
              JFrame f = new JFrame("Test panel glass pane");
              OverlayPanel p = new OverlayPanel();
              f.getContentPane().add(p, BorderLayout.CENTER);
              f.getContentPane().add(p.check, BorderLayout.SOUTH);
              f.pack();
              f.setVisible(true);
    }Denis

  • Problem in Swing Component (JComboBox)

    Hello i've got one amazing problem in my Swing Component (JComboBox) while testing for Glasspane..
    Please check this photo http://www.flickr.com/photos/39683118@N07/4483608081/
    Well i used Netbeans Drag n Drop Swing so the code might be messing..any way my code looks like this:
    My code looks like this:
    public class NewJPanel extends javax.swing.JPanel {
        /** Creates new form NewJPanel */
        public NewJPanel() {
            initComponents();
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
            jTextField1 = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jComboBox1 = new javax.swing.JComboBox();
            jCheckBox1 = new javax.swing.JCheckBox();
            setOpaque(false);
            jTextField1.setText("jTextField1");
            jLabel1.setText("jLabel1");
            jLabel2.setText("jLabel2");
            jLabel3.setText("jLabel3");
            jButton1.setText("jButton1");
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
            jComboBox1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            jComboBox1.setNextFocusableComponent(jCheckBox1);
            jComboBox1.setOpaque(false);
            jComboBox1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jComboBox1ActionPerformed(evt);
            jCheckBox1.setText("jCheckBox1");
            jCheckBox1.setOpaque(false);
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(119, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3))
                    .addGap(51, 51, 51)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jCheckBox1)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(115, 115, 115))
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(70, 70, 70)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGap(15, 15, 15)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jCheckBox1))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jButton1)
                    .addContainerGap(93, Short.MAX_VALUE))
            jComboBox1.getAccessibleContext().setAccessibleParent(null);
        }// </editor-fold>
        private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JCheckBox jCheckBox1;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration
    }

    For more help create a [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://sscce.org], that demonstrates the incorrect behaviour.

  • Repaint() problem

    this is the second thread im posting so if u reply in the first thx,
    but i still have a problem for example lets take this code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.MouseEvent;
    import javax.swing.event.MouseInputAdapter;
    /** An application that requires no other files. */
    public class GlassPaneDemo {
        static private MyGlassPane myGlassPane;
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("GlassPaneDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Start creating and adding components.
            JCheckBox changeButton =
                    new JCheckBox("Glass pane \"visible\"");
            changeButton.setSelected(false);
            //Set up the content pane, where the "main GUI" lives.
            Container contentPane = frame.getContentPane();
            contentPane.setLayout(new FlowLayout());
            contentPane.add(changeButton);
            contentPane.add(new JButton("Button 1"));
            contentPane.add(new JButton("Button 2"));
            //Set up the menu bar, which appears above the content pane.
            JMenuBar menuBar = new JMenuBar();
            JMenu menu = new JMenu("Menu");
            menu.add(new JMenuItem("Do nothing"));
            menuBar.add(menu);
            frame.setJMenuBar(menuBar);
            //Set up the glass pane, which appears over both menu bar
            //and content pane and is an item listener on the change
            //button.
            myGlassPane = new MyGlassPane(changeButton, menuBar,
                                          frame.getContentPane());
            changeButton.addItemListener(myGlassPane);
            frame.setGlassPane(myGlassPane);
            //Show the window.
            frame.pack();
            frame.setVisible(true);
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
    * We have to provide our own glass pane so that it can paint.
    class MyGlassPane extends JComponent
                      implements ItemListener {
        Point point;
        //React to change button clicks.
        public void itemStateChanged(ItemEvent e) {
            setVisible(e.getStateChange() == ItemEvent.SELECTED);
        protected void paintComponent(Graphics g) {
            if (point != null) {
                g.setColor(Color.red);
                g.fillOval(point.x - 10, point.y - 10, 20, 20);
        public void setPoint(Point p) {
            point = p;
        public MyGlassPane(AbstractButton aButton,
                           JMenuBar menuBar,
                           Container contentPane) {
            CBListener listener = new CBListener(aButton, menuBar,
                                                 this, contentPane);
            addMouseListener(listener);
            addMouseMotionListener(listener);
    * Listen for all events that our check box is likely to be
    * interested in.  Redispatch them to the check box.
    class CBListener extends MouseInputAdapter {
        Toolkit toolkit;
        Component liveButton;
        JMenuBar menuBar;
        MyGlassPane glassPane;
        Container contentPane;
        public CBListener(Component liveButton, JMenuBar menuBar,
                          MyGlassPane glassPane, Container contentPane) {
            toolkit = Toolkit.getDefaultToolkit();
            this.liveButton = liveButton;
            this.menuBar = menuBar;
            this.glassPane = glassPane;
            this.contentPane = contentPane;
        public void mouseMoved(MouseEvent e) {
            redispatchMouseEvent(e, false);
        public void mouseDragged(MouseEvent e) {
            redispatchMouseEvent(e, false);
        public void mouseClicked(MouseEvent e) {
            redispatchMouseEvent(e, false);
        public void mouseEntered(MouseEvent e) {
            redispatchMouseEvent(e, false);
        public void mouseExited(MouseEvent e) {
            redispatchMouseEvent(e, false);
        public void mousePressed(MouseEvent e) {
            redispatchMouseEvent(e, false);
        public void mouseReleased(MouseEvent e) {
            redispatchMouseEvent(e, true);
        //A basic implementation of redispatching events.
        private void redispatchMouseEvent(MouseEvent e,
                                          boolean repaint) {
            Point glassPanePoint = e.getPoint();
            Container container = contentPane;
            Point containerPoint = SwingUtilities.convertPoint(
                                            glassPane,
                                            glassPanePoint,
                                            contentPane);
            if (containerPoint.y < 0) { //we're not in the content pane
                if (containerPoint.y + menuBar.getHeight() >= 0) {
                    //The mouse event is over the menu bar.
                    //Could handle specially.
                } else {
                    //The mouse event is over non-system window
                    //decorations, such as the ones provided by
                    //the Java look and feel.
                    //Could handle specially.
            } else {
                //The mouse event is probably over the content pane.
                //Find out exactly which component it's over. 
                Component component =
                    SwingUtilities.getDeepestComponentAt(
                                            container,
                                            containerPoint.x,
                                            containerPoint.y);
                if ((component != null)
                    && (component.equals(liveButton))) {
                    //Forward events over the check box.
                    Point componentPoint = SwingUtilities.convertPoint(
                                                glassPane,
                                                glassPanePoint,
                                                component);
                    component.dispatchEvent(new MouseEvent(component,
                                                         e.getID(),
                                                         e.getWhen(),
                                                         e.getModifiers(),
                                                         componentPoint.x,
                                                         componentPoint.y,
                                                         e.getClickCount(),
                                                         e.isPopupTrigger()));
            //Update the glass pane if requested.
            if (repaint) {
                glassPane.setPoint(glassPanePoint);
                glassPane.repaint();
    }which is the glasspanedemo from the swing tutorial
    url: java.sun.com/docs/books/tutorialJWS/uiswing/components/example-1dot4/GlassPaneDemo.jnlp - 1k
    how can i make the dots not to disapear after i click again,meaning when i activate the glass pane i want all the dots i pressed to b shown and my problem is that the repaint function delete the earlier points every time,
    thx.

    first 1 was a few days ago and i cant find it so sry,So I did waste my time by responding to your posting. Good to know, I won't waste my time by responding in the future.
    Learn how to use the forum properly and add all postings you create to your watch list automatically.
    I can find it. I just click on your userid for you most recent postings. Now how difficult is that?
    anyway it wasn't very helpfull since i didn't post any code there,You where given two suggestions. You should be replying stating whether they where helpfull or not (at least if you want help from me in the future).

  • Dialog Box Problem

    i have created dialog box manually using stage and modility. the problem is when the dialog box executes the code following dialog box
    gets executed before closing the dialog box. i want to use it like confirmation box in swing. i want to stop further execution of code till yes/no button of dialog box clicked.please suggest... please help me....

    Ok, so it doesn't look like there is an easy way to do modality in JFX. I had thought since Swing does and JFX uses the same threading model that it should be do-able, but from what I can tell, Swing does some serious magic under the covers to make modal dialogs work.
    Your best bet probably is to use callbacks instead. Make your own dialog window and instead of doing something like this:
    int choice = MyDialog.show("Are you sure you want to do that?");
    if (choice == MyDialog.OK)
        doTheAction();
    } Do something more like:
    MyDialog dialog = new MyDialog("Are you sure you want to do that?");
    dialog.setOnOk(new EventHandler<ActionEvent>()
        public void handle(ActionEvent actionEvent)
            doTheAction();
    });In fairness, that is probably a bit more 'correct' anyway and inline with the JFX style of doing things (although it is more code).
    Here's some very, very rough code for doing something like what you want. You could obviously customise the class to take an enum of the button options available etc. It could all be improved a lot.
    public class TestApp extends Application
        public static void main(String[] args) throws Exception
            launch(args);
        public void start(final Stage stage) throws Exception
            StackPane rootPane = new StackPane();
            FlowPane contentPane = new FlowPane();
            BorderPane dialogContent = new BorderPane();
            dialogContent.setStyle("-fx-background-color: white");
            dialogContent.setCenter(new Label("This is a dialog"));
            final Dialog dialog = new Dialog(dialogContent);
            dialog.setOnOk(new EventHandler<ActionEvent>()
                public void handle(ActionEvent actionEvent)
                    System.out.println("Ok selected");
            dialog.setOnCancel(new EventHandler<ActionEvent>()
                public void handle(ActionEvent actionEvent)
                    System.out.println("Cancel selected");
            Button showDialogButton = new Button("Show Dialog");
            showDialogButton.setOnAction(new EventHandler<ActionEvent>()
                public void handle(ActionEvent actionEvent)
                    dialog.show(stage);
            contentPane.getChildren().add(showDialogButton);
            rootPane.getChildren().add(contentPane);
            BorderPane glassPane = new BorderPane();
            glassPane.setStyle("-fx-background-color: rgba(200, 200, 200, 0.5)");
            glassPane.visibleProperty().bind(dialog.showingProperty());
            rootPane.getChildren().add(glassPane);
            Scene scene = new Scene(rootPane, 300, 300);
            stage.setScene(scene);
            stage.show();
        private class Dialog extends Popup
            private BorderPane root;
            private EventHandler<ActionEvent> onOk;
            private EventHandler<ActionEvent> onCancel;
            private Button cancelButton;
            private Dialog(Node content)
                root = new BorderPane();
                root.setPrefWidth(200);
                root.setPrefHeight(200);
                root.setStyle("-fx-background-color: white; -fx-border-width: 1; -fx-border-color: gray");
                root.setTop(buildTitleBar());
                root.setCenter(content);
                root.setBottom(buildButtonBar());
                getContent().add(root);
            public void setOnOk(EventHandler<ActionEvent> onOk)
                this.onOk = onOk;
            public void setOnCancel(EventHandler<ActionEvent> onCancel)
                this.onCancel = onCancel;
            public void setContent(Node content)
                root.setCenter(content);
            private Node buildTitleBar()
                BorderPane pane = new BorderPane();
                pane.setStyle("-fx-background-color: #0000aa; -fx-text-fill: white; -fx-padding: 5");
                pane.setOnMouseDragged(new EventHandler<MouseEvent>()
                    public void handle(MouseEvent event)
                        // not sure why getX and getY don't work
                        // double x = getX() + event.getX();
                        // double y = getY() + event.getY();
                        double x = event.getScreenX();
                        double y = event.getScreenY();
                        setX(x);
                        setY(y);
                Label title = new Label("My Dialog");
                pane.setLeft(title);
                Button closeButton = new Button("X");
                closeButton.setOnAction(new EventHandler<ActionEvent>()
                    public void handle(ActionEvent actionEvent)
                        hide();
                pane.setRight(closeButton);
                return pane;
            protected Node buildButtonBar()
                FlowPane pane = new FlowPane(6, 6);
                pane.setAlignment(Pos.CENTER);
                Button okButton = new Button("OK");
                okButton.setOnAction(new EventHandler<ActionEvent>()
                    public void handle(ActionEvent actionEvent)
                        hide();
                        if (onOk != null)
                            onOk.handle(actionEvent);
                pane.getChildren().add(okButton);
                Button cancelButton = new Button("Cancel");
                cancelButton.setOnAction(new EventHandler<ActionEvent>()
                    public void handle(ActionEvent actionEvent)
                        hide();
                        if (onCancel != null)
                            onCancel.handle(actionEvent);
                pane.getChildren().add(cancelButton);
                return pane;
    }

  • Frame(sprite) animation delay problem

    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    public class ExampleTrialCanvas extends GameCanvas implements Runnable {
    private boolean isPlay; // Game Loop runs when isPlay is true
    private long delay; // To give thread consistency
    private int currentX, currentY; // To hold current position of the 'X'
    private int width; // To hold screen width
    private int height; // To hold screen height
    // Sprites to be used
    // private Sprite playerSprite;
    private Sprite backgroundSprite;
    private Sprite playerSprite;
    //private int i;
    // Layer Manager
    private LayerManager layerManager;
    // Constructor and initialization
    public ExampleTrialCanvas() throws Exception {
    super(true);
    width = getWidth();
    height = getHeight();
    currentX = 64;
    currentY = 150;
    delay = 100;
    // Load Images to Sprites
    Image playerImage = Image.createImage("/transparent.png");
    playerSprite = new Sprite (playerImage,32,32);
    Image backgroundImage = Image.createImage("/gillete.png");
    backgroundSprite = new Sprite(backgroundImage);
    layerManager = new LayerManager();
    layerManager.append(playerSprite);
    layerManager.append(backgroundSprite);
    // Automatically start thread for game loop
    long now, prev_time_moved;
    public void init(){
    now = System.currentTimeMillis();
    prev_time_moved = now;
    public void start() {
    isPlay = true;
    Thread t = new Thread(this);
    t.start();
    public void stop() { isPlay = false; }
    // Main Game Loop
    public void run() {
    Graphics g = getGraphics();
    while (isPlay == true) {
    input();
              drawScreen(g);
    try { Thread.sleep(delay); }
    catch (InterruptedException ie) {}
    // Method to Handle User Inputs
    private void input()
    int keyStates = getKeyStates();
    playerSprite.setFrame(0);
    // Up
    if ((keyStates & UP_PRESSED) != 0) {
         for(int i=0;i<5;i++)
         playerSprite.setFrame(i);
    // Method to Display Graphics
    private void drawScreen(Graphics g) {
    // updating player sprite position
    playerSprite.setPosition(currentX,currentY);
    // display all layers
    layerManager.paint(g,0,0);
    layerManager.setViewWindow(55,20,180,180);
    //layerManager.paint(g,20,20);
    flushGraphics();
    We want, on right click botton press the frame should animate.
    We have a player with 5 different poses, kicking a ball, "image width is 160, height is 32".
    the problem is that, the player does move through allthe frames. he starts from 0th p\frame and directly end at the last frame, withopur any delay between frames.
    we have used the for loop as follows.
    if ((keyStates & UP_PRESSED) != 0) {
         for(int i=0;i<5;i++)
         playerSprite.setFrame(i);
    we want a delay code between all the frames.

    Maybe this will help you!
    btnSignIn.addActionListener(new ActionListener()     
                                  public void actionPerformed(ActionEvent e)
                                      //display the glasspane first
                                      setGlassPane(new MyGlassPane(newAnimatedIcon("images/LoginWaitingIcon.png")));
                                      getupGlasPane.setVisible(true);        //expected the glassPane shows (it does), and the animation starts (it doesnt)
                                      Thread t1 = new Thread(new Runnable()    //The animation only starts when this thread finished
                                            public void run()
                                                 //DO THE REST
    t1.start();
    t1.join();
    setupGlasPane(false);   //finish, turning off
                       });

  • Repainting the GlassPane (Drag and Drop)

    I have A DesktopPane with a glassPane.
    The glassPane paints the outline of the components on the desktopPane That I'll be dragging with the mouse
    I need to be able to move and repaint the outline as I drag the mouse
    Every mouseDrag event I call repaint on the glasspane but the outline does not keep up with the mouse. It falls behind.
    If I call paint directly the outline keeps up with the mouse but it leaves garabge behind.
    How can I speed up the process without leaving garbage behind?
    A previous version of my software (which I inherited) was able to keep up but it was not object oriented so I had to change it to a more OO version to fix other no related problems.
    The older version called an erase method followed by a call to paint directly. The erase method would paint over the old outline that also involved a direct call to the paint method. The problem is that the erase method method won't work anymore because the layers have changed.
    painting the outline on the new position.
    Does anyone have a fast reapint algorithm that may work?
    Thank

    Hi Andy,
    Just make sure you are using Live View for drag and drop as it doesn't work with design view.
    Which icon is not showing up? Did you get a text field to edit the asset name if required?
    Please elaborate a bit more.
    Thanks,
    Lalita

  • Mouse moving crosshair over image problem

    Hello,
    I� am quite a newbe at Java and using the trail and error development method (or copy and paste method) �
    Now I� am facing the following problem:
    I made a program with an image that is draw out of received data. After that I want to draw a huge cursor crosshair (two lines at image size) over that image. It should move when I move my mouse.
    I can�t get it done by redrawing the entire image every mouse motion. That will result in a slow motion crosshair.
    The program start with:
    public class MyProg extends javax.swing.JFrame {
    Container cont;
    public byte[] BulkIn = new byte[6000];
    public DrawField drawscreen;
    public CrossHair crosshair;
    /** Creates new form MyProg */
    public MyProg() {
    drawscreen = new DrawField(BulkIn);
    �.
    The DrawField part (where the image is drawn) start like:
    class DrawField extends JPanel implements Printable {
    byte[] BulkData;
    Lines BlueLine,RedLine,WhiteLine,GreenLine,CyanLine,YellowLine,OrangeLine,PinkLine;
    static BufferedImage imgs;
    static BufferedImage raster;
    static BufferedImage buffImg;
    �..
    The CrossHair part starts with:
    class CrossHair extends JComponent{
    Container cont ;
    int x_Pos,y_Pos;
    int x_offset,x_end,y_offset,y_end;
    public void paint(Graphics g) {
    g.setColor(Color.yellow);
    g.drawLine(x_offset,y_Pos,x_end,y_Pos);
    g.drawLine(x_Pos,y_offset,x_Pos,y_end);
    �.
    Now, when I reach crosshair.repaint(); nothing happens.
    When I change "class CrossHair extends Jcomponent" into "class CrossHair extends Jpanel", the crosshair is drawn perfectly, however, my original image is gone.
    My intention was to draw the crosshair on some kind of glasspane, but until now with no success.
    Help!

    You'll have to redraw everything each time you reposition the cursor.
    Your best bet to speed things up (and look nice) are:
    1. Use double buffering
    2. Redraw only over the area where the cursor needs to first be erased (ie might only be a 30x30 area). You can achieve this by setting the clipping area of the pictures drawn.

Maybe you are looking for