1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Fenetre extends JFrame{
private JFrame f = new JFrame("This is a test");
private Container content = f.getContentPane();
private JTextArea textfield = new JTextArea();
private JList list;
public Fenetre(){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 600);
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.setSize(new Dimension(550,550));
textfield.setPreferredSize(new Dimension(550,550));
textfield.setBackground(new Color(240,200,240));
// début de création de la liste static pour le moment
String name[] = {"Deepak", "Chandan Kumar Verma", "Sushil", "Raju",
"Santosh", "Amar", "Vinod"};
DefaultListModel model = new DefaultListModel();
for(int i = 0; i < name.length; i++)
model.addElement(name[i]);
list = new JList(model){
public String getToolTipText(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (-1 < index) {
String item = (String)getModel().getElementAt(index);
return item;
} else {
return null;
}
}
};
list.setPrototypeCellValue("RoseIndia.net");
int x = 120;
int y = 50;
list.setLocation(x,y);
content.add(list);
// end of it
textfield.addKeyListener(new BoutonListener());
content.add(textfield);
f.setVisible(true);
}
class BoutonListener implements KeyListener{
public void keyPressed(KeyEvent keyEvent) {
printIt("Pressed", keyEvent);
}
public void keyReleased(KeyEvent keyEvent) {
//printIt("Released", keyEvent);
}
public void keyTyped(KeyEvent keyEvent) {
//printIt("Typed", keyEvent);
}
private void printIt(String title, KeyEvent keyEvent) {
System.out.println(textfield.getText());
System.out.println("Position : "+textfield.getCaret().getMagicCaretPosition().getX()+" - "+textfield.getCaret().getMagicCaretPosition().getY());
int x = (int)textfield.getCaret().getMagicCaretPosition().getX()+60;
int y = (int)textfield.getCaret().getMagicCaretPosition().getY()+(int)list.getSize().getHeight()+20;
System.out.println(x+" : "+y);
list.setLocation(x, y);
f.repaint();
}
}
} |
Partager