JComboBox 키 입력시 툴팁 보이기
프로그래밍/java 2021. 12. 23. 22:00반응형
JComboBox 에 key listener 설정 하여 키 발생시 툴팁을 보여주는 코드
package test.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
// key event tooltip
public class MainTest {
public static void main(String[] args) {
MainUI mainUI = new MainUI();
mainUI.setVisible(true);
}
}
class TestComboBox extends JComboBox {
JTextField mTf = null;
public TestComboBox() {
super();
mTf = (JTextField) editor.getEditorComponent();
mTf.setToolTipText("TextField : ");
mTf.addKeyListener(new KeyHandler());
}
class KeyHandler extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
// set tooltip
mTf.setToolTipText("TextField : " + mTf.getText());
// show tooltip
ToolTipManager.sharedInstance().mouseMoved(new MouseEvent(mTf, 0, 0, 0,0, 0, 0, false));
super.keyReleased(e);
}
}
}
class MainUI extends JFrame {
MainUI() {
setPreferredSize(new Dimension(400, 300));
setLayout(new FlowLayout());
TestComboBox combo = new TestComboBox();
combo.setToolTipText("ComboBox");
combo.setEditable(true);
add(combo);
pack();
}
}
반응형