Results 1 to 8 of 8
Thread: Column names
- 02-16-2012, 04:37 PM #1
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Column names
Hi,
In my last post I got very good help with a simple task, but this time I think it is somewhat more difficult task....gif)
I have a table that gets data from a MySQL table. Here is some of my code:
This code gets the data from MySQL very fine, but it uses the column names from MySQL. I would like to write custom Column names.Java Code:private void initComponents() { HeaderLabel = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); HeaderLabel.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N HeaderLabel.setText("Kontakter"); jTable1.setModel(new javax.swing.table.DefaultTableModel(vectordata.getData(),vectordata.getColumns())); jTable1.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { jTable1KeyReleased(evt); } }); jScrollPane1.setViewportView(jTable1); 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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 792, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(HeaderLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(HeaderLabel) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE) .addContainerGap()) ); pack();
Any suggestions on how to do this?
PS! I am using Netbeans...
- 02-16-2012, 04:45 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Column names
jTable1.setModel(new javax.swing.table.DefaultTableModel(vectordata.get Data(),vectordata.getColumns()));
edit that line (vectordata.getColumns()) :)
- 02-16-2012, 04:52 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Column names
The other option is to change your SQL query to return aliased names for the columns.
Though that is getting a bit too "GUI logic in business logic" for my liking.
- 02-16-2012, 05:09 PM #4
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
- 02-16-2012, 05:15 PM #5
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Column names
jTable1.setModel(new javax.swing.table.DefaultTableModel(vectordata.get Data(),new Vector<String>(Arrays.asList("ColumnA", "ColumnB" ,"...."))));

or like Tolls said use aliases or or or.
- 02-16-2012, 05:19 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Column names
Exactly.
It's just a collection of Strings...
Where they come from is entirely up to you.
- 02-16-2012, 05:33 PM #7
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Column names
Very nice. Worked very very good.
.gif)
Thanks!
- 02-16-2012, 06:58 PM #8
Similar Threads
-
CompositeUserType for customisable column names
By AnnaM in forum HibernateReplies: 0Last Post: 01-16-2012, 07:33 AM -
insert row and column and delete row and column
By daredavil82 in forum New To JavaReplies: 13Last Post: 09-22-2011, 06:10 PM -
How to write column by column to text file
By trkece in forum JDBCReplies: 9Last Post: 02-15-2011, 01:13 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM -
How to get column names for Oracle.sql.Datum Objects
By harikanth in forum JDBCReplies: 0Last Post: 11-08-2007, 08:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks