Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-13-2008, 10:31 AM
Member
 
Join Date: Feb 2008
Posts: 1
cagalli83 is on a distinguished road
Can't make JTable work -- please help!!
Hi, I'm relatively new to Java and here's what i want to do, i want to be able to insert what i type in the text field into the JTable. Just a very basic function. But i'm having problems with JTable. I used the Netbeans IDE GUI editor so there are some protected codes that i cannot alter. If i code the interface manually, the Jtable works fine. However, if i add JTable implementation to the GUI i created, it doesn't work. Thing is, this is just a part of a bigger application that already has the GUI designed nicely via Netbeans GUI editor. Here, i'm just trying to get the column names to show but they won't. I've already read the sun tutorials and the how to use JTables tutorial but as i've said, when i incorporate it in the codes generated by Netbeans UI editor, it won't work. When i do add the components to a frame manually, it works. My codes are as follows:



/*
* tryTable.java
*
* Created on February 11, 2008, 5:51 PM
*/

package trial_codes;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import javax.swing.event.*;

/**
*
* @author LIMDE
*/
public class tryTable extends javax.swing.JFrame {

Vector rows,columns;
DefaultTableModel tabModel;

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

rows = new Vector();
columns = new Vector();
String[] columnNames =
{
"ID",
"Name",
"Class",
"SubClass",
"Rate",
"Quantity",
"Amount"
};
addColumns(columnNames);

tabModel = new DefaultTableModel();
tabModel.setDataVector(rows, columns);

tbl1.setModel(tabModel);
//jScrollPane1 = new JScrollPane(tbl1);
tbl1.setVisible(true);
//tabModel.addRow(rows);




}

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);
tbl1.addNotify();

}

public Vector createBlankElement() {
Vector t = new Vector();
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
return t;
}





/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

txtFld1 = new javax.swing.JTextField();
txtFld2 = new javax.swing.JTextField();
btnAdd = new javax.swing.JButton();
pnlTbl = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
tbl1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

btnAdd.setText("ADD");

tbl1.setAutoCreateColumnsFromModel(false);
jScrollPane1.setViewportView(tbl1);

javax.swing.GroupLayout pnlTblLayout = new javax.swing.GroupLayout(pnlTbl);
pnlTbl.setLayout(pnlTblLayout);
pnlTblLayout.setHorizontalGroup(
pnlTblLayout.createParallelGroup(javax.swing.Group Layout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pnlTblLayout.setVerticalGroup(
pnlTblLayout.createParallelGroup(javax.swing.Group Layout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(pnlTbl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(txtFld1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(txtFld2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnAdd)))
.addContainerGap(51, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(txtFld1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtFld2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAdd))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(pnlTbl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);

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

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new tryTable().setVisible(true);
//tryTable t1 = new tryTable();
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel pnlTbl;
private javax.swing.JTable tbl1;
private javax.swing.JTextField txtFld1;
private javax.swing.JTextField txtFld2;
// End of variables declaration

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Will this applet ever work? willemjav Java Applets 4 04-20-2008 06:40 PM
Pass by ref. A work around? diRisig New To Java 0 02-05-2008 08:25 PM
how would i get this to work...? deeadeed New To Java 6 12-06-2007 03:58 AM
My own ClassLoader didn't work. snooze-g Advanced Java 1 07-17-2007 12:12 PM
To make work DLL from webservices Felissa Advanced Java 1 07-05-2007 08:07 PM


All times are GMT +3. The time now is 01:00 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org