Swing/TabbedPaneWithCheckBox のバックアップの現在との差分(No.12)
- category: swing folder: TabbedPaneWithCheckBox title: JTabbedPaneの余白にJCheckBoxを配置 tags: [JTabbedPane, JCheckBox, Border] author: aterai pubdate: 2006-04-03T01:35:12+09:00 description: JTabbedPaneの余白にJCheckBoxを配置して特定のタブの開閉を行います。 image:
概要
JTabbedPane
の余白にJCheckBox
を配置して特定のタブの開閉を行います。
概要
JTabbedPane
の余白にJCheckBox
を配置して特定のタブの表示・非表示を切り替えます。
Screenshot
Advertisement
サンプルコード
サンプルコード
class TabbedPaneWithCompBorder implements Border, MouseListener, SwingConstants {
private final JComponent dummy = new JPanel();
private final JCheckBox cbox;
private final JCheckBox checkBox;
private final JTabbedPane tab;
private Rectangle rect;
public TabbedPaneWithCompBorder(JCheckBox cbox, JTabbedPane tab) {
this.cbox = cbox;
this.tab = tab;
tab.addMouseListener(this);
cbox.setFocusPainted(false);
cbox.addMouseListener(new MouseAdapter() {
@Override public void mouseClicked(MouseEvent me) {
JCheckBox cb = (JCheckBox) me.getSource();
cb.setSelected(!cb.isSelected());
}
});
private final Container rubberStamp = new JPanel();
private final Rectangle rect = new Rectangle();
#spanadd
#spanend
protected TabbedPaneWithCompBorder(JCheckBox checkBox, JTabbedPane tab) {
this.checkBox = checkBox;
this.tab = tab;
}
@Override public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
Dimension size = cbox.getPreferredSize();
#spanadd
#spanend
@Override public void paintBorder(
Component c, Graphics g, int x, int y, int width, int height) {
Dimension size = checkBox.getPreferredSize();
int xx = tab.getSize().width - size.width;
Rectangle lastTab = tab.getUI().getTabBounds(tab, tab.getTabCount()-1);
Rectangle lastTab = tab.getBoundsAt(tab.getTabCount() - 1);
int tabEnd = lastTab.x + lastTab.width;
if (xx < tabEnd) {
xx = tabEnd;
}
rect = new Rectangle(xx, -2, size.width, size.height);
SwingUtilities.paintComponent(g, cbox, dummy, rect);
rect.setBounds(xx, -2, size.width, size.height);
SwingUtilities.paintComponent(g, checkBox, rubberStamp, rect);
}
#spanadd
#spanend
@Override public Insets getBorderInsets(Component c) {
return new Insets(0, 0, 0, 0);
}
#spanadd
#spanend
@Override public boolean isBorderOpaque() {
return true;
}
private void dispatchEvent(MouseEvent me) {
if (rect == null || !rect.contains(me.getX(), me.getY())) {
#spanadd
#spanend
private void dispatchEvent(MouseEvent e) {
if (!rect.contains(e.getX(), e.getY())) {
return;
}
cbox.setBounds(rect);
cbox.dispatchEvent(SwingUtilities.convertMouseEvent(tab, me, cbox));
checkBox.setBounds(rect);
checkBox.dispatchEvent(SwingUtilities.convertMouseEvent(tab, e, checkBox));
}
@Override public void mouseClicked(MouseEvent me) { dispatchEvent(me); }
@Override public void mouseEntered(MouseEvent me) { dispatchEvent(me); }
@Override public void mouseExited(MouseEvent me) { dispatchEvent(me); }
@Override public void mousePressed(MouseEvent me) { dispatchEvent(me); }
@Override public void mouseReleased(MouseEvent me) { dispatchEvent(me); }
#spandel
}
#spanend
#spandel
View in GitHub: Java, Kotlin解説
JTabbedPane
のBorder
にSwingUtilities.paintComponent
メソッドを使ってJCheckBox
を描画しています。JCheckBox
がJTabbedPane
の子になってタブが増えないように、ダミーパネルを中間コンテナに指定しています。
@Override public void mouseClicked(MouseEvent e) { dispatchEvent(e); }
JTabbedPane
で受け取ったマウスイベントを、SwingUtilities.convertMouseEvent
メソッドを利用し、チェックボックス用に座標などを変換して送り出しています。
@Override public void mouseEntered(MouseEvent e) { dispatchEvent(e); }
- -
タブとチェックボックスが重ならないように、フレームの最小サイズを設定しています。
@Override public void mouseExited(MouseEvent e) { dispatchEvent(e); }
@Override public void mousePressed(MouseEvent e) { dispatchEvent(e); }
@Override public void mouseReleased(MouseEvent e) { dispatchEvent(e); }} }}
解説
-
JTabbedPane
のBorder
にSwingUtilities.paintComponent(...)
メソッドを使ってJCheckBox
を描画 -
JCheckBox
がJTabbedPane
の子になってタブが増えないように、別途レンダリング用パネルを作成して中間コンテナに指定 -
JTabbedPane
で受け取ったマウスイベントをSwingUtilities.convertMouseEvent(...)
メソッドを利用し、チェックボックス用に座標などを変換してイベント転送 - タブとチェックボックスが重ならないようにフレームの最小サイズを設定
frame.setMinimumSize(new Dimension(240, 80));
他にも、レイアウトマネージャーを利用して同様のことを行う方法があります。
- レイアウトマネージャーを利用して同様のことを行う方法もある
- Swing - Any layout suggestions for this?
- レイアウトマネージャーを自作するweebibさんの投稿 (reply 1)
-
OverlayLayout
を利用するcamickrさんの投稿 (reply 2)
- Swing - Any layout suggestions for this?
- Swing - Any layout suggestions for this?
- レイアウトマネージャーを自作するweebibさんの投稿 (reply 1)
-
OverlayLayout
を利用するcamickrさんの投稿 (reply 2)