Rendering Swing component smoothly every 500 millisecond - java

I am facing rendering problem when I call paintComponent() every 500 millisecond to show updated charts. I have around 30 barcharts created by using JFreeChart on Panel.
Rendering with error and
How can I solve this problem?
private void ShowGraphs() {
FirstChart.removeAll();
SecondChart.removeAll();
ThirdChart.removeAll();
FirstChart.add(Label1);
SecondChart.add(Label2);
ThirdChart.add(Label3);
ChartUpdate(P1,FirstChart);
ChartUpdate(P2,SecondChart);
ChartUpdate(P3,ThirdChart);
//FirstChart, SecondChart, ThirdChart is JPanels
//Tabb is JTabbedPane
paintComponents(Tabb.getGraphics());
}
This code is called every 500 milliseconds and ChartUpdate(MyObject, Panel) is chart building function on Panel using MyObject's info.

Don't replace the view component. Instead, update the corresponding model and the listening view will update itself in response. In the example below, each ChartPanel returned by createPane() has a Swing Timer that updates its XYSeries every 500 ms.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* #see http://stackoverflow.com/a/38512314/230513
* #see http://stackoverflow.com/a/15715096/230513
* #see http://stackoverflow.com/a/11949899/230513
*/
public class Test {
private static final int N = 128;
private static final Random random = new Random();
private int n = 1;
private void display() {
JFrame f = new JFrame("TabChart");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < 3; i++) {
p.add(createPane());
}
f.add(p, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private ChartPanel createPane() {
final XYSeries series = new XYSeries("Data");
for (int i = 0; i < random.nextInt(N) + N / 2; i++) {
series.add(i, random.nextGaussian());
}
XYSeriesCollection dataset = new XYSeriesCollection(series);
new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
series.add(series.getItemCount(), random.nextGaussian());
}
}).start();
JFreeChart chart = ChartFactory.createXYLineChart("Test", "Domain",
"Range", dataset, PlotOrientation.VERTICAL, false, false, false);
return new ChartPanel(chart) {
#Override
public Dimension getPreferredSize() {
return new Dimension(480, 240);
}
};
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Test().display();
}
});
}
}

Related

Smooth motion for JPanel and update JLabel at same time

How to move with smooth motion for JPanel and update JLabel at same time?
I want to show current time on a JFrame so I created a new java.util.Timer and update to label every one second.
I created another Java thread to as well, move the panel component.
But while moving the panel and showing (updating) time on the frame, panel refreshing to form original position.
So I search that problem in Google and can't find the solution.
//Code to move jPanel smoothly
Thread t = new Thread(){
int i = 0 ;
public void run(){
while(i<150){
i++;
jPanel2.setLocation(i, jPanel2.getY());
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
}
}
};
t.start();
// Code to show Time
Timer t = new javax.swing.Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jLabel1.setText(new Date()+"");
}
});
t.start();
Here is a small example, how to provide animation and update for a component.
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;
/**
* <code>MovedClock</code>.
*/
public class MovedClock {
private final JLabel clock = new JLabel();
private final DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm:ss");
private void startUI() {
JFrame frame = new JFrame("Moved clock");
frame.setLayout(null); // usually it's a bad idea, but for animation we need this.
clock.setBounds(0, 50, 50, 20);
frame.add(clock);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
updateClock();
Timer clockTimer = new Timer(1000, e -> updateClock());
clockTimer.start();
// 15 milliseconds for about 60fps
Timer moveTimer = new Timer(15, new ActionListener() {
private int count = 1;
private int increment = 1;
#Override
public void actionPerformed(ActionEvent e) {
if (count == 435 || count == 0) {
increment = -increment;
}
Point loc = clock.getLocation();
loc.x += increment;
clock.setLocation(loc);
count += increment;
}
});
moveTimer.start();
}
private void updateClock() {
clock.setText(LocalTime.now().format(format));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new MovedClock()::startUI);
}
}

Displaying all XYSeries at once in jFreeChart for improving speed

Suppose that we need to display multiple XYSeries in a single XYSeriesCollection. My problem is that every time I add a XYSeries, the JFreeChart wants to update the chart and that slows down the process of displaying multiple XYSeries.
What I want is similar to this:
// Do not update the chart
XYSeriesCollection.add(XYSeries1)
XYSeriesCollection.add(XYSeries2)
...
XYSeriesCollection.add(XYSeries10)
// Update the chart
How can I do this?
Construct a new XYSeriesCollection having the desired series, and invoke setDataset() on the XYPlot. This will generate a single DatasetChangeEvent.
Addendum: Here's an SSCCE that updates N series, each having N2 values. As this is a performance question, the example may be helpful in profiling.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class ChartPanelTest {
private static final int N = 16;
private static final Random random = new Random();
private static XYDataset createDataset() {
TimeSeriesCollection tsc = new TimeSeriesCollection();
for (int j = 0; j < N; j++) {
TimeSeries series = new TimeSeries("Data" + j);
Day current = new Day();
for (int i = 0; i < N * N; i++) {
series.add(current, random.nextGaussian());
current = (Day) current.next();
}
tsc.addSeries(series);
}
return tsc;
}
private static JFreeChart createChart(final XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Test", "Day", "Value", dataset, false, false, false);
return chart;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
final XYPlot plot = chart.getXYPlot();
ChartPanel chartPanel = new ChartPanel(chart) {
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 300);
}
};
f.add(chartPanel);
JPanel p = new JPanel();
p.add(new JButton(new AbstractAction("New") {
#Override
public void actionPerformed(ActionEvent e) {
plot.setDataset(createDataset());
}
}));
f.add(p, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
Looking at the documentation for XYSeriesCollection (assuming add should be addSeries) there is no method addAll (or similar).
If you want, you could extend XYSeriesCollection and implement addAll. In this method, you could temporarily disable all listeners before adding and re-add them after. However, this would probably be a bad idea, and would definitely need to be synchronized:
it would produce unpredictable polymorphism behavior
it would require reflection hacks, as AbstractDataset.listenerList is private
it could generate a security exception
it would not be thread safe
So the short answer is no, it is not feasible.
… but this is a starting point for how you could do it:
Field field = getClass().getDeclaredField("listenerList");
field.setAccessible(true);
EventListenerList ell = field.get(this);
// go from here
field.setAccessible(false);

more efficient layout than Box

I've got some very old code which uses a Box to list some information. I create it like so:
Box patterns = Box.createVerticalBox();
Very (very) often, new items are added and old items are removed eg:
label = new JLabel("xyz");
patterns.add(label);
and later
patterns.remove(label);
whenever something is added ore removed I have to have it repaint, so I call:
patterns.revalidate();
patterns.repaint();
Problem is, since this happens very often it chokes up the UI. I think I need a better implementation in order to make it more efficient.
I know I could maintain a list of the active items in the background and then intermittently update the actual UI (batch update) but...
Can someone suggest a more efficient alternative approach?
Why don't you just use a JList and implement a cell renderer?
Or more flexibility with a JTable and implement a table cell renderer (returns a Component instead)?
Based on this example, the following code loafs doing 16 labels at 10 Hz.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
/** #see https://stackoverflow.com/questions/6605554 */
public class ImageLabelPanel extends Box implements ActionListener {
private static final int N = 16;
private final List<JLabel> list = new ArrayList<JLabel>();
private final Timer timer = new Timer(100, this);
ImageLabelPanel() {
super(BoxLayout.Y_AXIS);
BufferedImage bi = null;
try {
bi = ImageIO.read(new File("image.jpg"));
} catch (IOException e) {
e.printStackTrace(System.err);
}
for (int r = 0; r < N; r++) {
int w = bi.getWidth();
int h = bi.getHeight() / N;
BufferedImage b = bi.getSubimage(0, r * h, w, h);
list.add(new JLabel(new ImageIcon(b)));
}
createPane();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
timer.start();
}
private void createPane() {
this.removeAll();
for (JLabel label : list) {
add(label);
}
this.revalidate();
}
#Override
public void actionPerformed(ActionEvent e) {
Collections.shuffle(list);
createPane();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ImageLabelPanel();
}
});
}
}

Java tabbed panes within internal frame

I am trying to get an internal frame to contain tabbed panes. However, my code does not seem to be loading the panes into the internal frame. I have my code in the java files, called InternalFrame.java and TabbedPaneSample.java. The code for both files is included below. Can anyone show me how to fix the code below so that it loads the tabbed panes when I run InternalFrame.java?
Here is my code:
The code for InternalFrame.java is:
package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
public class InternalFrame extends JFrame {
JButton openButton;
JLayeredPane desktop;
JInternalFrame internalFrame;
TabbedPaneSample myTabbedPaneSample = new TabbedPaneSample();
public InternalFrame() {
super("Click button to open internal frame with two panels.");
setSize(500, 400);
openButton = new JButton("Open");
Panel p = new Panel();
p.add(openButton);
add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
openButton.addActionListener(new OpenListener());
desktop = new JDesktopPane();
desktop.setOpaque(true);
add(desktop, BorderLayout.CENTER);
}
class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((internalFrame == null) || (internalFrame.isClosed())) {
internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
internalFrame.setBounds(50, 50, 200, 100);
internalFrame.add(myTabbedPaneSample, BorderLayout.CENTER);
internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
internalFrame.pack();
internalFrame.setMinimumSize(new Dimension(300, 300));
desktop.add(internalFrame, new Integer(1));
internalFrame.setVisible(true);
}
}
}
public static void main(String args[]) {
InternalFrame myInternalFrame = new InternalFrame();
myInternalFrame.setVisible(true);
}
}
And the code for TabbedPaneSample.java is:
package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class TabbedPaneSample extends JTabbedPane {
private JTabbedPane tabbedPane = new JTabbedPane();
private ImageIcon closeImage = new ImageIcon("C:/test/shipIcon.gif");
private Dimension closeButtonSize;
private int tabCounter = 0;
public TabbedPaneSample() {
closeButtonSize = new Dimension(closeImage.getIconWidth() + 2, closeImage.getIconHeight() + 2);
}
public void add() {
final JPanel content = new JPanel();
JPanel tab = new JPanel();
tab.setOpaque(false);
JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
JButton tabCloseButton = new JButton(closeImage);
tabCloseButton.setPreferredSize(closeButtonSize);
tabCloseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int closeTabNumber = tabbedPane.indexOfComponent(content);
tabbedPane.removeTabAt(closeTabNumber);
}
});
tab.add(tabLabel, BorderLayout.WEST);
tab.add(tabCloseButton, BorderLayout.EAST);
this.addTab(null, content);
this.setTabComponentAt(this.getTabCount() - 1, tab);
}
public static void main(String[] args) {
TabbedPaneSample main = new TabbedPaneSample();
main.add();
main.add();
}
}
Here's one approach, shown below. A more flexible approach using Action is referenced here.
Addendum: Reviewing your code, you should let the various layout managers and component preferred sizes do more of the work, as shown. In particular, this.setPreferredSize() is done for demonstration purposes. In a real application, you would restore user size and location preferences.
package overflow;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
/** #see https://stackoverflow.com/posts/6514889 */
public class InternalFrame extends JFrame {
JButton openButton;
JLayeredPane desktop;
JInternalFrame internalFrame;
public InternalFrame() {
super("Click button to open internal frame with two tabs.");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(400, 400));
openButton = new JButton("Open");
JPanel p = new JPanel();
p.add(openButton);
this.add(p, BorderLayout.SOUTH);
openButton.addActionListener(new OpenListener());
desktop = new JDesktopPane();
this.add(desktop, BorderLayout.CENTER);
this.pack();
this.setLocationRelativeTo(null);
}
class OpenListener implements ActionListener {
private static final int DELTA = 40;
private int offset = DELTA;
public void actionPerformed(ActionEvent e) {
internalFrame = new JInternalFrame(
"Internal Frame", true, true, true, true);
internalFrame.setLocation(offset, offset);
offset += DELTA;
internalFrame.add(createTabbedPane());
desktop.add(internalFrame);
internalFrame.pack();
internalFrame.setVisible(true);
}
}
private JTabbedPane createTabbedPane() {
JTabbedPane jtp = new JTabbedPane();
createTab(jtp, "One");
createTab(jtp, "Two");
return jtp;
}
private void createTab(JTabbedPane jtp, String s) {
jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
InternalFrame myInternalFrame = new InternalFrame();
myInternalFrame.setVisible(true);
}
});
}
}
First of all, I think Finally, I think you shouldn't use desktop.add(internalFrame, new Integer(1)) but rather desktop.add(internalFrame) instead, the reason is that JDesktopPane uses its layers (it is a JLayeredPane subclass) internally, and I don't think you should play with layers yourself.
Then, following this problem I had once with JInternalFrame, I would advise you call pack() after adding the internal frame to the desktop pane.
Hence, you should try with your OpenListener class looking like this:
class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((internalFrame == null) || (internalFrame.isClosed())) {
internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
internalFrame.setBounds(50, 50, 200, 100);
internalFrame.add(myTabbedPaneSample, BorderLayout.CENTER);
internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// internalFrame.pack();
internalFrame.setMinimumSize(new Dimension(300, 300));
// desktop.add(internalFrame, new Integer(1));
desktop.add(internalFrame);
internalFrame.pack();
internalFrame.setVisible(true);
}
}
}
Besides, I also agree with trashgod comments on Action of course and the simplifying rework he has done on your snippet.
I preferred to create in my Main Frame class (which extends JFrame) the following function:
private void showIntFrame(Class intFrameClass) {
JInternalFrame targetFrame = null;
int xoff = 0, yoff = 0;
for(JInternalFrame jif : jdp.getAllFrames()) {
if(jif.getClass().equals(intFrameClass))
targetFrame = jif;
if(jif.getLocation().x > xoff)
xoff = jif.getLocation().x;
if(jif.getLocation().y > yoff)
yoff = jif.getLocation().y;
}
if(targetFrame == null) {
try {
Constructor<JInternalFrame> c = intFrameClass.getConstructor(MainFrame.class);
targetFrame = c.newInstance(MainFrame.this);
} catch (Exception ex) {
System.err.println("Exception in MainFrame.showIntFrame() while creating new JInternalFrame instance. " + ex.getLocalizedMessage());
ex.printStackTrace();
return;
}
jdp.add(targetFrame);
targetFrame.setLocation(xoff + 30, yoff + 30);
}
targetFrame.setVisible(true);
try {
targetFrame.setSelected(true);
} catch (PropertyVetoException ex) {
System.err.println("PropertyVetoException in MainFrame.showIntFrame() while activating JInternalFrame instance. " + ex.getLocalizedMessage());
}
}
Here jdp is instance of JDesktopPane, which previously was set as ContentPane of my main JFrame.
Because my programs often contain numbers of different classes, inherited from JInternalFrame, it is easier to call this function from event handlers to show new subclass of JInternalFrame.
Every subclass of JInternalFrame in my programs have one constructor with one parameter - MainFrame (main JFrame).

