Category Archives: Swing

Swing

How to find your Google Plus ID

This is so simple

1. Go to your Google + account (https://plus.google.com/).

2. Click on the Profile icon on the Left.

3. If you look at the URL in the address bar, it should look something like this:

https://plus.google.com/104653270154306099169/posts

4. The long numerical string in the URL is your Google+ ID. Here is CoderzHeaven’s from the URL above:

104653270154306099169/

Google + CoderzHeaven

Creating a JButton component in swing

Java swing provides a native look and feel that emulates the look and feel of several platforms.
Here i am going to create a Button and adding it to Frame.

For this first create a Jframe object,a JPanel object and a Container object

JFrame f = new JFrame();
JPanel panel1 = new JPanel();
Container con = f.getContentPane();

Then Create a button object and add this ti JPanel object

JButton panel1_but = new JButton();
panel1.add(panel1_but);

and finally add JPanel to JFrame

panel1.add(panel1_but);

This full code is given below

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class mainframe implements WindowListener
{
	JFrame f = new JFrame();
	Container con = f.getContentPane();
	JPanel panel1 = new JPanel();
	mainframe()
	{
		createpanel1();
		f.addWindowListener(this);
		f.setSize(900, 600);
		f.setVisible(true);
		f.setLocationRelativeTo(null);
		f.setResizable(false);    ///cannot maximize
	   	f.setVisible(true);
	}
	public static void main(String args[])
	{
		new mainframe();
	}
	private void createpanel1()
	{
		JButton panel1_but = new JButton();
		panel1_but.setBounds(new Rectangle(450,400,200,40));
		panel1_but.setText("Continue");

		panel1.add(panel1_but);
		panel1_but.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				//action to be performed....
			}
			}
		);
		panel1.setLayout(new BorderLayout());
		panel1.setBackground(Color.white);
		panel1.setVisible(true);
		con.add(panel1);
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}
}

JComboBox in Java swing

This shows you how to add JcomboBox to JPanel

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Example implements WindowListener
{
  JFrame content_frame = new JFrame();
  JPanel panel = new JPanel();
  JComboBox Combonote;
  public Example()
  {
  	    content_frame.setTitle("Contents");
		content_frame.addWindowListener(this);
		content_frame.setSize(300, 300);
		content_frame.setVisible(true);
		content_frame.setLocationRelativeTo(null);
		content_frame.setResizable(false);    ///cannot maximize
		content_frame.setVisible(true);

		showComboBox();
  }
  private void showComboBox()
  {
	  String test[]= {"[select Exercise...]","Exercise1","Exercise2","Exercise3","Exercise4","Exercise5"};
	  panel.setBackground(Color.lightGray);
	  panel.setVisible(true);
	  Combonote = new JComboBox(test);
	  Combonote.setBounds(250, 240, 250, 30);
	  panel.add(Combonote);

	  panel.setBounds(250, 240, 250, 30);
	  content_frame.getContentPane().add(panel);
	  Combonote .addItemListener(new ItemListener()
	  {
	  	  public void itemStateChanged(ItemEvent ie)
	      {
	      	  if(ie.getStateChange() == ie.SELECTED)
	      	  {
	      		    if(Combonote.getSelectedItem().equals("Exercise1"))
	  		  		{
	                                          //code...
	                }
	          }
	      }
	  });
  }

  public static void main(String[] args)
  {
    new Example();
  }

@Override
public void windowActivated(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowClosed(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowClosing(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowDeactivated(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowOpened(WindowEvent arg0) {
	// TODO Auto-generated method stub

}
}


Swing JPanel with background image

This code add an image to Jpanel.

JPanel panel1 = new JPanel();
JFrame recursion_frame = new JFrame();

BackgroundPanel stack_image1 = new BackgroundPanel("Recursion.jpg");

panel1.setLayout(null);
panel1.setLayout(new BorderLayout());
panel1.setBackground(Color.lightGray);
panel1.setVisible(true);
recursion_frame.setContentPane(panel1);  ///adding to panel...
recursion_frame.getContentPane().add(stack_image1);  //adding image to panel...

Position swing window in center of the screen

In some case we need to position the window in the center of the screen . The same effect can be achieved by hard coding the center points .But in that case the window position change when the screen resolution changes

This code will position the window in center irrespective of different screen resolutions

public JFrame stack_frame = new JFrame();
stack_frame.setLocationRelativeTo(null);

Creating a JMenubar in java swing

Creating a JMenubar is simple.

        JMenuBar  menubar = new JMenuBar();
	JMenu menu1 = new JMenu("File");
	JMenu menu2= new JMenu("Help");
	file = new JMenuItem("Exit to menu");
	edit = new JMenuItem("Exit");

	JMenuItem about = new JMenuItem("About us");
	f.setJMenuBar(menubar);
	menu1.add(file);
	menu1.add(edit);
	menu2.add(about);
	menubar.add(menu1);
	menubar.add(menu2);

	file.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action......
		}
	});
	edit.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action.....
		}
	});
	about.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action....
		}
	});