Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Example Java Swing SwingFrame

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

32 comments so far (post your own) View All Comments Latest 10 Comments:

package excercise;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
*
* @author test
*/
public class EmpInfo extends JFrame implements ActionListener {

JLabel name, id, salary,searchindex;
JTextField namefield, idfield, salaryfield,searchfield;
JRadioButton count, maxvalue, minvalue;
JButton add,sortbyname,sortbyID,sortBySalary;
Vector columnnames, datas,rowvec;
DefaultTableModel model;
JTable tabel;
JScrollPane js;
JPanel tabelpanel;
int index;

public EmpInfo() {
tabelpanel = new JPanel(new GridBagLayout());
index = 0;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(constructPanel());
setSize(10000, 10000);
pack();
setVisible(true);
}

private JPanel getInfoPanel() {
JPanel panel = new JPanel();
name = new JLabel("Name");
namefield = new JTextField(5);
id = new JLabel("ID");
idfield = new JTextField(5);
salary = new JLabel("Salary");
salaryfield = new JTextField(5);
panel.setLayout(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("Information Panel"));
int index = 0;
panel.add(name, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 0));
panel.add(namefield, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 0));
panel.add(id, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 0));
panel.add(idfield, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 0));
panel.add(salary, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 0));
panel.add(salaryfield, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 0));
return panel;
}

private JPanel getButtonPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("Action Panel"));
int index = 0;
add = new JButton("Add");
add.addActionListener(this);
sortbyname = new JButton("Sort By Name");
sortbyname.addActionListener(this);
sortbyID = new JButton("Sort By ID");
sortbyID.addActionListener(this);
sortBySalary = new JButton("Sort By Salary");
sortBySalary.addActionListener(this);
panel.add(add, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 1));
panel.add(sortbyname, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 1));
panel.add(sortbyID, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 1));
panel.add(sortBySalary, new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 1));
return panel;
}

private JScrollPane getTabelPanel() {
columnnames = new Vector();
datas = new Vector();
columnnames.add("S.No");
columnnames.add("Name");
columnnames.add("ID");
columnnames.add("salary");
model = new DefaultTableModel();
model.setColumnIdentifiers(columnnames);
tabel = new JTable(model);
//tabel.setPreferredSize(new Dimension(300, 300));
js = new JScrollPane(tabel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
js.setPreferredSize(new Dimension(300, 300));
js.setVisible(true);
js.setBorder(BorderFactory.createTitledBorder("Display Panel"));
/*tabelpanel.add(js, new GridBagConstraints(0, 0, 1, 1, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
tabelpanel.setBackground(Color.red);*/
return js;

}
public JPanel getSearchPanel(){
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
searchindex = new JLabel("Serach Index");
searchfield = new JTextField(10);
int index = 0;
panel.add(searchindex, new GridBagConstraints(0, 0, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 5, 2, 5), 0, 1));
panel.add(searchfield, new GridBagConstraints(0, 1, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 5, 2, 5), 0, 1));
return panel;
}

private JPanel constructPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
int index = 0;
panel.add(getInfoPanel(), new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 1));
panel.add(getSearchPanel(), new GridBagConstraints(index++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 1));
panel.add(getButtonPanel(), new GridBagConstraints(index, index++, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5), 0, 1));
panel.add(getTabelPanel(), new GridBagConstraints(0, index++, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 5, 2, 5), 0, 1));
return panel;
}

public static void main (String args[]){
EmpInfo in = new EmpInfo();
}
public void actionPerformed(ActionEvent e) {
String actioncmd = e.getActionCommand();
if (actioncmd.equalsIgnoreCase("Add")) {
String name = namefield.getText();
if (name == null) {
JOptionPane.showMessageDialog(this, "Enter the valid Employee name");
return;
} else if (name.equals("")) {
JOptionPane.showMessageDialog(this, "Enter the valid Employee name");
return;
}
AddEmployee();
}else if (actioncmd.equals("Sort By Name")){
Collections.sort(datas, new CustomNameComparator());
model.setDataVector(datas, columnnames);
TableColumnModel tabcolmod = tabel.getTableHeader().getColumnModel();
int colsize = tabcolmod.getColumnCount();
for (int index = 0; index < colsize; index++) {
TableColumn tableCol = tabcolmod.getColumn(index);
if(index==1){
tableCol.setMaxWidth(100);
tableCol.setMinWidth(100);
}else {
tableCol.setMaxWidth(50);
tableCol.setMinWidth(50);
}

}
tabel.invalidate();
tabel.revalidate();
tabel.repaint();
js.invalidate();
js.revalidate();
js.repaint();
tabelpanel.invalidate();
tabelpanel.revalidate();
tabelpanel.repaint();
}
}


private void AddEmployee() {
rowvec = new Vector();
rowvec.add(++index);
rowvec.add(namefield.getText());
rowvec.add(idfield.getText());
rowvec.add(salaryfield.getText());
if(datas!=null){
Collections.sort(datas, new CustomSNoComparator());
}
datas.add(rowvec);
model.setDataVector(datas, columnnames);
TableColumnModel tabcolmod = tabel.getTableHeader().getColumnModel();
int colsize = tabcolmod.getColumnCount();
for (int index = 0; index < colsize; index++) {
TableColumn tableCol = tabcolmod.getColumn(index);
if(index==1){
tableCol.setMaxWidth(100);
tableCol.setMinWidth(100);
}else {
tableCol.setMaxWidth(50);
tableCol.setMinWidth(50);
}

}
tabel.invalidate();
tabel.revalidate();
tabel.repaint();
js.invalidate();
js.revalidate();
js.repaint();
tabelpanel.invalidate();
tabelpanel.revalidate();
tabelpanel.repaint();
}

class HeaderThemeRenderer extends JLabel implements TableCellRenderer {

private Font thefont = null;

public HeaderThemeRenderer() {
setOpaque(true);
thefont = new Font("Times New Roman", Font.BOLD, 12);
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setHorizontalAlignment(JLabel.CENTER);
setText(value.toString().toUpperCase());
setBorder(new LineBorder(Color.gray));
//setBackground(new Color(214, 166, 233));
setBackground(new Color(218, 108, 45));
setFont(thefont);
return this;
}
}
class CustomNameComparator implements Comparator{
public int compare(Object o1, Object o2) {
String name1 = (String)((Vector)o1).get(1);
String name2 = (String)((Vector)o2).get(1);
return name1.compareTo(name2);
}
}

class CustomIdComparator implements Comparator{
public int compare(Object o1, Object o2) {
String name1 = (String)((Vector)o1).get(2);
String name2 = (String)((Vector)o2).get(2);
return name1.compareTo(name2);
}
}

class CustomSalaryComparator implements Comparator{
public int compare(Object o1, Object o2) {
String name1 = (String)((Vector)o1).get(3);
String name2 = (String)((Vector)o2).get(3);
return name1.compareTo(name2);
}
}
class CustomSNoComparator implements Comparator{
public int compare(Object o1, Object o2) {
Integer no1 = (Integer)((Vector)o1).get(0);
Integer no2 = (Integer)((Vector)o2).get(0);
return no1.compareTo(no2);
}
}


}

Posted by raja on Wednesday, 10.20.10 @ 07:53am | #100377

please explain me exat meaning of line:
public static void main(String args);

Posted by priyanka.chtrvd on Friday, 09.17.10 @ 17:58pm | #99656

Its very usefull for me.. Thanks to "RoseIndia"

Posted by SIVIS on Wednesday, 06.30.10 @ 16:56pm | #97866

gud work but you can do better

Posted by mardyella on Wednesday, 05.5.10 @ 16:16pm | #96836

how connect data base to java

Posted by pugal on Friday, 04.23.10 @ 15:07pm | #96581

this code is very easy to understand so please explain more information about frame application like this

Posted by priya v p on Friday, 04.16.10 @ 12:55pm | #96422

very good for java

Posted by antony on Tuesday, 03.30.10 @ 17:01pm | #95975

i want to learn java graphics programing very well

Posted by ziand on Friday, 12.18.09 @ 18:20pm | #93472

I need to become java programer
how can you help me?

Posted by Masengesho Donatien on Monday, 10.19.09 @ 16:57pm | #91898

I just started learning Swing and I found it very useful.

Posted by Sarje Barange on Saturday, 08.22.09 @ 12:39pm | #90218

this is good teacher of java begineer, because easly to understand companre than other....

Posted by rajkumar on Friday, 08.21.09 @ 16:09pm | #90203

