List of usage examples for javax.swing JButton getText
public String getText()
From source file:Main.java
public static void main(String[] args) { JProgressBar progressBar = new JProgressBar(); JButton button = new JButton("Start"); JFrame f = new JFrame(); f.setLayout(new FlowLayout()); f.add(progressBar);/*from w ww . j a va2 s. c o m*/ f.add(button); ActionListener updateProBar = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int val = progressBar.getValue(); if (val >= 100) { // timer.stop(); button.setText("End"); return; } progressBar.setValue(++val); } }; Timer timer = new Timer(50, updateProBar); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (timer.isRunning()) { timer.stop(); button.setText("Start"); } else if (button.getText() != "End") { timer.start(); button.setText("Stop"); } } }); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { final Timer timer; final JProgressBar progressBar = new JProgressBar(); final JButton button = new JButton("Start"); JFrame f = new JFrame(); f.setLayout(new FlowLayout()); f.add(progressBar);/*from ww w . j a va 2 s. co m*/ f.add(button); ActionListener updateProBar = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int val = progressBar.getValue(); if (val >= 100) { // timer.stop(); button.setText("End"); return; } progressBar.setValue(++val); } }; timer = new Timer(50, updateProBar); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (timer.isRunning()) { timer.stop(); button.setText("Start"); } else if (button.getText() != "End") { timer.start(); button.setText("Stop"); } } }); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setResizable(false); f.setLocationRelativeTo(null); f.setVisible(true); }
From source file:Graph_with_jframe_and_arduino.java
public static void main(String[] args) { // create and configure the window JFrame window = new JFrame(); window.setTitle("Sensor Graph GUI"); window.setSize(600, 400);//ww w . ja va2 s .co m window.setLayout(new BorderLayout()); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create a drop-down box and connect button, then place them at the top of the window JComboBox<String> portList_combobox = new JComboBox<String>(); Dimension d = new Dimension(300, 100); portList_combobox.setSize(d); JButton connectButton = new JButton("Connect"); JPanel topPanel = new JPanel(); topPanel.add(portList_combobox); topPanel.add(connectButton); window.add(topPanel, BorderLayout.NORTH); //pause button JButton Pause_btn = new JButton("Start"); // populate the drop-down box SerialPort[] portNames; portNames = SerialPort.getCommPorts(); //check for new port available Thread thread_port = new Thread() { @Override public void run() { while (true) { SerialPort[] sp = SerialPort.getCommPorts(); if (sp.length > 0) { for (SerialPort sp_name : sp) { int l = portList_combobox.getItemCount(), i; for (i = 0; i < l; i++) { //check port name already exist or not if (sp_name.getSystemPortName().equalsIgnoreCase(portList_combobox.getItemAt(i))) { break; } } if (i == l) { portList_combobox.addItem(sp_name.getSystemPortName()); } } } else { portList_combobox.removeAllItems(); } portList_combobox.repaint(); } } }; thread_port.start(); for (SerialPort sp_name : portNames) portList_combobox.addItem(sp_name.getSystemPortName()); //for(int i = 0; i < portNames.length; i++) // portList.addItem(portNames[i].getSystemPortName()); // create the line graph XYSeries series = new XYSeries("line 1"); XYSeries series2 = new XYSeries("line 2"); XYSeries series3 = new XYSeries("line 3"); XYSeries series4 = new XYSeries("line 4"); for (int i = 0; i < 100; i++) { series.add(x, 0); series2.add(x, 0); series3.add(x, 0); series4.add(x, 10); x++; } XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series); dataset.addSeries(series2); XYSeriesCollection dataset2 = new XYSeriesCollection(); dataset2.addSeries(series3); dataset2.addSeries(series4); //create jfree chart JFreeChart chart = ChartFactory.createXYLineChart("Sensor Readings", "Time (seconds)", "Arduino Reading", dataset); JFreeChart chart2 = ChartFactory.createXYLineChart("Sensor Readings", "Time (seconds)", "Arduino Reading 2", dataset2); //color render for chart 1 XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer(); r1.setSeriesPaint(0, Color.RED); r1.setSeriesPaint(1, Color.GREEN); r1.setSeriesShapesVisible(0, false); r1.setSeriesShapesVisible(1, false); XYPlot plot = chart.getXYPlot(); plot.setRenderer(0, r1); plot.setRenderer(1, r1); plot.setBackgroundPaint(Color.WHITE); plot.setDomainGridlinePaint(Color.DARK_GRAY); plot.setRangeGridlinePaint(Color.blue); //color render for chart 2 XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer(); r2.setSeriesPaint(0, Color.BLUE); r2.setSeriesPaint(1, Color.ORANGE); r2.setSeriesShapesVisible(0, false); r2.setSeriesShapesVisible(1, false); XYPlot plot2 = chart2.getXYPlot(); plot2.setRenderer(0, r2); plot2.setRenderer(1, r2); ChartPanel cp = new ChartPanel(chart); ChartPanel cp2 = new ChartPanel(chart2); //multiple graph container JPanel graph_container = new JPanel(); graph_container.setLayout(new BoxLayout(graph_container, BoxLayout.X_AXIS)); graph_container.add(cp); graph_container.add(cp2); //add chart panel in main window window.add(graph_container, BorderLayout.CENTER); //window.add(cp2, BorderLayout.WEST); window.add(Pause_btn, BorderLayout.AFTER_LAST_LINE); Pause_btn.setEnabled(false); //pause btn action Pause_btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (Pause_btn.getText().equalsIgnoreCase("Pause")) { if (chosenPort.isOpen()) { try { Output.write(0); } catch (IOException ex) { Logger.getLogger(Graph_with_jframe_and_arduino.class.getName()).log(Level.SEVERE, null, ex); } } Pause_btn.setText("Start"); } else { if (chosenPort.isOpen()) { try { Output.write(1); } catch (IOException ex) { Logger.getLogger(Graph_with_jframe_and_arduino.class.getName()).log(Level.SEVERE, null, ex); } } Pause_btn.setText("Pause"); } throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }); // configure the connect button and use another thread to listen for data connectButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (connectButton.getText().equals("Connect")) { // attempt to connect to the serial port chosenPort = SerialPort.getCommPort(portList_combobox.getSelectedItem().toString()); chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0); if (chosenPort.openPort()) { Output = chosenPort.getOutputStream(); connectButton.setText("Disconnect"); Pause_btn.setEnabled(true); portList_combobox.setEnabled(false); } // create a new thread that listens for incoming text and populates the graph Thread thread = new Thread() { @Override public void run() { Scanner scanner = new Scanner(chosenPort.getInputStream()); while (scanner.hasNextLine()) { try { String line = scanner.nextLine(); int number = Integer.parseInt(line); series.add(x, number); series2.add(x, number / 2); series3.add(x, number / 1.5); series4.add(x, number / 5); if (x > 100) { series.remove(0); series2.remove(0); series3.remove(0); series4.remove(0); } x++; window.repaint(); } catch (Exception e) { } } scanner.close(); } }; thread.start(); } else { // disconnect from the serial port chosenPort.closePort(); portList_combobox.setEnabled(true); Pause_btn.setEnabled(false); connectButton.setText("Connect"); } } }); // show the window window.setVisible(true); }
From source file:Main.java
public static JButton getButton(Container container, String text) { JButton btn = null;/*ww w . jav a 2s.c o m*/ List<Container> children = new ArrayList<>(25); for (Component child : container.getComponents()) { if (child instanceof JButton) { JButton button = (JButton) child; if (text.equals(button.getText())) { btn = button; break; } } else if (child instanceof Container) { children.add((Container) child); } } if (btn == null) { for (Container cont : children) { JButton button = getButton(cont, text); if (button != null) { btn = button; break; } } } return btn; }
From source file:Main.java
/** * Determine the maximum size for a 2-state button with the specified text and icons. * This can be used to make sure that a button doesn't resize during state change. * * @param button the UI of this JButton is used for size determination * @param string1 text for 1st mode/*ww w.j a v a 2 s . c o m*/ * @param icon1 icon for 1st mode * @param string2 text for 2nd mode * @param icon2 icon for 2nd mode * @return the Dimension that contains both modes for the button. */ public static Dimension getMaxDimension(JButton button, String string1, ImageIcon icon1, String string2, ImageIcon icon2) { String originalText = button.getText(); Icon originalIcon = button.getIcon(); // Get dimensions for "Play" state button.setText(string1); button.setIcon(icon1); Dimension playSize = button.getUI().getPreferredSize(button); // Get dimensions for "Pause" state button.setText(string2); button.setIcon(icon2); Dimension pauseSize = button.getUI().getPreferredSize(button); // Restore original text and icon button.setText(originalText); button.setIcon(originalIcon); // Return max dimensions int maxWidth = (int) Math.max(playSize.getWidth(), pauseSize.getWidth()); int maxHeight = (int) Math.max(playSize.getHeight(), pauseSize.getHeight()); return new Dimension(maxWidth, maxHeight); }
From source file:Main.java
public static void setJButtonSizesTheSame(final JButton[] btns) { if (btns == null) { throw new IllegalArgumentException(); }//from w w w . j av a 2 s.c o m final Dimension maxSize = new Dimension(0, 0); for (int i = 0; i < btns.length; ++i) { final JButton btn = btns[i]; final FontMetrics fm = btn.getFontMetrics(btn.getFont()); final Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics()); final int boundsHeight = (int) bounds.getHeight(); final int boundsWidth = (int) bounds.getWidth(); maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width; maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height; } final Insets insets = btns[0].getInsets(); maxSize.width += insets.left + insets.right; maxSize.height += insets.top + insets.bottom; for (int i = 0; i < btns.length; ++i) { final JButton btn = btns[i]; btn.setPreferredSize(maxSize); } initComponentHeight(btns); }
From source file:Main.java
/** * Configures a button as if it was an hyperlink. * /*from w w w. j a va 2 s . co m*/ * @param button * the button to configure. */ public static void configureButtonAsHyperlink(JButton button) { if (button == null) { return; } StringBuffer html = new StringBuffer(); html.append("<html><font color=\"blue\"><u>"); html.append(button.getText()); html.append("</u></font></html>"); button.setText(html.toString()); button.setMargin(new Insets(0, 0, 0, 0)); button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); button.setFocusPainted(false); button.setBorderPainted(false); button.setContentAreaFilled(false); }
From source file:GUIUtils.java
/** * Change the sizes of all the passed buttons to be the size of the largest * one.//from w w w . j a va2s. c o m * * @param btns * Array of buttons to eb resized. * * @throws IllegalArgumentException * If <TT>btns</TT> is <TT>null</TT>. */ public static void setJButtonSizesTheSame(JButton[] btns) { if (btns == null) { throw new IllegalArgumentException("null JButton[] passed"); } // Get the largest width and height final Dimension maxSize = new Dimension(0, 0); for (int i = 0; i < btns.length; ++i) { final JButton btn = btns[i]; final FontMetrics fm = btn.getFontMetrics(btn.getFont()); Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics()); int boundsHeight = (int) bounds.getHeight(); int boundsWidth = (int) bounds.getWidth(); maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width; maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height; } Insets insets = btns[0].getInsets(); maxSize.width += insets.left + insets.right; maxSize.height += insets.top + insets.bottom; for (int i = 0; i < btns.length; ++i) { JButton btn = btns[i]; btn.setPreferredSize(maxSize); } }
From source file:AddingButtonWithActionListener.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()); System.out.println(button.getText()); }// ww w. j av a 2s . c o m }; button.addActionListener(actionListener); return 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()); }//w w w . jav a 2 s . co m }; button.addActionListener(actionListener); return button; }