Simple swing sample to answer this stackoverflow question
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.HashSet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
public class TimetableFrame extends javax.swing.JFrame {
Point p1, p2;
Rectangle2D rectangle;
int mode = 0;
HashSet<JLabel> all = new HashSet<JLabel>();
HashSet<JLabel> selected = new HashSet<JLabel>();
HashSet<JLabel> current_selection = new HashSet<JLabel>();
Color empty_color = Color.WHITE;
Color fill_color = Color.RED;
Color empty_select = Color.GRAY;
Color fill_select = Color.PINK;
JPanel grid;
JPanel jPanel1;
JScrollPane jScrollPane1;
public TimetableFrame() {
initComponents();
setTitle("Swing grid selector example halimoglu.com");
String[] col_titles = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
String[] row_titles = new String[24];
for (int i = 0; i < row_titles.length; i++) {
row_titles[i] = (i) + "-" + (i + 1);
}
for (int y = 0; y < row_titles.length + 1; y++) {
for (int x = 0; x < col_titles.length + 1; x++) {
if (y == 0 && x == 0) {
grid.add(new JLabel(""));
} else if (y == 0) {
JLabel col = new JLabel(col_titles[x - 1]);
col.setHorizontalAlignment(SwingConstants.CENTER);
grid.add(col);
} else if (x == 0) {
grid.add(new JLabel(row_titles[y - 1]));
} else {
JLabel mylabel = new JLabel(" ");
mylabel.setOpaque(true);
mylabel.setBackground(empty_color);
all.add(mylabel);
grid.add(mylabel);
}
}
}
grid.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent me) {
for (JLabel j : all) {
if (j.getBounds().contains(me.getPoint())) {
if (selected.contains(j)) {
selected.remove(j);
j.setBackground(empty_color);
} else {
selected.add(j);
j.setBackground(fill_color);
}
}
}
}
@Override
public void mousePressed(MouseEvent me) {
p1 = me.getPoint();
rectangle = new Rectangle2D.Double(p1.x, p1.y, p1.x - p1.x, p1.y - p1.y);
}
@Override
public void mouseReleased(MouseEvent me) {
if (mode == 1) {
selected.addAll(current_selection);
} else if (mode == -1) {
selected.removeAll(current_selection);
}
for (JLabel j : all) {
if (selected.contains(j)) {
j.setBackground(fill_color);
} else {
j.setBackground(empty_color);
}
}
current_selection.clear();
mode = 0;
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
});
grid.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent me) {
p2 = me.getPoint();
Point lu;
int minx = Math.min(p1.x, p2.x);
int miny = Math.min(p1.y, p2.y);
int maxx = Math.max(p1.x, p2.x);
int maxy = Math.max(p1.y, p2.y);
lu = new Point(minx, miny);// left upper point
rectangle.setRect(lu.x, lu.y, maxx - minx, maxy - miny);
//set mode
//1 = if first selected cell is empty set mode to fill
//-1 = otherwise clear
if (mode == 0) {
for (JLabel j : all) {
if (j.getBounds().intersects(rectangle)) {
if (selected.contains(j)) {
mode = -1;
} else {
mode = 1;
}
break;
}
}
}
for (JLabel j : all) {
if (selected.contains(j) == (mode != 1)) {
if (j.getBounds().intersects(rectangle)) {
current_selection.add(j);
} else {
if (current_selection.contains(j)) {
current_selection.remove(j);
if (mode == 1) {
if (selected.contains(j) == false) {
j.setBackground(empty_color);
}
} else {
if (selected.contains(j) == true) {
j.setBackground(fill_color);
}
}
}
}
}
}
for (JLabel j : current_selection) {
if (mode == 1) {
j.setBackground(fill_select);
} else if (mode == -1) {
j.setBackground(empty_select);
}
}
}
@Override
public void mouseMoved(MouseEvent me) {
}
});
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
grid = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.BorderLayout());
grid.setLayout(new java.awt.GridLayout(25, 0, 2, 2));
jPanel1.add(grid, java.awt.BorderLayout.CENTER);
jScrollPane1.setViewportView(jPanel1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
new TimetableFrame().setVisible(true);
}
}
Be First to Comment