Example usage for javax.swing JButton addActionListener

List of usage examples for javax.swing JButton addActionListener

Introduction

In this page you can find the example usage for javax.swing JButton addActionListener.

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

From source file:DocumentEventDemo.java

public DocumentEventDemo() {
    super(new GridBagLayout());
    GridBagLayout gridbag = (GridBagLayout) getLayout();
    GridBagConstraints c = new GridBagConstraints();

    JButton button = new JButton("Clear");
    button.addActionListener(this);

    textField = new JTextField(20);
    textField.addActionListener(new MyTextActionListener());
    textField.getDocument().addDocumentListener(new MyDocumentListener());
    textField.getDocument().putProperty("name", "Text Field");

    textArea = new JTextArea();
    textArea.getDocument().addDocumentListener(new MyDocumentListener());
    textArea.getDocument().putProperty("name", "Text Area");

    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setPreferredSize(new Dimension(200, 75));

    displayArea = new JTextArea();
    displayArea.setEditable(false);/*w w w  .  j av  a2  s .  com*/
    JScrollPane displayScrollPane = new JScrollPane(displayArea);
    displayScrollPane.setPreferredSize(new Dimension(200, 75));

    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;
    gridbag.setConstraints(textField, c);
    add(textField);

    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 0.0;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH;
    gridbag.setConstraints(scrollPane, c);
    add(scrollPane);

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 1.0;
    c.weighty = 1.0;
    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);

    c.gridx = 1;
    c.gridy = 2;
    c.weightx = 0.0;
    c.gridheight = 1;
    c.weighty = 0.0;
    c.fill = GridBagConstraints.HORIZONTAL;
    gridbag.setConstraints(button, c);
    add(button);

    setPreferredSize(new Dimension(450, 250));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:jfreeechart.DynamicDataDemo.java

/**
 * Constructs a new demonstration application.
 *
 * @param title  the frame title.//from ww  w.  jav  a  2 s  . c  om
 */
public DynamicDataDemo(final String title) {

    super(title);
    this.series = new TimeSeries("Random Data", Millisecond.class);
    final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
    final JFreeChart chart = createChart(dataset);

    final ChartPanel chartPanel = new ChartPanel(chart);
    final JButton button = new JButton("Add New Data Item");
    button.setActionCommand("ADD_DATA");
    button.addActionListener(this);

    final JPanel content = new JPanel(new BorderLayout());
    content.add(chartPanel);
    content.add(button, BorderLayout.SOUTH);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(content);

}

From source file:Main.java

public void buildGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame f = new JFrame();
    f.setResizable(false);//w w w. java 2  s. c om
    removeMinMaxClose(f);
    JPanel p = new JPanel(new GridBagLayout());
    JButton btn = new JButton("Exit");
    p.add(btn, new GridBagConstraints());
    f.getContentPane().add(p);
    f.setSize(400, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    btn.addActionListener(e -> System.exit(0));
}

From source file:ListModelExample.java

public ListModelExample() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    for (int i = 0; i < 15; i++)
        model.addElement("Element " + i);

    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.addElement("Element " + counter);
            counter++;/*  w w w .  j  ava  2  s  .  co m*/
        }
    });
    removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (model.getSize() > 0)
                model.removeElementAt(0);
        }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    for (int i = 0; i < 15; i++)
        model.addElement("Element " + i);

    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.addElement("Element " + counter);
            counter++;// ww  w.j a va 2 s  . com
        }
    });
    removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (model.getSize() > 0)
                model.removeElementAt(0);
        }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    populateTree(treePanel);/*from  w w w.  ja v a 2s .  com*/
    JButton addButton = new JButton("Add");
    addButton.setActionCommand(ADD_COMMAND);
    addButton.addActionListener(this);
    JButton removeButton = new JButton("Remove");
    removeButton.setActionCommand(REMOVE_COMMAND);
    removeButton.addActionListener(this);
    JButton clearButton = new JButton("Clear");
    clearButton.setActionCommand(CLEAR_COMMAND);
    clearButton.addActionListener(this);
    treePanel.setPreferredSize(new Dimension(300, 150));
    add(treePanel, BorderLayout.CENTER);
    JPanel panel = new JPanel(new GridLayout(0, 3));
    panel.add(addButton);
    panel.add(removeButton);
    panel.add(clearButton);
    add(panel, BorderLayout.SOUTH);
}

From source file:net.chaosserver.timelord.swingui.JCalendarDialog.java

