List of usage examples for javax.swing BoxLayout BoxLayout
@ConstructorProperties({ "target", "axis" }) public BoxLayout(Container target, int axis)
From source file:Main.java
public static void main(String[] args) { JTextArea textArea = new JTextArea(10, 20); final JProgressBar progressBar = new JProgressBar(0, 10); final CounterTask task = new CounterTask(); task.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if ("progress".equals(evt.getPropertyName())) { progressBar.setValue((Integer) evt.getNewValue()); }//from w w w . j a v a 2 s.c om } }); JButton startButton = new JButton("Start"); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { task.execute(); } }); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { task.cancel(true); } }); JPanel buttonPanel = new JPanel(); buttonPanel.add(startButton); buttonPanel.add(cancelButton); JPanel cp = new JPanel(); LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS); cp.setLayout(layout); cp.add(buttonPanel); cp.add(new JScrollPane(textArea)); cp.add(progressBar); JFrame frame = new JFrame("SwingWorker Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); }
From source file:PassiveTextField.java
public static void main(String[] args) { try {/*from w ww .j av a 2 s .c o m*/ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } JFrame f = new JFrame("Passive Text Field"); f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS)); final PassiveTextField ptf = new PassiveTextField(32); JTextField tf = new JTextField(32); JPanel p = new JPanel(); JButton b = new JButton("OK"); p.add(b); f.getContentPane().add(ptf); f.getContentPane().add(tf); f.getContentPane().add(p); ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Action event from a text field"); } }; ptf.addActionListener(l); tf.addActionListener(l); // Make the button the default button f.getRootPane().setDefaultButton(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Content of text field: <" + ptf.getText() + ">"); } }); f.pack(); f.setVisible(true); }
From source file:net.redstonelamp.gui.RedstoneLampGUI.java
public static void main(String[] args) { JFrame frame = new JFrame("RedstoneLamp"); frame.setLayout(new GridLayout(2, 1)); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JLabel label = new JLabel("RedstoneLamp"); label.setHorizontalAlignment(SwingConstants.CENTER); frame.add(label);/*w w w .j a v a 2s .c om*/ JPanel lowPanel = new JPanel(); JPanel left = new JPanel(); left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS)); lowPanel.add(left); JPanel right = new JPanel(); right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS)); lowPanel.add(right); JButton openButton = new JButton("Open server at..."); openButton.addActionListener(e -> { JFileChooser chooser = new JFileChooser(new File(".")); chooser.setDialogTitle("Select RedstoneLamp server home"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setAcceptAllFileFilterUsed(false); int action = chooser.showOpenDialog(frame); if (action == JFileChooser.APPROVE_OPTION) { File selected = chooser.getSelectedFile(); File jar = new File("RedstoneLamp.jar"); if (!jar.isFile()) { int result = JOptionPane.showConfirmDialog(frame, "Could not find RedstoneLamp installation. " + "Would you like to install RedstoneLamp there?"); if (result == JOptionPane.YES_OPTION) { installCallback(frame, selected); } return; } frame.dispose(); addHistory(selected); currentRoot = new ServerActivity(selected); } }); right.add(openButton); JButton installButton = new JButton("Install server at..."); installButton.addActionListener(e -> { JFileChooser chooser = new JFileChooser("."); chooser.setDialogTitle("Select directory to install server in"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setAcceptAllFileFilterUsed(false); int action = chooser.showSaveDialog(frame); if (action == JFileChooser.APPROVE_OPTION) { File selected = chooser.getSelectedFile(); File jar = new File("RedstoneLamp.jar"); if (jar.isFile()) { int result = JOptionPane.showConfirmDialog(frame, "A RedstoneLamp jar installation is present. " + "Are you sure you want to reinstall RedstoneLamp there?"); if (result == JOptionPane.NO_OPTION) { frame.dispose(); addHistory(selected); currentRoot = new ServerActivity(selected); return; } } installCallback(frame, selected); } }); frame.add(lowPanel); frame.pack(); Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(dimension.width / 2 - frame.getSize().width / 2, dimension.height / 2 - frame.getSize().height / 2); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame aWindow = new JFrame(); aWindow.setBounds(200, 200, 200, 200); aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Box left = Box.createVerticalBox(); left.add(Box.createVerticalStrut(30)); ButtonGroup radioGroup = new ButtonGroup(); JRadioButton rbutton;//w ww. j a v a2s. c om radioGroup.add(rbutton = new JRadioButton("Red")); left.add(rbutton); left.add(Box.createVerticalStrut(30)); radioGroup.add(rbutton = new JRadioButton("Green")); left.add(rbutton); left.add(Box.createVerticalStrut(30)); radioGroup.add(rbutton = new JRadioButton("Blue")); left.add(rbutton); left.add(Box.createVerticalStrut(30)); radioGroup.add(rbutton = new JRadioButton("Yellow")); left.add(rbutton); left.add(Box.createGlue()); JPanel leftPanel = new JPanel(new BorderLayout()); leftPanel.add(left, BorderLayout.CENTER); Box right = Box.createVerticalBox(); right.add(Box.createVerticalStrut(30)); right.add(new JCheckBox("Dashed")); right.add(Box.createVerticalStrut(30)); right.add(new JCheckBox("Thick")); right.add(Box.createVerticalStrut(30)); right.add(new JCheckBox("Rounded")); right.add(Box.createGlue()); JPanel rightPanel = new JPanel(new BorderLayout()); rightPanel.add(right, BorderLayout.CENTER); Box top = Box.createHorizontalBox(); top.add(leftPanel); top.add(Box.createHorizontalStrut(5)); top.add(rightPanel); Container content = aWindow.getContentPane(); content.setLayout(new BorderLayout()); content.add(top, BorderLayout.CENTER); BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS); content.setLayout(box); content.add(top); aWindow.setVisible(true); }
From source file:MainClass.java
public static void main(String[] args) { JFrame aWindow = new JFrame("This is a Box Layout"); aWindow.setBounds(200, 200, 200, 200); aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Box left = Box.createVerticalBox(); left.add(Box.createVerticalStrut(30)); ButtonGroup radioGroup = new ButtonGroup(); JRadioButton rbutton;//from w w w.j a v a 2s . co m radioGroup.add(rbutton = new JRadioButton("Red")); left.add(rbutton); left.add(Box.createVerticalStrut(30)); radioGroup.add(rbutton = new JRadioButton("Green")); left.add(rbutton); left.add(Box.createVerticalStrut(30)); radioGroup.add(rbutton = new JRadioButton("Blue")); left.add(rbutton); left.add(Box.createVerticalStrut(30)); radioGroup.add(rbutton = new JRadioButton("Yellow")); left.add(rbutton); left.add(Box.createGlue()); JPanel leftPanel = new JPanel(new BorderLayout()); leftPanel.setBorder(new TitledBorder(new EtchedBorder(), "Line Color")); leftPanel.add(left, BorderLayout.CENTER); Box right = Box.createVerticalBox(); right.add(Box.createVerticalStrut(30)); right.add(new JCheckBox("Dashed")); right.add(Box.createVerticalStrut(30)); right.add(new JCheckBox("Thick")); right.add(Box.createVerticalStrut(30)); right.add(new JCheckBox("Rounded")); right.add(Box.createGlue()); JPanel rightPanel = new JPanel(new BorderLayout()); rightPanel.setBorder(new TitledBorder(new EtchedBorder(), "Line Properties")); rightPanel.add(right, BorderLayout.CENTER); Box top = Box.createHorizontalBox(); top.add(leftPanel); top.add(Box.createHorizontalStrut(5)); top.add(rightPanel); Container content = aWindow.getContentPane(); content.setLayout(new BorderLayout()); content.add(top, BorderLayout.CENTER); BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS); content.setLayout(box); content.add(top); aWindow.setVisible(true); }
From source file:URLMonitorPanel.java
public static void main(String[] args) throws Exception { JFrame frame = new JFrame("URL Monitor"); Container c = frame.getContentPane(); c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS)); Timer t = new Timer(); String[] u = new String[] { "http://www.java2s.com", "http://www.java2s.com" }; for (int i = 0; i < u.length; i++) { c.add(new URLMonitorPanel(u[i], t)); }//w w w. j a v a 2 s . com frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); frame.pack(); frame.show(); }
From source file:moviedatas.MovieDatas.java
public static void main(String[] args) { FilterController fpc = new FilterController(); SortController spc = new SortController(); //1. Create the frame. JFrame frame = new JFrame("Movies Open Datas by Harp-e"); frame.setPreferredSize(new Dimension(1280, 800)); //2. Optional: What happens when the frame closes? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MovieListView movieListView = new MovieListView(); JPanel movieListPanel = movieListView.createViewPanel(); TitledBorder moviesTitle;/* w ww .j a v a 2 s . c om*/ moviesTitle = BorderFactory.createTitledBorder("Movies"); movieListPanel.setBorder(moviesTitle); //3. Create components and put them in the frame. //---------------------------------------------------------------------- // Sort & Filter panel (right) //---------------------------------------------------------------------- SortPanelView sortFilterView = new SortPanelView(); FilterPanelView filterPanelView = new FilterPanelView(); JPanel sortPanel = sortFilterView.createSortPanel(); JPanel filterPanel = filterPanelView.createFilterPanel(); JPanel sortFilterPanel = new JPanel(); sortFilterPanel.setLayout(new BoxLayout(sortFilterPanel, BoxLayout.PAGE_AXIS)); sortFilterPanel.add(sortPanel); sortFilterPanel.add(filterPanel); frame.getContentPane().add(sortFilterPanel, BorderLayout.WEST); //---------------------------------------------------------------------- // Movie Info Panel (left) //---------------------------------------------------------------------- MovieInfoController movieInfoController = new MovieInfoController(); JPanel movieInfoView = movieInfoController.initView(); TitledBorder infoTitle; infoTitle = BorderFactory.createTitledBorder("Informations"); movieInfoView.setBorder(infoTitle); //---------------------------------------------------------------------- // Movie List Panel (middle) //---------------------------------------------------------------------- JPanel listPanel = new JPanel(); listPanel.setLayout(new BorderLayout()); listPanel.setPreferredSize(new Dimension(1280, 400)); listPanel.add(sortFilterPanel, BorderLayout.WEST); listPanel.add(movieInfoView, BorderLayout.EAST); listPanel.add(movieListPanel, BorderLayout.CENTER); frame.getContentPane().add(listPanel, BorderLayout.NORTH); //---------------------------------------------------------------------- // Charts Panel (bottom) //---------------------------------------------------------------------- JPanel chartPanel = new JPanel(); // Spider chart Panel //______________________________________________________________________ // Create the panel JPanel spiderPanel = new JPanel(); // Create the view SpiderChartView spiderChartView = new SpiderChartView(); // Create a tilte TitledBorder spiderTitle; // Put a border around the title spiderTitle = BorderFactory.createTitledBorder("Spider chart"); // Put the border on the panel spiderPanel.setBorder(spiderTitle); // Put the view on the panel spiderPanel.add(spiderChartView.initView()); // Put the spider panel on the global panel chartPanel.add(spiderPanel); //______________________________________________________________________ // Bar chart Panel //______________________________________________________________________ JPanel barPanel = new JPanel(); BarChartView barChartView = new BarChartView(); TitledBorder barTitle; barTitle = BorderFactory.createTitledBorder("Bar chart"); barPanel.setBorder(barTitle); barPanel.add(barChartView.initView()); chartPanel.add(barPanel); //______________________________________________________________________ // Global chart Panel //______________________________________________________________________ JPanel globalChartPanel = new JPanel(); GlobalChart globalChartView = new GlobalChart(); TitledBorder globalTitle; globalTitle = BorderFactory.createTitledBorder("Global chart"); globalChartPanel.setBorder(globalTitle); globalChartPanel.add(globalChartView.initView()); chartPanel.add(globalChartPanel); //______________________________________________________________________ frame.getContentPane().add(chartPanel, BorderLayout.CENTER); //---------------------------------------------------------------------- //4. Size the frame. frame.pack(); //5. Show it. frame.setVisible(true); //ArrayList<Movie> movies = new ArrayList<>(); //FilterController.filter(10,SortController.byTitle(movies)); }
From source file:com.amazonaws.services.kinesis.leases.impl.LeaseCoordinatorExerciser.java
public static void main(String[] args) throws InterruptedException, DependencyException, InvalidStateException, ProvisionedThroughputException, IOException { int numCoordinators = 9; int numLeases = 73; int leaseDurationMillis = 10000; int epsilonMillis = 100; AWSCredentialsProvider creds = new DefaultAWSCredentialsProviderChain(); AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(creds); ILeaseManager<KinesisClientLease> leaseManager = new KinesisClientLeaseManager("nagl_ShardProgress", ddb); if (leaseManager.createLeaseTableIfNotExists(10L, 50L)) { LOG.info("Waiting for newly created lease table"); if (!leaseManager.waitUntilLeaseTableExists(10, 300)) { LOG.error("Table was not created in time"); return; }//w w w.jav a2s . com } CWMetricsFactory metricsFactory = new CWMetricsFactory(creds, "testNamespace", 30 * 1000, 1000); final List<LeaseCoordinator<KinesisClientLease>> coordinators = new ArrayList<LeaseCoordinator<KinesisClientLease>>(); for (int i = 0; i < numCoordinators; i++) { String workerIdentifier = "worker-" + Integer.toString(i); LeaseCoordinator<KinesisClientLease> coord = new LeaseCoordinator<KinesisClientLease>(leaseManager, workerIdentifier, leaseDurationMillis, epsilonMillis, metricsFactory); coordinators.add(coord); } leaseManager.deleteAll(); for (int i = 0; i < numLeases; i++) { KinesisClientLease lease = new KinesisClientLease(); lease.setLeaseKey(Integer.toString(i)); lease.setCheckpoint(new ExtendedSequenceNumber("checkpoint")); leaseManager.createLeaseIfNotExists(lease); } final JFrame frame = new JFrame("Test Visualizer"); frame.setPreferredSize(new Dimension(800, 600)); final JPanel panel = new JPanel(new GridLayout(coordinators.size() + 1, 0)); final JLabel ticker = new JLabel("tick"); panel.add(ticker); frame.getContentPane().add(panel); final Map<String, JLabel> labels = new HashMap<String, JLabel>(); for (final LeaseCoordinator<KinesisClientLease> coord : coordinators) { JPanel coordPanel = new JPanel(); coordPanel.setLayout(new BoxLayout(coordPanel, BoxLayout.X_AXIS)); final Button button = new Button("Stop " + coord.getWorkerIdentifier()); button.setMaximumSize(new Dimension(200, 50)); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (coord.isRunning()) { coord.stop(); button.setLabel("Start " + coord.getWorkerIdentifier()); } else { try { coord.start(); } catch (LeasingException e) { LOG.error(e); } button.setLabel("Stop " + coord.getWorkerIdentifier()); } } }); coordPanel.add(button); JLabel label = new JLabel(); coordPanel.add(label); labels.put(coord.getWorkerIdentifier(), label); panel.add(coordPanel); } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); new Thread() { // Key is lease key, value is green-ness as a value from 0 to 255. // Great variable name, huh? private Map<String, Integer> greenNesses = new HashMap<String, Integer>(); // Key is lease key, value is last owning worker private Map<String, String> lastOwners = new HashMap<String, String>(); @Override public void run() { while (true) { for (LeaseCoordinator<KinesisClientLease> coord : coordinators) { String workerIdentifier = coord.getWorkerIdentifier(); JLabel label = labels.get(workerIdentifier); List<KinesisClientLease> asgn = new ArrayList<KinesisClientLease>(coord.getAssignments()); Collections.sort(asgn, new Comparator<KinesisClientLease>() { @Override public int compare(KinesisClientLease arg0, KinesisClientLease arg1) { return arg0.getLeaseKey().compareTo(arg1.getLeaseKey()); } }); StringBuilder builder = new StringBuilder(); builder.append("<html>"); builder.append(workerIdentifier).append(":").append(asgn.size()).append(" "); for (KinesisClientLease lease : asgn) { String leaseKey = lease.getLeaseKey(); String lastOwner = lastOwners.get(leaseKey); // Color things green when they switch owners, decay the green-ness over time. Integer greenNess = greenNesses.get(leaseKey); if (greenNess == null || lastOwner == null || !lastOwner.equals(lease.getLeaseOwner())) { greenNess = 200; } else { greenNess = Math.max(0, greenNess - 20); } greenNesses.put(leaseKey, greenNess); lastOwners.put(leaseKey, lease.getLeaseOwner()); builder.append(String.format("<font color=\"%s\">%03d</font>", String.format("#00%02x00", greenNess), Integer.parseInt(leaseKey))).append(" "); } builder.append("</html>"); label.setText(builder.toString()); label.revalidate(); label.repaint(); } if (ticker.getText().equals("tick")) { ticker.setText("tock"); } else { ticker.setText("tick"); } try { Thread.sleep(200); } catch (InterruptedException e) { } } } }.start(); frame.pack(); frame.setVisible(true); for (LeaseCoordinator<KinesisClientLease> coord : coordinators) { coord.start(); } }
From source file:FileTree.java
/** Main: make a Frame, add a FileTree */ public static void main(String[] av) { JFrame frame = new JFrame("FileTree"); frame.setForeground(Color.black); frame.setBackground(Color.lightGray); Container cp = frame.getContentPane(); if (av.length == 0) { cp.add(new FileTree(new File("."))); } else {//from w ww . ja v a2 s. com cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS)); for (int i = 0; i < av.length; i++) cp.add(new FileTree(new File(av[i]))); } frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
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);/*from ww w .j av a2 s . c o 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); }