/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gui.basics;
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
/**
*
* @author Конарх
*/
public class EventsExFrame extends javax.swing.JFrame {
/**
* Creates new form EventsExFrame
*/
public EventsExFrame() {
initComponents();
setLocationRelativeTo(null);
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener(){
@Override
public void eventDispatched(final AWTEvent event) {
//System.out.println(event);
final MouseEvent m = (MouseEvent)event;
if (m.getSource()==addBut&&m.getID()==MouseEvent.MOUSE_CLICKED){
final String text = inputField.getText().trim();
if (!text.isEmpty()){
outArea.append(text+"\n");
}
}
}
}, MouseEvent.MOUSE_EVENT_MASK);
addBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
final String text = inputField.getText().trim();
if (!text.isEmpty()) {
outArea.append(text + "\n");
}
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jScrollPane1 = new javax.swing.JScrollPane();
outArea = new javax.swing.JTextArea();
addBut = new javax.swing.JButton();
inputField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Events example form");
setMinimumSize(new java.awt.Dimension(200, 200));
getContentPane().setLayout(new java.awt.GridBagLayout());
jScrollPane1.setPreferredSize(new java.awt.Dimension(300, 300));
outArea.setEditable(false);
outArea.setColumns(20);
outArea.setRows(5);
jScrollPane1.setViewportView(outArea);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane1, gridBagConstraints);
addBut.setText("Add");
addBut.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addText(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
getContentPane().add(addBut, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
getContentPane().add(inputField, gridBagConstraints);
pack();
}// </editor-fold>//GEN-END:initComponents
private void addText(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addText
// TODO add your handling code here:
}//GEN-LAST:event_addText
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EventsExFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EventsExFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EventsExFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EventsExFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EventsExFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addBut;
private javax.swing.JTextField inputField;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea outArea;
// End of variables declaration//GEN-END:variables
}