Welcome to Dream.In.Code
Become a Java Expert!

Join 150,154 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,366 people online right now. Registration is fast and FREE... Join Now!




Creating a JTable

 
Reply to this topicStart new topic

Creating a JTable

nick2price
5 Aug, 2008 - 03:59 PM
Post #1

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
I have the following piece of code

CODE
import java.awt.*; //the importing of required libraries
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

public class UpdateEventMain extends JDialog
{

private JPanel contentPane; //declaring variables

private JLabel titleMain;
private JPanel pnlTop;

private JPanel pnlCenter;

private JPanel  pnlBottom;
private JButton btnUpdate;
private JButton btnExit;


public UpdateEventMain() {

super();  
initializeComponent();  
}  

private void initializeComponent() {

contentPane = (JPanel)this.getContentPane();

pnlTop = new JPanel(); //declaring JPanels
pnlCenter = new JPanel();
pnlBottom = new JPanel();

btnUpdate = new JButton();
btnExit = new JButton();

titleMain = new JLabel("UPDATE EVENTS"); //setting text, font and colour
titleMain.setFont(titleMain.getFont().deriveFont(Font.BOLD, 48));
titleMain.setForeground(Color.black);

contentPane.setLayout(new BorderLayout(5, 5));  //adding my JPanels to content pane using a border layout
contentPane.add(pnlTop, BorderLayout.NORTH);
contentPane.add(pnlCenter, BorderLayout.CENTER);
contentPane.add(pnlBottom, BorderLayout.SOUTH);
contentPane.setBackground(new Color(0, 0, 0));

pnlTop.setLayout(new FlowLayout());  //setting my JPanels with a flow layout
pnlTop.add(titleMain, 0);  //adding a variable

btnUpdate.setText("    UPDATE RECORDS    ");
btnUpdate.addActionListener(new ActionListener() { //adding an action listener to the button
public void actionPerformed(ActionEvent e)
{
btnUpdate_actionPerformed(); //calling up the buttons action event
}

});

btnExit.setText(" EXIT ");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
btnExit_actionPerformed();
}

});

pnlBottom.setLayout(new FlowLayout(FlowLayout.CENTER, 6, 5));
pnlBottom.add(btnUpdate, 0);
pnlBottom.add(btnExit, 1);


setSize(800, 608);
setTitle("Update");
setResizable(false); //so users cant resize the JDialog
setVisible(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); //action if dialog is closed

}

private void btnUpdate_actionPerformed()
{

}

private void btnExit_actionPerformed()
{
this.dispose(); //exit
}


public static void main(String[] args) {  
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UpdateEventMain();  //run this class and make it visible
}
});
}
}




Now too the center panel i would like to try and add a JTabel, but i am a bit lost of how to do it. The sun tutorials seem to provide examples of JTable with the data already set beforehand. I need for the user to enter the data themselves. Examples i can find of this seem very overcomplicated with a lot of features i dont need. So what i basically need is an empty table, with just the column names set. Does anyone have any examples or advice on how i could do this.
cheers
User is offlineProfile CardPM
+Quote Post

pbl
RE: Creating A JTable
5 Aug, 2008 - 04:56 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
OK, the shortest I could make it

CODE

import java.util.Vector;
import javax.swing.*;

public class Table {

       public static void main(String[] arg) {
           // header for the constructor
           String[] header = {"Col1", "Col2", "Col3"};
           // put in a vector
           Vector<String> vHead = new Vector<String>(header.length);
           // fil the vector
           for(int i = 0; i < header.length; i++)
               vHead.add(header[i]);
           // an empty vector of vectors of objects
           Vector<Vector<Object>> vData = new Vector<Vector<Object>>(100,25);    
          
           // create JTable and display it
           JTable t = new JTable(vData, vHead);
           JFrame f = new JFrame("Test");
           f.add(new JScrollPane(t));
           f.setSize(200,100);
           f.setVisible(true);
           f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          
           // to add a row:
           // create a vector of Object of header.length
           Vector<Object> row = new Vector<Object>(header.length);
           // fill it with String object
           for(int i = 0; i < header.length; i++) {
               String str = "Line1 " + i;
               row.add(str);
           }
           // addthis vector to our vector of rows
           vData.add(row);    
       }
}

User is offlineProfile CardPM
+Quote Post

nick2price
RE: Creating A JTable
5 Aug, 2008 - 05:01 PM
Post #3

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Sorry, really brain dead today, suppossed its to late. How would i add this to my pnlCenter in my original post. Where would i put all that code?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Creating A JTable
5 Aug, 2008 - 07:42 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Don't even know in which panel you want to put your JTable

Things to remember:

- you have to create a Vector<String> (iVectors are almost like an ArrayList) of your headers
- you have to create a Vector of Vectors (it can be empty) of the objects to be displayed in the JTable
- now you can create your JTable(emptyVector of vectors, the Vector of headers)
- you have to put your JTable in a JScrollPane then add the JScrollPane to your JPanel
- now to add a row: create a Vector<Object> and add to it all the elements of your row (the elements must imprement the toString() method)
- add that Vector to the Vector of Vector
- to delete a row delete the row from the Vector of Vectors

your JTable will be updated autoimatically

User is offlineProfile CardPM
+Quote Post

nick2price
RE: Creating A JTable
6 Aug, 2008 - 05:50 AM
Post #5

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
This is what i have attempted:
CODE
import java.awt.*; //the importing of required libraries
import java.io.*;
import java.awt.event.KeyListener;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.event.*;

public class UpdateEventMain extends JDialog
{
            
        private JPanel contentPane; //declaring variables
        
        private JLabel titleMain;
         private JPanel pnlTop;
         
        private JPanel pnlCenter;
        
         private JPanel  pnlBottom;
         private JButton btnUpdate;
         private JButton btnExit;
        
        JTable table;
        Vector rows,columns;
        DefaultTableModel tabModel;
        JScrollPane scrollPane;

    
    public UpdateEventMain() {
    
        super();  
        initializeComponent();  
    }  
    
    private void initializeComponent() {

    contentPane = (JPanel)this.getContentPane();
        
    pnlTop = new JPanel(); //declaring JPanels
    pnlCenter = new JPanel();
    pnlBottom = new JPanel();
    
    btnUpdate = new JButton();
    btnExit = new JButton();
    
    titleMain = new JLabel("UPDATE EVENTS"); //setting text, font and colour
    titleMain.setFont(titleMain.getFont().deriveFont(Font.BOLD, 48));
    titleMain.setForeground(Color.black);
    
    contentPane.setLayout(new BorderLayout(5, 5));  //adding my JPanels to content pane using a border layout
    contentPane.add(pnlTop, BorderLayout.NORTH);
    contentPane.add(pnlCenter, BorderLayout.CENTER);
    contentPane.add(pnlBottom, BorderLayout.SOUTH);
    contentPane.setBackground(new Color(0, 0, 0));
    
    pnlTop.setLayout(new FlowLayout());  //setting my JPanels with a flow layout
    pnlTop.add(titleMain, 0);  //adding a variable
    
    rows= new Vector();
    columns= new Vector();
    String[] columnNames =
    {
    "Position",
    "First Name",
    "Last Name",
    "Event",
    "Time",
    };
    addColumns(columnNames);

    tabModel=new DefaultTableModel();
    tabModel.setDataVector(rows,columns);
    
    table = new JTable(tabModel);
    scrollPane= new JScrollPane(table);//ScrollPane
    
    table.setRowSelectionAllowed(false);
    
    //table.getModel().addTableModelListener(this);
    pnlCenter.add(scrollPane);

    
    btnUpdate.setText("    UPDATE RECORDS    ");
    btnUpdate.addActionListener(new ActionListener() { //adding an action listener to the button
        public void actionPerformed(ActionEvent e)
        {
            btnUpdate_actionPerformed(); //calling up the buttons action event
        }

    });

    btnExit.setText(" EXIT ");
    btnExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            btnExit_actionPerformed();
        }

    });
        
    pnlBottom.setLayout(new FlowLayout(FlowLayout.CENTER, 6, 5));
    pnlBottom.add(btnUpdate, 0);
    pnlBottom.add(btnExit, 1);
    
    
    setSize(800, 608);
    setTitle("Update");
    setResizable(false); //so users cant resize the JDialog
    setVisible(true);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); //action if dialog is closed

    }
    
    private void btnUpdate_actionPerformed()
    {
    
    }
    
    private void btnExit_actionPerformed()
    {
    this.dispose(); //exit
    }
    

    public void addColumns(String[] colName)//Table Columns
    {
    for(int i=0;i<colName.length;i++)
    columns.addElement((String) colName[i]);
    }
    
    public void addRow() //Add Row
    {
    Vector r=new Vector();
    r=createBlankElement();
    rows.addElement(r);
    table.addNotify();
    
    }
    
    public Vector createBlankElement()
    {
    Vector t = new Vector();
    t.addElement((String) " ");
    t.addElement((String) " ");
    t.addElement((String) " ");
    t.addElement((String) " ");
    t.addElement((String) " ");
    return t;
    }    
            
    public static void main(String[] args) {  
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new UpdateEventMain();  //run this class and make it visible
        }
    });
}
}


I am getting a table in the correct panel, but there are no cells in the table. I am a bit lost now.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Creating A JTable
6 Aug, 2008 - 05:00 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
CODE

    String[] columnNames =
    {
    "Position",
    "First Name",
    "Last Name",
    "Event",
    "Time",
    };
    addColumns(columnNames);


should be

CODE

[code]
    String[] columnNames =
    {
    "Position",
    "First Name",
    "Last Name",
    "Event",
    "Time",
    };

    for(int i = 0; i < columnNames.length; i++)
       colums.add(columnNames[i];

addColumns(columnNames);



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:37AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month