/**
 * Creates the dialog./* w w  w. j a v  a 2 s  . c  o m*/
 *
 * @param owner the owner frame
 * @param selectedDate the date to have the calendar on, or null for
 *        current date.
 */
public JCalendarDialog(Frame owner, Date selectedDate) {
    super(owner, "Pick Date", true);
    calendar = new JCalendar();

    if (selectedDate != null) {
        calendar.setDate(selectedDate);
    }

    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(calendar, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel();
    JButton selectButton = new JButton("Select");
    selectButton.setActionCommand(actionSelect);
    selectButton.addActionListener(this);
    buttonPanel.add(selectButton);

    JButton cancelButton = new JButton("Cancel");
    cancelButton.setActionCommand(actionCancel);
    cancelButton.addActionListener(this);
    buttonPanel.add(cancelButton);

    this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}

From source file:es.emergya.ui.plugins.JButtonCellEditor.java

@Override
public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected,
        final int row, final int column) {
    JButton b = (JButton) value;
    b.setBorderPainted(false);/*from w  ww  .  j a  v  a  2 s.  co m*/
    b.setContentAreaFilled(false);
    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
                @Override
                protected Object doInBackground() throws Exception {
                    stopCellEditing();
                    return null;
                }
            };
            SwingUtilities.invokeLater(sw);

            SwingWorker<Object, Object> sw1 = new SwingWorker<Object, Object>() {
                @Override
                protected Object doInBackground() throws Exception {
                    table.repaint();
                    return null;
                }
            };
            SwingUtilities.invokeLater(sw1);
        }
    });
    return b;
}

From source file:medsavant.uhn.cancer.SetCommentStatusDialog.java

private JPanel getMainPanel() {
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    //Status box.
    mainPanel.add(getStatusEditorPanel());

    //Comment box.
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(Box.createHorizontalGlue());
    comment = new JTextArea();
    p.add(comment);/*from   ww  w.j ava  2  s  .co m*/
    p.add(Box.createHorizontalGlue());
    mainPanel.add(p);

    //Button box.        
    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(Box.createHorizontalGlue());
    JButton OKButton = new JButton("Update status");
    OKButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                updateStatus();
                dispose();
            } catch (Exception ex) {
                LOG.error(ex.getMessage(), ex);
                DialogUtils.displayException("Error", ex.getMessage(), ex);
            }
        }
    });

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    p.add(OKButton);
    p.add(cancelButton);
    p.add(Box.createHorizontalGlue());
    mainPanel.add(p);

    return mainPanel;
}

From source file:com.google.code.facebook.graph.sna.applet.GraphFromGraphMLDemo.java

/**
 * create an instance of a simple graph with controls to
 * demo the zoom features.// w ww  . ja  v  a2  s .com
 * @throws SAXException 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * 
 */
public GraphFromGraphMLDemo(String filename) throws ParserConfigurationException, SAXException, IOException {

    Factory<Number> vertexFactory = new Factory<Number>() {
        int n = 0;

        public Number create() {
            return n++;
        }
    };
    Factory<Number> edgeFactory = new Factory<Number>() {
        int n = 0;

        public Number create() {
            return n++;
        }
    };

    GraphMLReader<DirectedGraph<Number, Number>, Number, Number> gmlr = new GraphMLReader<DirectedGraph<Number, Number>, Number, Number>(
            vertexFactory, edgeFactory);
    final DirectedGraph<Number, Number> graph = new DirectedSparseMultigraph<Number, Number>();
    gmlr.load(filename, graph);

    // create a simple graph for the demo
    vv = new VisualizationViewer<Number, Number>(new FRLayout<Number, Number>(graph));

    vv.addGraphMouseListener(new TestGraphMouseListener<Number>());
    vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<Number, Number>(Color.white, Color.red,
            Color.white, Color.blue, vv.getPickedVertexState(), false));

    // add my listeners for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller<Number>());
    vv.setEdgeToolTipTransformer(new Transformer<Number, String>() {
        public String transform(Number edge) {
            return "E" + graph.getEndpoints(edge).toString();
        }
    });

    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Number>());
    vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse<Number, Number>();
    vv.setGraphMouse(graphMouse);
    vv.addKeyListener(graphMouse.getModeKeyListener());

    JMenuBar menubar = new JMenuBar();
    menubar.add(graphMouse.getModeMenu());
    panel.setCorner(menubar);

    vv.addKeyListener(graphMouse.getModeKeyListener());
    vv.setToolTipText("<html><center>Type 'p' for Pick mode<p>Type 't' for Transform mode");

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    content.add(controls, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
}