Results 1 to 8 of 8

Thread: Column names

  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default 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...

    I have a table that gets data from a MySQL table. Here is some of my code:
    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();
    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.
    Any suggestions on how to do this?

    PS! I am using Netbeans...

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Column names

    jTable1.setModel(new javax.swing.table.DefaultTableModel(vectordata.get Data(),vectordata.getColumns()));

    edit that line (vectordata.getColumns()) :)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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.

  4. #4
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Column names

    Quote Originally Posted by eRaaaa View Post
    jTable1.setModel(new javax.swing.table.DefaultTableModel(vectordata.get Data(),vectordata.getColumns()));

    edit that line (vectordata.getColumns()) :)
    Do you have an example on how this line could look?

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default 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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Column names

    Exactly.
    It's just a collection of Strings...
    Where they come from is entirely up to you.

  7. #7
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Column names

    Very nice. Worked very very good.
    Thanks!

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Column names

    Moving to AWT/Swing

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 01-16-2012, 07:33 AM
  2. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 06:10 PM
  3. Replies: 9
    Last Post: 02-15-2011, 01:13 AM
  4. Replies: 1
    Last Post: 07-06-2010, 04:50 PM
  5. Replies: 0
    Last Post: 11-08-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •