JTextPaneでコードアシストもどき
2008/09/10
java
swing
コードアシスト
前回、JTextFieldでコードアシストもどきができたので、今回は、JTextPaneでやってみた。
前回のコードのTextFieldの部分を書き換えたらそれでてokだった.....
コードは以下
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
public class TestCodeAssistModoki {
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextPane textPane = new JTextPane();
textPane.getKeymap().addActionForKeyStroke(
KeyStroke.getKeyStroke("ctrl SPACE"), new Action() {
public void addPropertyChangeListener(
PropertyChangeListener listener) {
}
public Object getValue(String key) {
return null;
}
public boolean isEnabled() {
// !!!!!!!!
return true;
}
public void putValue(String key, Object value) {
}
public void removePropertyChangeListener(
PropertyChangeListener listener) {
}
public void setEnabled(boolean b) {
}
/**
* イベントが発生したコンポーネントでJWindowを表示してみる
*/
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource());
final JTextPane textPane = (JTextPane) e.getSource();
final int p = textPane.getCaretPosition();
Rectangle rectangle;
try {
rectangle = textPane.modelToView(p);
JPopupMenu popupMenu = new JPopupMenu();
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
textPane.getDocument().insertString(p,
e.getActionCommand(), null);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
};
System.out.println(rectangle);
popupMenu.add("テストです").addActionListener(
actionListener);
popupMenu.add("0_0!").addActionListener(
actionListener);
popupMenu.add("^_^!").addActionListener(
actionListener);
popupMenu.add("o_o!").addActionListener(
actionListener);
popupMenu.show(textPane, rectangle.x, rectangle.y
+ rectangle.height);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
});
frame.getContentPane().add(textPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
public class TestCodeAssistModoki {
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextPane textPane = new JTextPane();
textPane.getKeymap().addActionForKeyStroke(
KeyStroke.getKeyStroke("ctrl SPACE"), new Action() {
public void addPropertyChangeListener(
PropertyChangeListener listener) {
}
public Object getValue(String key) {
return null;
}
public boolean isEnabled() {
// !!!!!!!!
return true;
}
public void putValue(String key, Object value) {
}
public void removePropertyChangeListener(
PropertyChangeListener listener) {
}
public void setEnabled(boolean b) {
}
/**
* イベントが発生したコンポーネントでJWindowを表示してみる
*/
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource());
final JTextPane textPane = (JTextPane) e.getSource();
final int p = textPane.getCaretPosition();
Rectangle rectangle;
try {
rectangle = textPane.modelToView(p);
JPopupMenu popupMenu = new JPopupMenu();
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
textPane.getDocument().insertString(p,
e.getActionCommand(), null);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
};
System.out.println(rectangle);
popupMenu.add("テストです").addActionListener(
actionListener);
popupMenu.add("0_0!").addActionListener(
actionListener);
popupMenu.add("^_^!").addActionListener(
actionListener);
popupMenu.add("o_o!").addActionListener(
actionListener);
popupMenu.show(textPane, rectangle.x, rectangle.y
+ rectangle.height);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
});
frame.getContentPane().add(textPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
}
: