Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Swing is faster than windows forms.

843806Dec 28 2007 — edited Dec 28 2007
I always see people writing that swing are slow swing are heavy. I always disagreed i love swing i think its the most flexible and well designed gui api out there. So i decided to do a little experiment i took the swing and the windows form(which is supposed to be faster and lighter cause of the native thing) and created a program that puts on a grid numbers from 1 to 500000. Here is the code for java :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class GridFrame extends JPanel {

	private JScrollPane scrollPane;
	private JTable jTable;
	private JButton jButton;
	private JToolBar jToolBar;

	public GridFrame() {
		setLayout(new BorderLayout());
		add(getScrollPane(), BorderLayout.CENTER);
		add(getJToolBar(), BorderLayout.NORTH);
	}

	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getJTable());
		}
		return scrollPane;
	}

	public JToolBar getJToolBar() {
		if (jToolBar == null) {
			jToolBar = new JToolBar();
			jToolBar.add(getJButton());
			jToolBar.setFloatable(false);
		}
		return jToolBar;
	}

	public JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
		}
		return jTable;
	}

	public JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton("Start");
			jButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					getJTable().setModel(new AutoNumberTableModel(500000));
					getJTable().getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
				}

			});
		}
		return jButton;
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				JFrame frame = new JFrame("Grid Test");
				frame.setContentPane(new GridFrame());
				frame.setSize(1024, 768);
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}

		});
	}

	private class AutoNumberTableModel extends DefaultTableModel {

		public AutoNumberTableModel(int autoNum) {
			Vector<String> header = new Vector<String>();
			Vector<String> singleValue = new Vector<String>();
			Vector<Vector<String>> values = new Vector<Vector<String>>();
			header.add("AutoNumber");
			for (int i = 0; i < autoNum; i++) {
				singleValue = new Vector<String>();
				singleValue.add(String.valueOf(i));
				values.add(singleValue);
			}
			setDataVector(values, header);
		}
	}

	private class CellRenderer implements TableCellRenderer {

		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			JLabel label = new JLabel(value.toString());
			label.setOpaque(true);

			if ((row % 2) == 0) {
				label.setBackground(Color.lightGray);
			}

			if (isSelected) {
				Font font = new Font(label.getFont().getFamily(), Font.BOLD, 12);
				label.setFont(font);
				label.setBackground(Color.GRAY);
				label.setForeground(Color.WHITE);
				label.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.BLACK), BorderFactory.createRaisedBevelBorder()));
			}

			return label;
		}
	}

}
And here is the code for c# :
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
    class GridTest : Form
    {
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.DataGridView dataGridView1;

        public GridTest()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.dataGridView1 = new DataGridView();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Start";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(12, 41);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(346, 495);
            this.dataGridView1.TabIndex = 2;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(370, 548);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int max = 500000;
            DataTable dt = new DataTable();
            dt.Columns.Add("AutoNum");
            for (int i = 0; i < max; i++)
            {
                dt.Rows.Add(new Object[] { i });
            }
            dataGridView1.DataSource = dt;
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new GridTest());
        }
    }
}
Try run those examples and you'll see that java is much faster than c# in my pc java took 1 sec to load 500000 and occupied 93.168k and c# took 19 sec and occupied 127.380k for the same entries. To run java put the -Xmx256m option. To prove that swing have better design try doing this example for 5000000 for those entries c# took 10 minutes and occupied 755.236k and java took 11seconds and occupied 763.492
for the 5000000 put the -Xmx1024m option. As you can see java is really fast, the amount of memory is almost the same and it runs on all platforms. If you have done other benchmarks like this please post them.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 25 2008
Added on Dec 28 2007
3 comments
603 views