Multiline combobox + word highlight
프로그래밍/java 2022. 3. 24. 22:09반응형
Mulitline combobox - dynamic, ComboBoxEditor
word highlight - Highlighter
package test.swing;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import java.awt.*;
import java.awt.event.*;
class TextAreaComboBoxEditor implements ComboBoxEditor {
private MyTextArea textArea = new MyTextArea();
private JComboBox<String> mCombo;
public TextAreaComboBoxEditor(JComboBox<String> combo) {
mCombo = combo;
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("A " + mCombo.getPreferredSize().height + ", " + textArea.getPreferredSize().height);
mCombo.setPreferredSize(new Dimension(mCombo.getPreferredSize().width, textArea.getPreferredSize().height));
textArea.setUpdateHighlighter(true);
mCombo.getParent().revalidate();
mCombo.getParent().repaint();
}
});
textArea.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
textArea.setUpdateHighlighter(true);
}
});
}
@Override
public Component getEditorComponent() {
return textArea;
}
@Override
public void setItem(Object anObject) {
if (anObject instanceof String) {
textArea.setText((String) anObject);
} else {
textArea.setText(null);
}
}
@Override
public Object getItem() {
return textArea.getText();
}
@Override
public void selectAll() {
textArea.selectAll();
}
@Override
public void addActionListener(ActionListener l) {
}
@Override
public void removeActionListener(ActionListener l) {
}
class MyTextArea extends JTextArea {
public void setUpdateHighlighter(boolean mUpdateHighlighter) {
this.mUpdateHighlighter = mUpdateHighlighter;
}
private boolean mUpdateHighlighter = false;
@Override
public void paint(Graphics g) {
if (mUpdateHighlighter) {
Highlighter highlighter = getHighlighter();
Highlighter.HighlightPainter painter1 = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);
Highlighter.HighlightPainter painter2 = new DefaultHighlighter.DefaultHighlightPainter(Color.LIGHT_GRAY);
String text = getText();
String separator = " ";
try {
highlighter.removeAllHighlights();
int currPos = 0;
while (currPos < text.length()) {
int startPos = currPos;
int separatorPos = text.indexOf(separator, currPos);
int endPos = separatorPos;
if (separatorPos < 0) {
endPos = text.length();
}
if (startPos >= 0 && endPos > startPos) {
highlighter.addHighlight(startPos, endPos, painter1);
}
if (separatorPos >= 0) {
highlighter.addHighlight(separatorPos, separatorPos + 1, painter2);
}
currPos = endPos + 1;
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
mUpdateHighlighter = false;
}
super.paint(g);
}
}
}
public class MainTest extends JFrame
{
public MainTest()
{
setPreferredSize(new Dimension(400, 120));
JComboBox<String> combo = new JComboBox<>();
combo.setPreferredSize(new Dimension(100, 18));
combo.setEditor(new TextAreaComboBoxEditor(combo));
combo.setEditable(true);
JPanel panel = new JPanel();
panel.add(combo);
this.add(panel);
this.setTitle("combo Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainTest();
}
});
}
}
실행 화면
반응형