Swing/ButtonInComboEditor のバックアップの現在との差分(No.6)
TITLE:JComboBoxのEditorComponentにJButtonを配置
Posted by terai at 2009-08-17
JComboBoxのEditorComponentにJButtonを配置
JComboBoxのEditorComponentにJButtonやJLabelなどを配置します。-
category: swing
folder: ButtonInComboEditor
title: JComboBoxのEditorComponentにJButtonを配置
tags: [JComboBox, LayoutManager, JButton, JLabel, Icon, RGBImageFilter, RescaleOp]
author: aterai
pubdate: 2009-08-17T12:55:55+09:00
description: JComboBoxのEditorComponentにJButtonやJLabelなどを配置します。
image:
hreflang:
href: https://java-swing-tips.blogspot.com/2009/08/jbutton-in-comboeditor.html lang: en
概要
JComboBox
のEditorComponent
にJButton
やJLabel
などを配置します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
#spanadd
class SiteComboBoxLayout implements LayoutManager {
#spanend
private final JLabel favicon;
private final JButton feedButton;
#spandel
**サンプルコード [#o63272a1]
#spanend
#spandel
#code{{
#spanend
#spandel
final JButton button = new JButton(rss);
#spanend
#spandel
final JLabel label = new JLabel(image1);
#spanend
#spandel
final JTextField field = (JTextField) combo.getEditor().getEditorComponent();
#spanend
#spandel
field.add(button);
#spanend
#spandel
field.add(label);
#spanend
#spandel
field.setBorder(BorderFactory.createEmptyBorder(0,16+4,0,14+2));
#spanend
#spandel
field.addComponentListener(new ComponentAdapter() {
#spanend
@Override public void componentResized(ComponentEvent e) {
Rectangle r = field.getBounds();
label.setBounds(1, 0, 16, r.height);
button.setBounds(r.width-14, 0, 14, r.height);
protected SiteComboBoxLayout(JLabel favicon, JButton feedButton) {
this.favicon = favicon;
this.feedButton = feedButton;
}
#spandel
});
#spanend
#spanadd
#spanend
@Override public void addLayoutComponent(String name, Component comp) {
/* not needed */
}
#spanadd
#spanend
@Override public void removeLayoutComponent(Component comp) {
/* not needed */
}
#spanadd
#spanend
@Override public Dimension preferredLayoutSize(Container parent) {
return parent.getPreferredSize();
}
#spanadd
#spanend
@Override public Dimension minimumLayoutSize(Container parent) {
return parent.getMinimumSize();
}
#spanadd
#spanend
@Override public void layoutContainer(Container parent) {
if (!(parent instanceof JComboBox)) {
return;
}
JComboBox<?> cb = (JComboBox<?>) parent;
int width = cb.getWidth();
int height = cb.getHeight();
Insets insets = cb.getInsets();
int arrowHeight = height - insets.top - insets.bottom;
int arrowWidth = arrowHeight;
int faviconWidth = arrowHeight;
int feedWidth; // = arrowHeight;
#spanadd
#spanend
// Arrow Icon JButton
JButton arrowButton = (JButton) cb.getComponent(0);
if (Objects.nonNull(arrowButton)) {
Insets arrowInsets = arrowButton.getInsets();
arrowWidth = arrowButton.getPreferredSize().width
+ arrowInsets.left + arrowInsets.right;
arrowButton.setBounds(width - insets.right - arrowWidth, insets.top,
arrowWidth, arrowHeight);
}
#spanadd
#spanend
// Favicon JLabel
if (Objects.nonNull(favicon)) {
Insets faviconInsets = favicon.getInsets();
faviconWidth = favicon.getPreferredSize().width
+ faviconInsets.left + faviconInsets.right;
favicon.setBounds(insets.left, insets.top, faviconWidth, arrowHeight);
}
#spanadd
#spanend
// Feed Icon JButton
if (Objects.nonNull(feedButton) && feedButton.isVisible()) {
Insets feedInsets = feedButton.getInsets();
feedWidth = feedButton.getPreferredSize().width
+ feedInsets.left + feedInsets.right;
feedButton.setBounds(width - insets.right - feedWidth - arrowWidth, insets.top,
feedWidth, arrowHeight);
} else {
feedWidth = 0;
}
#spanadd
#spanend
// JComboBox Editor
Component editor = cb.getEditor().getEditorComponent();
if (Objects.nonNull(editor)) {
editor.setBounds(insets.left + faviconWidth, insets.top,
width - insets.left - insets.right - arrowWidth - faviconWidth - feedWidth,
height - insets.top - insets.bottom);
}
}
#spanadd
}
#spanend
View in GitHub: Java, Kotlin解説
上記のサンプルでは、setBorderメソッドで作成した余白の上に、JButtonやJLabelを配置しています。解説
上記のサンプルでは、JComboBox
に独自のレイアウトマネージャーを設定して、そのエディタ内部にデフォルトのArrowButton
とは異なるJButton
やJLabel
を追加で配置しています。
-
JButton
にFeed
アイコンを設定してArrowButton
の左側に追加配置-
EditorComponent
にフォーカスがある場合このFeedButton
は非表示
-
-
JLabel
にFavicon
を設定して左端に追加配置
RolloverIconは、元のアイコンに以下のようなフィルタを掛けて作成しています。
-
RolloverIcon
は元のアイコンに以下のようなフィルタを掛けて作成している
#spanadd
// RGBImageFilter を使用
#spanend
private static ImageIcon makeFilteredImage(ImageIcon srcIcon) {
RGBImageFilter filter = new SelectedImageFilter();
FilteredImageSource fis = new FilteredImageSource(srcIcon.getImage().getSource(), filter);
FilteredImageSource fis = new FilteredImageSource(
srcIcon.getImage().getSource(), filter);
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(fis));
}
#spandel
static class SelectedImageFilter extends RGBImageFilter {
#spanend
public SelectedImageFilter() {
canFilterIndexColorModel = true;
#spanadd
#spanend
#spanadd
class SelectedImageFilter extends RGBImageFilter {
#spanend
private static final float SCALE = 1.2f;
@Override public int filterRGB(int x, int y, int argb) {
// int a = (argb >> 24) & 0xFF;
int r = (int) Math.min(0xFF, ((argb >> 16) & 0xFF) * SCALE);
int g = (int) Math.min(0xFF, ((argb >> 8) & 0xFF) * SCALE);
int b = (int) Math.min(0xFF, ((argb) & 0xFF) * SCALE);
return (argb & 0xFF_00_00_00) | (r << 16) | (g << 8) | (b);
}
private final float scale = 1.2f;
public int filterRGB(int x, int y, int argb) {
//int a = (argb >> 24) & 0xff;
int r = (argb >> 16) & 0xff;
int g = (argb >> 8) & 0xff;
int b = (argb) & 0xff;
r = (int)(r*scale);
g = (int)(g*scale);
b = (int)(b*scale);
if(r > 255) r = 255;
if(g > 255) g = 255;
if(b > 255) b = 255;
return (argb & 0xff000000) | (r<<16) | (g<<8) | (b);
}
}
#spandel
// private static ImageIcon makeFilteredImage2(ImageIcon srcIcon) {
#spanend
#spandel
// RescaleOp op = new RescaleOp(
#spanend
#spandel
// new float[] { 1.2f,1.2f,1.2f,1.0f },
#spanend
#spandel
// new float[] { 0f,0f,0f,0f }, null);
#spanend
#spandel
// BufferedImage img = new BufferedImage(
#spanend
#spandel
// srcIcon.getIconWidth(), srcIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
#spanend
#spandel
// Graphics g = img.getGraphics();
#spanend
#spandel
// //g.drawImage(srcIcon.getImage(), 0, 0, null);
#spanend
#spandel
// srcIcon.paintIcon(null, g, 0, 0);
#spanend
#spandel
// g.dispose();
#spanend
#spandel
// return new ImageIcon(op.filter(img, null));
#spanend
#spandel
// }
#spanend
#spanadd
#spanend
#spanadd
// RescaleOp を使用
#spanend
#spanadd
private static ImageIcon makeFilteredImage2(ImageIcon srcIcon) {
#spanend
RescaleOp op = new RescaleOp(
new float[] { 1.2f, 1.2f, 1.2f, 1f },
new float[] { 0f, 0f, 0f, 0f }, null);
BufferedImage img = new BufferedImage(
srcIcon.getIconWidth(), srcIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
//g.drawImage(srcIcon.getImage(), 0, 0, null);
srcIcon.paintIcon(null, g, 0, 0);
g.dispose();
return new ImageIcon(op.filter(img, null));
#spanadd
}
#spanend
参考リンク
参考リンク
コメント
- EditorComponentに追加したJButtonをクリックすると例外が発生する場合があるバグを修正 -- terai
コメント
-
EditorComponent
に追加したJButton
をクリックすると例外が発生する場合があるバグを修正 -- aterai