java Swing Application Freezes when call to jpcapCaptor.openDevice method

I am developing some bandwidth utilization monitor tool using jpcap.
what am i doing:-
1. i created a radio buttons panel containing the list of all the network interfaces that is present on system, and user has to choose one of them.
2. a jfreechart panel (dynamic) that will show the real time graph of bandwidth utilization when user clicks GO! button.
problem:-
i have added following in ActionListener in GO! button
try{captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);}catch(Exception e){}
timer.start();
captor.loopPacket(-1,new PacketPrinter());
so when i run the program GUI comes with radiobutton panel and jfreechart panel but when i select an option and press GO! application freezes and chart panel does not show any dynamic updation.
when i commented out the JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);
then when i press GO! button, everything works,for example timer starts and chart panel is being updated.(but as captor is null so it is not capturing any data)
Please Help me!!!
my system is ubuntu 10.04, jpcap 0.7
my code is as follows:-
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import java.util.List;
import java.util.ArrayList;
import jpcap.*;
import jpcap.packet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
public class Sniffer extends JPanel
{
private static jpcap.NetworkInterface[] devices;
private static int selecteddevice=-1;
JFrame frame;
public static JpcapCaptor captor;
private static final String TITLE="Bandwidth Utilization Meter";;
public static List<Packet> packets;
public static Timer timer;
public static ArrayList<JRadioButton> radioButtonArray = new ArrayList<JRadioButton>();
private ButtonGroup group= new ButtonGroup();
public static JButton go;
JFreeChart chart;
static DynamicTimeSeriesCollection dataset;
public Sniffer()
{
packets = new ArrayList<Packet>();
dataset =new DynamicTimeSeriesCollection(1,120, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 2, 1, 2011));
dataset.addSeries(new float[0], 0, "PPP0 Bandwidth Utilization Meter");
chart = createChart(dataset);
getDevices();
timer = new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e)
{ long tlen=0;
List<Packet> temp = new ArrayList<Packet>(packets);
packets.clear();
for(Packet i : temp)
{
tlen+=i.len;
}
float[] newData = new float[1];
newData[0]=(float)tlen/1024;
dataset.advanceTime();
dataset.appendData(newData);
}});
setGUI();
}
void setGUI()
{
setLayout(new BorderLayout());
frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(devices.length, 1));
for (JRadioButton combo : radioButtonArray)
{
panel.add(combo);
}
JScrollPane scrollPane = new JScrollPane(panel);
go= new JButton("GO!");
go.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
//problem starts here.............
try{captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);}catch(Exception e){}
timer.start();
captor.loopPacket(-1,new PacketPrinter());
//.....................................
}
}
);
go.setEnabled(false);
panel.add(go);
add(scrollPane, BorderLayout.CENTER);
scrollPane.setSize(300,300);
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setLayout(new GridLayout(2, 0));
frame.add(scrollPane);
frame.add(new ChartPanel(chart));
frame.setSize(1024, 768);
frame.setTitle("BW");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void getDevices()
{
devices = JpcapCaptor.getDeviceList();
for(int i=0;i<devices.length;i++)
{
String device=null;
radioButtonArray.add(new JRadioButton());
group.add(radioButtonArray.get(i));
radioButtonArray.get(i).addActionListener(new RadioButtonListener());
device= devices[i].name+" "+"("+devices[i].description+")";
radioButtonArray.get(i).setText(device);
}
}
public static void startSniffing() throws Exception
{
captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);
}
public static void setSelectedDevice(int device)
{
selecteddevice = device;
}
public NetworkInterface getSelectedDevice()
{
if (selecteddevice == -1)
{
return null;
}
else
{
return devices[selecteddevice];
}
}
public static void main(String args[])
{
Sniffer sniffer = new Sniffer();
//sniffer.start();
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart xyz = ChartFactory.createTimeSeriesChart(
TITLE, "Time(Seconds)", "Bandwidth KB/s", dataset, true, true, false);
final XYPlot plot = xyz.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
range.setRange(0,1000);
return xyz;
}
}
class RadioButtonListener extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent e) {
Sniffer.go.setEnabled(true);
for (JRadioButton radio : Sniffer.radioButtonArray) {
if (radio.isSelected()) {
Sniffer.setSelectedDevice(Sniffer.radioButtonArray.indexOf(radio));
}
}
}
}
class PacketPrinter implements PacketReceiver {
static long tlen;
public void receivePacket(Packet packet) {
Sniffer.packets.add(packet);
}
}
Don't block the EDT. Put the time consuming task in a SwingWorker.

Categories

Resources