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();
            }
        });
    }
}

 

실행 화면

 

반응형
:

c++ getline 예제 코드

프로그래밍/c,c++ 2022. 2. 21. 14:01
반응형

코드

#include <iostream>

using namespace std;
int main() {
    string input = "";

    while (input != "quit") {
        getline(cin, input);
        cout << "read string = " << input << endl;
    }

    return 0;
}

 

실행

$ ./a.out

read string = 
aa
read string = aa
aa bb
read string = aa bb
quit
read string = quit
반응형
:

프로세스 실행 후 종료 확인 코드

프로그래밍/java 2022. 2. 17. 22:18
반응형

java에서 process 실행 후 결과 값을 stream 으로 계속 읽어 들일때 process 가 종료 되면

readline 등의 메소드가 block 이 걸릴수 있다.

이때 process를 체크하는 thread를 따로 생성 후 모니터링 하여 종료를 체크 하여 필요한 동작을 처리 한다.

https://beradrian.wordpress.com/2008/11/03/detecting-process-exit-in-java/

 

Detecting process exit in Java

If you develop a more complex system, the chances of being a heterogeneous are pretty big. So if you develop a system in Java, but you have to integrate all ready build parts with other technology,…

beradrian.wordpress.com

 

    public void run() {
        try {
            // wait for the process to finish
            process.waitFor();
            // invokes the listeners
            for (ProcessListener listener : listeners) {
                listener.processFinished(process);
            }
        } catch (InterruptedException e) {
        }
    }
반응형
: