Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Pause Overlay for JScrollPane

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pause Overlay for JScrollPane

    Here's what I'm trying to do:
    When the user pauses my application I want to grey-out the data area and display the word "Paused..." over it. Ideally, the user will still be able to see the data and scroll through it.

    What I've tried:
    This seems to have me about 75% there. I extended JLayeredPane to OverlayPane. What this does is add a JPanel that stays on top of everything else that gets added to the container and is the same size as the container. I have overridden the: add, remove, removeAll, moveToFront, moveToBack, setBounds and setLocation methods for this purpose.

    What's happening:
    When I click Pause the overlay becomes visible the word "Paused..." appears and I can see the data through the panel. However, when I scroll the data the panel does not get redrawn, just the JScrollPane the data resides in. I think I need to add some listeners to trigger a redraw of the overlay if it is set to visible, but I'm not sure what listener or where to add it.

    edit by helloworld922: In the future please post your code in the post, particularly when it's not too long.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package com.hammers.itos.schedule_executor;
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.Rectangle;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
     
    /**
     *
     * @author Sean Smitz
     */
    public class OverlayPane extends JLayeredPane{
        private JPanel overlay;
     
        public OverlayPane(){
            super();
     
            overlay = new JPanel();
            overlay.setBounds(getBounds());
            overlay.setBackground(new Color(51, 51, 51, 128));
            overlay.setForeground(Color.WHITE);
     
            add(overlay);
            moveToFront(overlay);
        }
     
        public Color getOverlayBackgroundColor(){ return overlay.getBackground(); }
        public Color getOverlayForegroundColor(){ return overlay.getForeground(); }
     
        public void setOverlayBackgroundColor(Color color){
            overlay.setBackground(color);
        }
        public void setOverlayForegroundColor(Color color){
            overlay.setForeground(color);
        }
     
        public Component addComponentToOverlay(Component comp){
            return overlay.add(comp);
        }
        public void removeComponentFromOverlay(Component comp){
            overlay.remove(comp);
        }
        public void removeAllComponentsFromOverlay(){
            overlay.removeAll();
        }
     
        public boolean isOverlayVisible(){ return overlay.isVisible(); }
        public void setOverlayVisible(boolean aFlag){ overlay.setVisible(aFlag); }
     
        @Override
        public Component add(Component comp){
            Component ret = null;
     
            ret = super.add(comp);
     
            moveToFront(overlay);
     
            return ret;
        }
     
        @Override
        public void remove(int index){
            if(index != this.getIndexOf(overlay))
                super.remove(index);
        }
     
        @Override
        public void removeAll(){
            for(int i = 0; i < this.getComponentCount(); i++){
                remove(i);
            }
        }
     
        @Override
        public void moveToFront(Component comp){
            super.moveToFront(comp);
            super.moveToFront(overlay);
        }
     
        @Override
        public void moveToBack(Component comp){
            super.moveToBack(comp);
            super.moveToFront(overlay);
        }
     
        @Override
        public void setBounds(Rectangle r){
            setBounds(r.x, r.y, r.width, r.height);
        }
        @Override
        public void setBounds(int x, int y, int width, int height){
            super.setBounds(x, y, width, height);
            overlay.setBounds(x, y, width, height);
        }
     
        @Override
        public void setLocation(Point p){
            setLocation(p.x, p.y);
        }
        @Override
        public void setLocation(int x, int y){
            super.setLocation(x, y);
            overlay.setLocation(x, y);
        }
     
        @Override
        public void setMaximumSize(Dimension maximumSize) {
            super.setMaximumSize(maximumSize);
            overlay.setMaximumSize(maximumSize);
        }
     
        @Override
        public void setMinimumSize(Dimension minimumSize) {
            super.setMinimumSize(minimumSize);
            overlay.setMinimumSize(minimumSize);
        }
     
        @Override
        public void setPreferredSize(Dimension preferredSize) {
            super.setPreferredSize(preferredSize);
            overlay.setPreferredSize(preferredSize);
        }
     
        @Override
        public void setSize(Dimension d) {
            setSize(d.width, d.height);
        }
     
        @Override
        public void setSize(int width, int height) {
            super.setSize(width, height);
            overlay.setSize(width, height);
        }
    }
    Attached Files Attached Files
    Last edited by helloworld922; July 6th, 2010 at 09:29 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Pause Overlay for JScrollPane

    How are you "disabling" the elements in the background? The best way I can think of in this situation is to actually just make all the components read-only so they can't be changed. This also allows Java's Swing toolkit to handle all necessary repaints for you. The actual method for making items read-only depends on the item, can you post your code for where you're handling the pause button being pressed?

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pause Overlay for JScrollPane

    Right now, nothing inside the OverlayPane should stop updating. The pause is to prevent the rest of the application from executing. Here's the code for the pause/resume that involves the overlay and the code that instantiates it in the larger application. I'm just trying to give a better visual indication that the application is paused.

    Pause/Resume Button clicked
    private void buttonPauseResumeMouseClicked(java.awt.event.MouseEvent evt) {                                               
        conf.pauseResume.toggle();
        if(conf.pauseResume.getStatus() == PauseResume.stat.PAUSED) {
            parent.createPauseFile();
            overlayPanel.setOverlayVisible(true);
        } else if(conf.pauseResume.getStatus() == PauseResume.stat.RUNNING ) {
            parent.deletePauseFile();
            overlayPanel.setOverlayVisible(false);
        }
        pauseResumeManager(false);
    }
     
    private void initMyComponents() {
        Rectangle r = getBounds();
        JLabel text = new JLabel("Paused...");
     
        r.x += 10;
        r.y += 10;
        r.width -= 30;
        r.height -= 180;
     
        overlayPanel = new OverlayPane();
     
        text.setBounds(r);
        text.setForeground(overlayPanel.getOverlayForegroundColor());
        text.setFont(new Font("Arial", Font.BOLD, 48));
        text.setHorizontalAlignment(JLabel.CENTER);
        text.setVerticalTextPosition(JLabel.CENTER);
     
        overlayPanel.setBounds(r);
        overlayPanel.addComponentToOverlay(text);
     
        tableDisplay = new JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
     
        tableDisplay.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableDisplay.setFocusable(false);
        tableDisplay.setRowSelectionAllowed(false);
     
        r.width -= 10;
        r.height -= 10;
     
        jScrollPane1.setViewportView(tableDisplay);
        jScrollPane1.setBounds(r);
        overlayPanel.add(jScrollPane1);
     
        add(overlayPanel);
    }
    Attached Files Attached Files
    Last edited by NotNormal; July 7th, 2010 at 06:55 AM. Reason: To post the code

Similar Threads

  1. [SOLVED] need help, Problem in JScrollPane
    By sny in forum AWT / Java Swing
    Replies: 5
    Last Post: March 15th, 2010, 07:16 AM
  2. JScrollPane
    By MysticDeath in forum AWT / Java Swing
    Replies: 1
    Last Post: February 17th, 2010, 09:21 PM
  3. JTable in JScrollPane
    By alwayslearner in forum AWT / Java Swing
    Replies: 1
    Last Post: January 31st, 2010, 09:24 PM
  4. JTable in JScrollPane
    By alwayslearner in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 29th, 2010, 10:42 AM
  5. Delay/Wait/Pause in Java + AirBrushing
    By obliza in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 27th, 2009, 10:27 AM