List of usage examples for javax.swing JButton addActionListener
public void addActionListener(ActionListener l)
ActionListener
to the button. From source file:MessagePopup.java
public static JButton getButton(final JOptionPane optionPane, String text, Icon icon) { final JButton button = new JButton(text, icon); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { // Return current text label, instead of argument to method optionPane.setValue(button.getText()); }//from w ww . ja v a2s . c o m }; button.addActionListener(actionListener); return button; }
From source file:eu.delving.sip.Application.java
private static void memoryNotConfigured() { String os = System.getProperty("os.name"); Runtime rt = Runtime.getRuntime(); int totalMemory = (int) (rt.totalMemory() / 1024 / 1024); StringBuilder out = new StringBuilder(); String JAR_NAME = "SIP-Creator-2014-XX-XX.jar"; if (os.startsWith("Windows")) { out.append(":: SIP-Creator Startup Batch file for Windows (more memory than ").append(totalMemory) .append("Mb)\n"); out.append("java -jar -Xms1024m -Xmx1024m ").append(JAR_NAME); } else if (os.startsWith("Mac")) { out.append("# SIP-Creator Startup Script for Mac OSX (more memory than ").append(totalMemory) .append("Mb)\n"); out.append("java -jar -Xms1024m -Xmx1024m ").append(JAR_NAME); } else {// ww w.j av a2 s. c om System.out.println("Unrecognized OS: " + os); } String script = out.toString(); final JDialog dialog = new JDialog(null, "Memory Not Configured Yet!", Dialog.ModalityType.APPLICATION_MODAL); JTextArea scriptArea = new JTextArea(3, 40); scriptArea.setText(script); scriptArea.setSelectionStart(0); scriptArea.setSelectionEnd(script.length()); JPanel scriptPanel = new JPanel(new BorderLayout()); scriptPanel.setBorder(BorderFactory.createTitledBorder("Script File")); scriptPanel.add(scriptArea, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); buttonPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15)); JButton ok = new JButton("OK, Continue anyway"); ok.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.setVisible(false); EventQueue.invokeLater(LAUNCH); } }); buttonPanel.add(ok); JPanel centralPanel = new JPanel(new GridLayout(0, 1)); centralPanel.setBorder(BorderFactory.createEmptyBorder(15, 25, 15, 25)); centralPanel.add( new JLabel("<html><b>The SIP-Creator started directly can have too little default memory allocated." + "<br>It should be started with the following script:</b>")); centralPanel.add(scriptPanel); centralPanel.add(new JLabel( "<html><b>Please copy the above text into a batch or script file and execute that instead.</b>")); dialog.getContentPane().add(centralPanel, BorderLayout.CENTER); dialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH); dialog.pack(); Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((dimension.getWidth() - dialog.getWidth()) / 2); int y = (int) ((dimension.getHeight() - dialog.getHeight()) / 2); dialog.setLocation(x, y); dialog.setVisible(true); }
From source file:de.codesourcery.jasm16.ide.ui.utils.UIUtils.java
public static void showErrorDialog(Component parent, String title, String textMessage, Throwable cause) { final String stacktrace; if (cause == null) { stacktrace = null;/* w w w . j a v a2 s . c o m*/ } else { final ByteArrayOutputStream stackTrace = new ByteArrayOutputStream(); cause.printStackTrace(new PrintStream(stackTrace)); stacktrace = new String(stackTrace.toByteArray()); } final JDialog dialog = new JDialog((Window) null, title); dialog.setModal(true); final JTextArea message = createMultiLineLabel(textMessage); final DialogResult[] outcome = { DialogResult.CANCEL }; final JButton okButton = new JButton("Ok"); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { outcome[0] = DialogResult.YES; dialog.dispose(); } }); final JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(okButton); final JPanel messagePanel = new JPanel(); messagePanel.setLayout(new GridBagLayout()); GridBagConstraints cnstrs = constraints(0, 0, true, true, GridBagConstraints.BOTH); cnstrs.weightx = 1; cnstrs.weighty = 0.1; cnstrs.gridheight = 1; messagePanel.add(message, cnstrs); if (stacktrace != null) { final JTextArea createMultiLineLabel = new JTextArea(stacktrace); createMultiLineLabel.setBackground(null); createMultiLineLabel.setEditable(false); createMultiLineLabel.setBorder(null); createMultiLineLabel.setLineWrap(false); createMultiLineLabel.setWrapStyleWord(false); final JScrollPane pane = new JScrollPane(createMultiLineLabel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); cnstrs = constraints(0, 1, true, true, GridBagConstraints.BOTH); cnstrs.weightx = 1.0; cnstrs.weighty = 0.9; cnstrs.gridwidth = GridBagConstraints.REMAINDER; cnstrs.gridheight = GridBagConstraints.REMAINDER; messagePanel.add(pane, cnstrs); } final JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); cnstrs = constraints(0, 0, true, false, GridBagConstraints.BOTH); cnstrs.gridwidth = GridBagConstraints.REMAINDER; cnstrs.gridheight = 1; cnstrs.weighty = 1.0; cnstrs.insets = new Insets(5, 2, 5, 2); // top,left,bottom,right panel.add(messagePanel, cnstrs); cnstrs = constraints(0, 1, true, true, GridBagConstraints.HORIZONTAL); cnstrs.gridwidth = GridBagConstraints.REMAINDER; cnstrs.gridheight = 1; cnstrs.weighty = 0; cnstrs.insets = new Insets(0, 2, 10, 2); // top,left,bottom,right panel.add(buttonPanel, cnstrs); dialog.getContentPane().add(panel); dialog.setMinimumSize(new Dimension(600, 400)); dialog.setPreferredSize(new Dimension(600, 400)); dialog.setMaximumSize(new Dimension(600, 400)); dialog.pack(); dialog.setVisible(true); }
From source file:de.codesourcery.jasm16.ide.ui.utils.UIUtils.java
/** * /*from w ww .j av a 2 s . c o m*/ * @param parent * @param title * @param message * @return <code>true</code> if project should also be physically deleted, * <code>false</code> is project should be deleted but all files should be left alone, * <code>null</code> if user cancelled the dialog/project should not be deleted */ public static Boolean showDeleteProjectDialog(IAssemblyProject project) { final JDialog dialog = new JDialog((Window) null, "Delete project " + project.getName()); dialog.setModal(true); final JTextArea message = createMultiLineLabel( "Do you really want to delete project '" + project.getName() + " ?"); final JCheckBox checkbox = new JCheckBox("Delete project files"); final DialogResult[] outcome = { DialogResult.CANCEL }; final JButton yesButton = new JButton("Yes"); yesButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { outcome[0] = DialogResult.YES; dialog.dispose(); } }); final JButton noButton = new JButton("No"); noButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { outcome[0] = DialogResult.NO; dialog.dispose(); } }); final JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { outcome[0] = DialogResult.CANCEL; dialog.dispose(); } }); final JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(yesButton); buttonPanel.add(noButton); buttonPanel.add(cancelButton); final JPanel messagePanel = new JPanel(); messagePanel.setLayout(new GridBagLayout()); GridBagConstraints cnstrs = constraints(0, 0, true, false, GridBagConstraints.NONE); cnstrs.gridwidth = GridBagConstraints.REMAINDER; cnstrs.weighty = 0; cnstrs.gridheight = 1; messagePanel.add(message, cnstrs); cnstrs = constraints(0, 1, true, true, GridBagConstraints.NONE); cnstrs.gridwidth = GridBagConstraints.REMAINDER; cnstrs.gridheight = 1; cnstrs.weighty = 0; messagePanel.add(checkbox, cnstrs); final JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); cnstrs = constraints(0, 0, true, false, GridBagConstraints.NONE); cnstrs.gridwidth = GridBagConstraints.REMAINDER; cnstrs.gridheight = 1; cnstrs.weighty = 0; cnstrs.insets = new Insets(5, 2, 5, 2); // top,left,bottom,right panel.add(messagePanel, cnstrs); cnstrs = constraints(0, 1, true, true, GridBagConstraints.HORIZONTAL); cnstrs.gridwidth = GridBagConstraints.REMAINDER; cnstrs.gridheight = 1; cnstrs.weighty = 0; cnstrs.insets = new Insets(0, 2, 10, 2); // top,left,bottom,right panel.add(buttonPanel, cnstrs); dialog.getContentPane().add(panel); dialog.pack(); dialog.setVisible(true); if (outcome[0] != DialogResult.YES) { return null; } return checkbox.isSelected(); }
From source file:lu.lippmann.cdb.lab.mds.MDSViewBuilder.java
/** * //w w w . ja va2s. c o m */ public static JXPanel buildMDSViewFromDataSet(final Instances instances, final MDSResult mdsResult, final int maxInstances, final Listener<Instances> listener, final String... attrNameToUseAsPointTitle) throws Exception { final XYSeriesCollection dataset = new XYSeriesCollection(); final JFreeChart chart = ChartFactory.createScatterPlot("", // title "X", "Y", // axis labels dataset, // dataset PlotOrientation.VERTICAL, attrNameToUseAsPointTitle.length == 0, // legend? true, // tooltips? yes false // URLs? no ); final XYPlot xyPlot = (XYPlot) chart.getPlot(); xyPlot.setBackgroundPaint(Color.WHITE); xyPlot.getDomainAxis().setTickLabelsVisible(false); xyPlot.getRangeAxis().setTickLabelsVisible(false); //FIXME : should be different for Shih if (!mdsResult.isNormalized()) { String stress = FormatterUtil.DECIMAL_FORMAT .format(ClassicMDS.getKruskalStressFromMDSResult(mdsResult)); chart.setTitle(mdsResult.getCInstances().isCollapsed() ? "Collapsed MDS(Instances=" + maxInstances + ",Stress=" + stress + ")" : "MDS(Stress=" + stress + ")"); } else { chart.setTitle(mdsResult.getCInstances().isCollapsed() ? "Collapsed MDS(Instances=" + maxInstances + ")" : "MDS"); } final SimpleMatrix coordinates = mdsResult.getCoordinates(); buildFilteredSeries(mdsResult, xyPlot, attrNameToUseAsPointTitle); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setMouseWheelEnabled(true); chartPanel.setPreferredSize(new Dimension(1200, 900)); chartPanel.setBorder(new TitledBorder("MDS Projection")); chartPanel.setBackground(Color.WHITE); final JButton selectionButton = new JButton("Select data"); selectionButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { final org.jfree.data.Range XDomainRange = xyPlot.getDomainAxis().getRange(); final org.jfree.data.Range YDomainRange = xyPlot.getRangeAxis().getRange(); final Instances cInstances = mdsResult.getCollapsedInstances(); final Instances selectedInstances = new Instances(cInstances, 0); List<Instances> clusters = null; if (mdsResult.getCInstances().isCollapsed()) { clusters = mdsResult.getCInstances().getCentroidMap().getClusters(); } for (int i = 0; i < cInstances.numInstances(); i++) { final Instance centroid = instances.instance(i); if (XDomainRange.contains(coordinates.get(i, 0)) && YDomainRange.contains(coordinates.get(i, 1))) { if (mdsResult.getCInstances().isCollapsed()) { if (clusters != null) { final Instances elementsOfCluster = clusters.get(i); final int nbElements = elementsOfCluster.numInstances(); for (int k = 0; k < nbElements; k++) { selectedInstances.add(elementsOfCluster.get(k)); } } } else { selectedInstances.add(centroid); } } } if (listener != null) { listener.onAction(selectedInstances); } } }); final JXPanel allPanel = new JXPanel(); allPanel.setLayout(new BorderLayout()); allPanel.add(chartPanel, BorderLayout.CENTER); final JXPanel southPanel = new JXPanel(); southPanel.add(selectionButton); allPanel.add(southPanel, BorderLayout.SOUTH); return allPanel; }
From source file:PagingTester.java
public static JScrollPane createPagingScrollPaneForTable(JTable jt) { JScrollPane jsp = new JScrollPane(jt); TableModel tmodel = jt.getModel(); // Don't choke if this is called on a regular table . . . if (!(tmodel instanceof PagingModel)) { return jsp; }/* w w w. j a va2s .c o m*/ // Okay, go ahead and build the real scrollpane final PagingModel model = (PagingModel) tmodel; final JButton upButton = new JButton(new ArrowIcon(ArrowIcon.UP)); upButton.setEnabled(false); // starts off at 0, so can't go up final JButton downButton = new JButton(new ArrowIcon(ArrowIcon.DOWN)); if (model.getPageCount() <= 1) { downButton.setEnabled(false); // One page...can't scroll down } upButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { model.pageUp(); // If we hit the top of the data, disable the up button. if (model.getPageOffset() == 0) { upButton.setEnabled(false); } downButton.setEnabled(true); } }); downButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { model.pageDown(); // If we hit the bottom of the data, disable the down button. if (model.getPageOffset() == (model.getPageCount() - 1)) { downButton.setEnabled(false); } upButton.setEnabled(true); } }); // Turn on the scrollbars; otherwise we won't get our corners. jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); // Add in the corners (page up/down). jsp.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, upButton); jsp.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, downButton); return jsp; }
From source file:eu.europa.ec.markt.dss.applet.util.ComponentFactory.java
/** * // w w w . j a v a 2 s . com * @param label * @param actionListener * @param enabled * @return */ public static JButton createButton(final String label, final boolean enabled, final ActionListener actionListener) { final JButton button = new JButton(label); button.setEnabled(enabled); button.addActionListener(actionListener); return button; }
From source file:levelBuilder.DialogMaker.java
/** * First window to interact with. Offers options of load graph or new graph. *///from ww w . jav a 2 s .c o m private static void splashWindow() { final JFrame frame = new JFrame("Dialog Maker"); //Handles button pushes. class SplashActionHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); if (e.getActionCommand().equals("loadGraph")) loadFilePopup(); else loadGraph(true); } } //Loads and sets background image. BufferedImage img = null; try { img = ImageIO.read(new File(imgDir + "splash.png")); } catch (IOException e) { e.printStackTrace(); } final BufferedImage img2 = img; JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img2, 0, 0, null); } }; panel.setOpaque(true); panel.setLayout(new BorderLayout()); panel.setPreferredSize(new Dimension(800, 600)); panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 300, 0)); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); buttonPanel.setOpaque(false); //Buttons JButton loadMap = new JButton("Load Graph"); loadMap.setActionCommand("loadGraph"); loadMap.addActionListener(new SplashActionHandler()); buttonPanel.add(loadMap); JButton newMap = new JButton("New Graph"); newMap.setActionCommand("newGraph"); newMap.addActionListener(new SplashActionHandler()); buttonPanel.add(newMap); panel.add(buttonPanel, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.pack(); frame.setVisible(true); }
From source file:levelBuilder.DialogMaker.java
/** * Provides other action buttons.//from w w w.ja va2 s .c om */ private static void actionWindow() { //Performs actions based on button pushes. class ActionHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "edge": if (addedEdgeID != 0.0) { DialogNode n = g.getSource(addedEdgeID); int length; if (n.getChildren().length == 0) length = 0; else length = n.getChildren().length; DialogNode[] newChildren = new DialogNode[length + 1]; for (int c = 0; c < newChildren.length - 1; c++) newChildren[c] = n.getChildren()[c]; newChildren[newChildren.length - 1] = nodeMap.get(g.getDest(addedEdgeID).getText()); n.setChildren(newChildren); addedEdgeID = 0.0; } case "save": saveGraph(); case "check": //Checks for errors and displays them if they exist. ArrayList<String> errorList = new ArrayList<String>(); errorList = dg.check(); if (errorList.isEmpty()) { JOptionPane.showMessageDialog(null, "No errors!", null, JOptionPane.PLAIN_MESSAGE); } else { JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0)); for (String error : errorList) panel.add(new JLabel(error)); JOptionPane.showMessageDialog(null, panel, "Errors. Fix these to continue.", JOptionPane.PLAIN_MESSAGE); } case "reload": loadGraph(false); } } } JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0)); //Buttons JButton button3 = new JButton("New edge"); button3.setActionCommand("edge"); button3.addActionListener(new ActionHandler()); panel.add(button3); JButton button6 = new JButton("Save"); button6.setActionCommand("save"); button6.addActionListener(new ActionHandler()); panel.add(button6); JButton button7 = new JButton("Check"); button7.setActionCommand("check"); button7.addActionListener(new ActionHandler()); panel.add(button7); JButton button1 = new JButton("Reload"); button1.setActionCommand("reload"); button1.addActionListener(new ActionHandler()); panel.add(button1); JFrame frame = new JFrame("Other Input"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.pack(); frame.setLocation(windowWidth + horizontalWindowPlacement, 0); frame.setVisible(true); }
From source file:levelBuilder.DialogMaker.java
/** * Displays properties of the currently selected node. * Also allows changing of most node properties. */// w w w. ja v a 2 s. c o m private static void nodePropertiesWindow() { //Performs actions based on button pushes. class ActionHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "probSet": //parsing new probSet string to double[] ArrayList<double[]> probSets = selectedNode.getProbSets(); if (probSetField.getText().equals("")) probSets.remove(strategy); else { String[] probSetInput = probSetField.getText().split(","); int numChildren = probSetInput.length; double[] newProbSet = new double[numChildren]; for (int c = 0; c < numChildren; c++) newProbSet[c] = Double.parseDouble(probSetInput[c]); if (probSets.size() > strategy)//TODO is this right? probSets.add(strategy, newProbSet); else probSets.set(strategy, newProbSet); } selectedNode.setProbSets(probSets); case "npc": if (isNPCBoxChecked) selectedNode.setNPC(); else selectedNode.setPC(); case "text": dg.changeNodeText(selectedNode, textField.getText()); case "strategy": strategy = Integer.parseInt(strategyField.getText()); } } } JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 0, 0)); JPanel labelPanel = new JPanel(new GridLayout(0, 1, 0, 0)); JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 0, 0)); JPanel masterPanel = new JPanel(new GridLayout(0, 3, 0, 0)); //Buttons, ckeckboxes, textboxes, and labels textField = new JTextField("May I buy your wand?", 10); fieldPanel.add(textField); labelPanel.add(new JLabel("Node Text")); JButton button1 = new JButton("Change"); button1.setActionCommand("text"); button1.addActionListener(new ActionHandler()); buttonPanel.add(button1); npcBox = new JCheckBox(); npcBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.DESELECTED) isNPCBoxChecked = false; else isNPCBoxChecked = true; } }); fieldPanel.add(npcBox); labelPanel.add(new JLabel("NPC")); JButton button2 = new JButton("Change"); button2.setActionCommand("npc"); button2.addActionListener(new ActionHandler()); buttonPanel.add(button2); childrenField = new JTextField("child1,child2", 10); fieldPanel.add(childrenField); labelPanel.add(new JLabel("Children")); JButton button3 = new JButton(""); buttonPanel.add(button3); probSetField = new JTextField("0.1,0.9", 10); fieldPanel.add(probSetField); labelPanel.add(new JLabel("Probability set")); JButton button4 = new JButton("Change"); button4.setActionCommand("probSet"); button4.addActionListener(new ActionHandler()); buttonPanel.add(button4); strategyField = new JTextField("0", 10); fieldPanel.add(strategyField); labelPanel.add(new JLabel("Strategy")); JButton button5 = new JButton("Change"); button5.setActionCommand("strategy"); button5.addActionListener(new ActionHandler()); buttonPanel.add(button5); masterPanel.add(buttonPanel); masterPanel.add(fieldPanel); masterPanel.add(labelPanel); JFrame frame = new JFrame("Node Properties"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fieldPanel.setOpaque(true); frame.setContentPane(masterPanel); frame.pack(); frame.setLocation(windowWidth, verticalWindowPlacement); frame.setVisible(true); verticalWindowPlacement += frame.getBounds().height; }