이런 방식이 있다는 설명으로, 실제 사용 시에는 SwingWorker를 같이 사용해야 한다!
1. GlassPane
작성한 값 Panel을 setGlassPane(buildGlass())로 넣어주고, visible로 사용하면 된다.
보여줄 내용이 있으면 적용이 다양하게 적용 가능하다.
//생성
private JPanel buildGlass() {
glass = new JPanel(new GridBagLayout());
glass.setOpaque(false);
glass.setCursor(new Cursor(Cursor.WAIT_CURSOR));
glass.addMouseListener(new java.awt.event.MouseAdapter() {}); //뒤쪽 클릭 막기
JLabel label = new JLabel("조회 중...");
label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
label.setOpaque(true);
label.setBackground(new Color(50, 50, 50));
label.setForeground(Color.WHITE);
label.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(100, 100, 100), 1),
BorderFactory.createEmptyBorder(12, 32, 12, 32)
));
glass.add(label);
return glass;
}
//넣기
setGlassPane(buildGlass());
//사용법
glass.setVisible(true); -> 작업이 끝나면 glass.setVisible(false);
label의 값도 변경하면서 사용하면 된다.
2. dialog
private void buildDialog() {
dialog = new JDialog(this, true); // modal
dialog.setUndecorated(true); // 타이틀바 제거
dialog.setBackground(new Color(0, 0, 0, 0)); // 투명
JLabel lblMsg = new JLabel("조회 중...");
lblMsg.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
lblMsg.setOpaque(true);
lblMsg.setBackground(new Color(50, 50, 50));
lblMsg.setForeground(Color.WHITE);
lblMsg.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(100, 100, 100), 1),
BorderFactory.createEmptyBorder(12, 32, 12, 32)
));
JPanel panel = new JPanel(new GridBagLayout());
panel.setOpaque(false); //배경 지우기
panel.add(lblMsg);
dialog.setContentPane(panel);
dialog.pack();
dialog.setLocationRelativeTo(this);
}
//생성
//사용 시
dialog.setVisible(true) -> 작업 끝나면 dialog.dispose()

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class OverlayTest extends JFrame {
private JPanel glass;
private JDialog dialog;
public OverlayTest() {
super("Overlay");
setLayout(new FlowLayout(FlowLayout.LEFT));
JButton btnGlass = new JButton("Glass 조회");
JButton btnDialog = new JButton("Dialog 조회");
add(btnGlass);
add(btnDialog);
setGlassPane(buildGlass());
buildDialog();
btnGlass.addActionListener(e -> {
searchGlass();
});
btnDialog.addActionListener(e -> {
searchDialog();
});
pack();
setLocationRelativeTo(null);
}
private void searchGlass() {
glass.setVisible(true);
Timer timer = new Timer(1000 * 5, null);
timer.addActionListener(e -> {
glass.setVisible(false);
timer.stop();
});
timer.setRepeats(false);
timer.start();
}
private void searchDialog() {
SwingUtilities.invokeLater(() -> dialog.setVisible(true));
Timer timer = new Timer(1000 * 5, null);
timer.addActionListener(e -> {
dialog.dispose();
timer.stop();
});
timer.setRepeats(false);
timer.start();
}
private JPanel buildGlass() {
glass = new JPanel(new GridBagLayout());
glass.setOpaque(false);
glass.setCursor(new Cursor(Cursor.WAIT_CURSOR));
glass.addMouseListener(new java.awt.event.MouseAdapter() {}); //뒤쪽 클릭 막기
JLabel label = new JLabel("조회 중...");
label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
label.setOpaque(true);
label.setBackground(new Color(50, 50, 50));
label.setForeground(Color.WHITE);
label.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(100, 100, 100), 1),
BorderFactory.createEmptyBorder(12, 32, 12, 32)
));
glass.add(label);
return glass;
}
private void buildDialog() {
dialog = new JDialog(this, true); // modal
dialog.setUndecorated(true); // 타이틀바 제거
dialog.setBackground(new Color(0, 0, 0, 0)); // 투명
JLabel lblMsg = new JLabel("조회 중...");
lblMsg.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
lblMsg.setOpaque(true);
lblMsg.setBackground(new Color(50, 50, 50));
lblMsg.setForeground(Color.WHITE);
lblMsg.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(100, 100, 100), 1),
BorderFactory.createEmptyBorder(12, 32, 12, 32)
));
JPanel panel = new JPanel(new GridBagLayout());
panel.setOpaque(false); //배경 지우기
panel.add(lblMsg);
dialog.setContentPane(panel);
dialog.pack();
dialog.setLocationRelativeTo(this);
}
}'Java > Swing' 카테고리의 다른 글
| Swing JDialog (0) | 2026.03.08 |
|---|---|
| Swing 디자인 layout (0) | 2026.03.08 |
| Swing 사용하기 좋은 기능 (0) | 2026.03.08 |
| Swing 먼저 알아두면 좋은 내용 (0) | 2026.03.08 |
| Swing 시작하기 (0) | 2026.03.08 |