I want to learn more about java is any one want to help me about my problem, on how to create a text box and command button in java..

thnx..a lot

Posted by hanna on Thursday, 07.30.09 @ 13:11pm | #89704

how to connect link for one form to another form in java

Posted by manasa on Wednesday, 05.20.09 @ 08:10am | #87841

why we are not extending JFrame class even if we r importing all java classes?

Posted by Avinash Kumar Nalam on Wednesday, 05.13.09 @ 10:47am | #87655

i want to learnhow to insert textbox into a frame

Posted by anil on Thursday, 04.9.09 @ 14:53pm | #86683

How to set Frame component Orientation and Change frame default color that is blue to orange... pls help me...

Posted by ahmad ashraf azman on Monday, 04.6.09 @ 16:19pm | #86552

how to connect jframes to the servlets like when i click a button a new window should open i.e. when i click on insert it should open a new window... how do i do this?

Posted by amit on Thursday, 01.8.09 @ 07:32am | #83618

hi i am new to swings...i have a few issues for which i need help... how do i divide a frame such that i can put 3 jbuttons whereever i wish....i need a screen something similar to this
jframe-"reader"
jbutton1 jbutton2

jbutton3

end of frame

Posted by pooja on Friday, 12.26.08 @ 03:50am | #83181

may i have a fvor to all user's and programmers.... that i badly needed a java installer for macintosh os...!

Posted by sansoa on Monday, 09.29.08 @ 13:08pm | #80768

how to connect one frame to other,for a project???

Posted by somya on Thursday, 06.26.08 @ 11:22am | #64645

1 ) how to declare the variable static and acess them.

2) how to send data from one frame to another.

Posted by sanjana on Wednesday, 04.30.08 @ 16:06pm | #58216

Regarding Has Map.

Posted by Ramesh on Saturday, 03.1.08 @ 14:59pm | #50843

i have created the GUI using netbeans.But when project is run only the main window is displayed and none of the components in it are shown.How to connect Main.java and NewJFrame1.java in netbeans?

Posted by ramya on Friday, 02.29.08 @ 23:30pm | #50762

how to divide a frame in swings if so provide me some examples as soon as possible and tell me the way to know the key words of swings

Posted by uma on Saturday, 02.16.08 @ 17:00pm | #48583

I m a fresher. i want to know how to read a pdf file. i want to create a text area in an applet
and print the whole content of the desired pdf file in that text area.I m able to read content from txt files(using inputStreamReader and all).
but how abt des pdf files?
plz help me.

Posted by ravi sankar on Thursday, 10.25.07 @ 15:46pm | #34745

I have created a main frame with 2 buttons, New and Close All. When New is clicked, a new frame is to be created titled Frame 1. Subsequent clicks of New will create additional frames, Frame 2, Frame 3, etc. When Close All is clicked in my main frame, all Frame n's are to close. I'm new to Java Swing and cannot seem to find an example on how to perform this in code? I see stuff on internal frames, but that is not what I am attempting to do. Can you help?

Posted by J. Beaumont on Wednesday, 05.30.07 @ 07:50am | #17703

How to use HashMap with Swings to retrieve data from database

Posted by Ashish Kumar on Friday, 04.20.07 @ 15:50pm | #14623

Hi,
P.Rajeswari!
I receive your problem for creating menus in the frame. Donít worry; Pease, click the following link and get the creation process and description to menus or submenus in the frame.

Create menus and submenus

Vinod kumar
(RoseIndia.net)

Posted by vinod kumar on Friday, 04.13.07 @ 18:02pm | #14108

i need more inforamtion to create menu in hte frames plz help me sir
regards
raji

Posted by P.Rajeswari on Friday, 03.30.07 @ 10:52am | #12970

Hi Anand,

You can get the solution of your problem on the following link. Please click at:

http://www.roseindia.net/java/example/java/swing/RemoveTitleFrame.shtml

Thanks
Chandan Kumar
Roseindia Team Member

Posted by Chandan Kumar on Sunday, 03.18.07 @ 18:18pm | #12111

How to create a freme with out title bar

Posted by Anand kumar on Sunday, 03.18.07 @ 11:25am | #12078

very good website

Posted by GURU on Monday, 03.12.07 @ 11:57am | #11459

Ask programming questions?

 

 

Back to Tutorial

 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.