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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.tree.*;
import java.io.*;
//import javax.swing.table.*;
public class BasicDnD extends JPanel implements ActionListener, MouseListener {
static JFrame frame;
JTextArea textArea;
JTextField textField;
JList list;
//JTable table;
JTree tree;
JCheckBox toggleDnD;
JLabel label;
public BasicDnD() {
super(new BorderLayout());
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new GridLayout(2,2));
//Create a table model.
//LEFT COLUMN
//Use the table model to create a table.
//Create a color chooser.
//RIGHT COLUMN
//Create a textfield.
textField = new JTextField(30);
textField.addActionListener(this);
textField.addMouseListener(this);
textField.setText("Favorite foods:\nPizza, Moussaka, Pot roast");
rightPanel.add(createPanelForComponent(textField, "JTextField"));
//Create a scrolled text area.
textArea = new JTextArea(5, 30);
textArea.setText("Favorite shows:\nBuffy, Alias, Angel");
//textArea.addActionListener(this);
textArea.addMouseListener(this);
JScrollPane scrollPane = new JScrollPane(textArea);
rightPanel.add(createPanelForComponent(scrollPane, "JTextArea"));
//Creat a label
//Create a list model and a list.
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("Martha Washington");
listModel.addElement("Abigail Adams");
listModel.addElement("Martha Randolph");
listModel.addElement("Dolley Madison");
listModel.addElement("Elizabeth Monroe");
listModel.addElement("Louisa Adams");
listModel.addElement("Emily Donelson");
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
JScrollPane listView = new JScrollPane(list);
listView.setPreferredSize(new Dimension(300, 100));
rightPanel.add(createPanelForComponent(listView, "JList"));
//Create a tree.
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Mia Familia");
DefaultMutableTreeNode sharon = new DefaultMutableTreeNode("Sharon");
rootNode.add(sharon);
DefaultMutableTreeNode maya = new DefaultMutableTreeNode("Maya");
sharon.add(maya);
DefaultMutableTreeNode anya = new DefaultMutableTreeNode("Anya");
sharon.add(anya);
sharon.add(new DefaultMutableTreeNode("Bongo"));
maya.add(new DefaultMutableTreeNode("Muffin"));
anya.add(new DefaultMutableTreeNode("Winky"));
DefaultTreeModel model = new DefaultTreeModel(rootNode);
tree = new JTree(model);
//tree.addActionListener(this);
tree.addMouseListener(this);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
JScrollPane treeView = new JScrollPane(tree);
treeView.setPreferredSize(new Dimension(300, 100));
rightPanel.add(createPanelForComponent(treeView, "JTree"));
//Create the toggle button.
toggleDnD = new JCheckBox("Turn on Drag and Drop");
toggleDnD.setActionCommand("toggleDnD");
toggleDnD.addActionListener(this);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
null, rightPanel);
//splitPane.setOneTouchExpandable(true);
add(splitPane, BorderLayout.CENTER);
add(toggleDnD, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
protected JPanel createVerticalBoxPanel() {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
return p;
}
public JPanel createPanelForComponent(JComponent comp,
String title) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(comp, BorderLayout.CENTER);
if (title != null) {
panel.setBorder(BorderFactory.createTitledBorder(title));
}
return panel;
}
public void actionPerformed(ActionEvent e) {
System.out.println("chiamata a actionPerformed");
if ("toggleDnD".equals(e.getActionCommand())) {
boolean toggle = toggleDnD.isSelected();
//boolean toggle = true;
textArea.setDragEnabled(toggle);
//label.setDragEnabled(toggle);
textField.setDragEnabled(toggle);
list.setDragEnabled(toggle);
//table.setDragEnabled(toggle);
tree.setDragEnabled(toggle);
}
}
public void mouseEntered(MouseEvent e) {
System.out.println("chiamata a mouseEntered");
if(e.getSource() instanceof JTree) {
System.out.println("Entro in componentei Jtree");
}
/*else if(e.getSource() instanceof JCheckBox) {
System.out.println("Entro in componentei JCheckBox");
}*/
else if(e.getSource() instanceof JTextArea) {
System.out.println("Entro in componentei JTextArea");
}
}
public void mouseExited(MouseEvent e) {
System.out.println("chiamata a mouseExite");
if(e.getSource() instanceof JTree) {
System.out.println("Esco da un componente Jtree");
}
/*else if(e.getSource() instanceof JCheckBox) {
System.out.println("Esco da un componente JCheckBox");
}*/
else if(e.getSource() instanceof JTextArea) {
System.out.println("Esco da un componente JTextArea");
}
}
public void mousePressed(MouseEvent e) {
System.out.println("chiamata a mousePressed");
if(e.getSource() instanceof JTree) {
System.out.println("Presso un componente Jtree");
}
/*else if(e.getSource() instanceof JCheckBox) {
System.out.println("Presso un componente JCheckBox");
}*/
else if(e.getSource() instanceof JTextArea) {
System.out.println("Presso un componente JTextArea");
}
}
public void mouseReleased(MouseEvent e) {
System.out.println("chiamata a mouseReleased");
if(e.getSource() instanceof JTree) {
System.out.println("Relese un componente Jtree");
}
/*else if(e.getSource() instanceof JCheckBox) {
System.out.println("Relese un componente JCheckBox");
}*/
else if(e.getSource() instanceof JTextArea) {
System.out.println("Relese un componente JTextArea");
}
}
public void mouseClicked(MouseEvent e) {
System.out.println("chiamata a mouseClicked");
if(e.getSource() instanceof JTree) {
System.out.println("Click un componente Jtree");
}
else if("JTextArea".equals(e.getSource())) {
System.out.println("Click un componente JTextArea");
}
}
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("########## Prova Drop & Drag ##########");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new BasicDnD();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
//frame.setSize(700,700);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |
Partager