Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


k murali
hi
2 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners


how to create a table by using jtable jt=new jtable(5,5) with column names,if i am using this table type table(rowno,colno) how i am set the colum names for this table.
Thank u

Advertisement
View Answers

February 24, 2010 at 11:05 AM




Here is the Sample Code.

String[] columnNames = {"A","B","C","D","E", "F", "G"};
Object[][] data = new Object[5][7];
JTable jTable = new JTable();
jTable.setVisible(true);
jTable.setAutoCreateRowSorter(true);
jTable.setModel(new DefaultTableModel(data, columnNames));
add(jTable);

JScrollPane scrollPane = new JScrollPane(jTable);
jTable.setFillsViewportHeight(true);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jTable.getTableHeader(), BorderLayout.PAGE_START);
this.getContentPane().add(jTable, BorderLayout.CENTER);












---- Complete code --------------------------------------
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

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

/*
* NewJFrame.java
*
* Created on Feb 24, 2010, 10:37:20 AM
*/
/**
*
* @author samith
*/
public class NewJFrame extends javax.swing.JFrame {

private JScrollPane jScrollPane1;

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();


String[] columnNames = {"A","B","C","D","E", "F", "G"};
Object[][] data = new Object[5][7];
JTable jTable = new JTable();
jTable.setVisible(true);
jTable.setAutoCreateRowSorter(true);
jTable.setModel(new DefaultTableModel(data, columnNames));
add(jTable);

JScrollPane scrollPane = new JScrollPane(jTable);
jTable.setFillsViewportHeight(true);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jTable.getTableHeader(), BorderLayout.PAGE_START);
this.getContentPane().add(jTable, BorderLayout.CENTER);



}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(576, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(467, Short.MAX_VALUE)
.addComponent(jButton1))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
--------------------------------------------------------------------
For more visit :-
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Best Regards.


February 24, 2010 at 12:06 PM


Hi Friend,

Try the following code:

import java.awt.*;
import javax.swing.*;

public class SimpleTable extends JFrame {

public SimpleTable() {
super("Simple JTable Test");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);

JTable table = new JTable(5,5);
JScrollPane jsp = new JScrollPane(table);
String st[]={"Id","Name","Address","ContactNo","Email"};
for(int i=0;i<st.length;i++){
table.getColumnModel().getColumn(i).setHeaderValue(st[i]);
}
getContentPane().add(jsp, BorderLayout.CENTER);
}

public static void main(String args[]) {
SimpleTable st = new SimpleTable();
st.setVisible(true);
}
}

Thanks


Related Tutorials/Questions & Answers:
Hi,
Hi,  Hi,what is the purpose of hash table
Hi..
Hi..   what are the keywords available in simple HelloWorld program
Advertisements
Hi..
Hi..  what are access specifier available in java
Hi,
Hi,   labels = new Hashtable<Integer,JLabel>
HI.
HI.  hi,plz send me the code for me using search button bind the data from data base in dropdownlist
Hi..
Hi..  null is a keyword.True/False?   hi friend, In Java true, false and null are not a Java keyword they are literals in Java. For reading in detail go through the following link The null keyword
hi....
hi....  plzz sent me d code for counting vowels in a string... gui programme
Hi
Hi  Hi How to implement I18N concept in struts 1.3? Please reply to me
Hi
Hi  Hi this is really good example to beginners who is learning struts2.0 thanks
hi,
hi,  print("code sample");how to display all elements in 2d array usin any one loop
Hi
Hi   how to read collection obj from jsp to servlet and from jsp - jsp?   Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/servlet-jsp-data-list.shtml Thanks
hi....
hi....  a programme which displays a combobox which contains num from 1 to 10 and calculates the factorial of dt num which user clicks
Hi
Hi  I want import txt fayl java.please say me...   Hi, Please clarify your problem! Thanks
hi
hi   I have connected mysql with jsp in linux and i have used JDBC connectivity but when i run the program, its not working the program is displaying
hi
hi   'print("code sample");`class example(){ public.static.void.main(); system.out.println("this is first pro");}   class example{ public static void main(String[]args){ System.out.println("this is first pro
Hi..
Hi..  diff between syntax and signature?   signature is a particular identity of a method in terms of its argument order ,type and their number e.g. void A(arguments) then here the order ,type and number of arguments
Hi
Hi  Hi All, I am new to roseindia. I want to learn struts. I do not know anything in struts. What exactly is struts and where do we use it. Please help me. Thanks in advance. Regards, Deepak
hi!
hi!  how can i write aprogram in java by using scanner when asking user to enter element user will be free to enter any type that he/she wants to to enter, like(int,double,float,String,....) thanx for answering....   Hi
Hi..
Hi..  what are the steps mandatory to develop a simple java program?   To develop a Java program following steps must be followed by a Java developer : First of all the JDK (Java Development Kit) must be available
hi
hi   on clicking on button on 1st jsp page i want to display following options like friends,family,doctor etc.on next jsp page and also i want link to those options through which i can save the data from 1st jsp in different
hi
hi   on clicking on button on 1st jsp page i want to display following options like friends,family,doctor etc.on next jsp page and also i want link to those options through which i can save the data from 1st jsp in different
hi
online multiple choice examination  hi i am developing online multiple choice examination for that i want to store questions,four options,correct answer in a xml file using jsp or java?can any one help me?   Please
hi
hi  what is the code for printing stars as follows * * * * * * * * * *print("code sample");   Hi Friend, Try... num=4; int p = num; int q = 0; for (int i = 0; i <= num; i++) { for (int j
hi
hi  int A[]=new int{2,5,4,1,3}; what is the error in declaration of this   Hi Friend, This is the wrong way of declaring an array. Array is always declared in the following ways: 1)int arr[]=new int[5]; 2)int arr
Hi
Hi   I need some help I've got my java code and am having difficulty to spot what errors there are is someone able to help import java.util.Scanner; public class Post { public static void main(String[] args) { Scanner sc
hi.......
hi.......  i've a project on railway reservation... i need to connect netbeans and mysql with the help of jdbc driver... then i need to design... for such a programme... plz help me...   Hi Friend, Try this: import java.awt.
hi!
hi!  public NewJFrame() { initComponents(); try { Class.forName("java.sql.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin"); } catch(Exception e
Hi
Hi   I have got this code but am not totally understanding what the errors. Could someone Please help. Thanks in advance! import java.util.Random; import java.util.Scanner; private static int nextInt() { public class
hi
storing data in xml file using jsp  hi i am storing data in xml file using jsp.in this when i enter data into xml file i am getting xml declaration for each specified data?can any one help me to prevent this?   <
hi
hi   i want to develop a online bit by bit examination process as part of my project in this i am stuck at how to store multiple choice questions options and correct option for the question.this is the first project i am doing
hi..
hi..  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page contentType="text/html;charset=windows-1252"%> <html> <head> <meta http-equiv
 

 

 

DMCA.com