1 Attachment(s)
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.
Code Java:
/*
* 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);
}
}
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?
1 Attachment(s)
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
Code java:
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);
}