Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #16250 > unrolled thread

NetBeans Application with sortable Table and pre-existing frame/table code

Started byclusardi2k@aol.com
First post2012-07-23 10:54 -0700
Last post2012-07-25 09:31 -0700
Articles 7 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  NetBeans Application with sortable Table and pre-existing frame/table code clusardi2k@aol.com - 2012-07-23 10:54 -0700
    Re: NetBeans Application with sortable Table and pre-existing frame/table code Lew <lewbloch@gmail.com> - 2012-07-23 12:55 -0700
      Re: NetBeans Application with sortable Table and pre-existing frame/table code clusardi2k@aol.com - 2012-07-25 07:54 -0700
        Re: NetBeans Application with sortable Table and pre-existing frame/table code clusardi2k@aol.com - 2012-07-25 08:14 -0700
        Re: NetBeans Application with sortable Table and pre-existing frame/table code clusardi2k@aol.com - 2012-07-25 08:37 -0700
        Re: NetBeans Application with sortable Table and pre-existing   frame/table code Nigel Wade <nmw@ion.le.ac.uk> - 2012-07-25 16:40 +0100
          Re: NetBeans Application with sortable Table and pre-existing   frame/table code clusardi2k@aol.com - 2012-07-25 09:31 -0700

#16250 — NetBeans Application with sortable Table and pre-existing frame/table code

Fromclusardi2k@aol.com
Date2012-07-23 10:54 -0700
SubjectNetBeans Application with sortable Table and pre-existing frame/table code
Message-ID<e4d9d3b6-0457-45c8-9bf3-6f7bbe0da09a@googlegroups.com>
I have an NetBeans application with a few controls. To this, I dragged a Swing Table control from the control palette. I'd like to use all of these and not use the below code.

The reason I don't want to use the below example is I cannot use the dragged in table above. I unsuccessfully tried to modify this code (the frame and table variable names) to use the above dragged-in table with its pre-existing frame. But, when I try to do that I get error messages such as "non-static method add (...) cannot be referenced from a static context". I even tried instantiating a class object, but received similar error messages.

http://www.java-tips.org/java-se-tips/javax.swing/sorting-and-filtering-tables.html

This linked example creates a table (columns which are sortable) but the new window is not docked anywhere.

Do you have a better example for me.

Thanks,

[toc] | [next] | [standalone]


#16259

FromLew <lewbloch@gmail.com>
Date2012-07-23 12:55 -0700
Message-ID<2c4cb42d-92d9-48a6-9d69-0f63c5461070@googlegroups.com>
In reply to#16250
(unknown) wrote:
> I have an NetBeans application with a few controls. To this, I dragged a Swing Table control from the control palette. I&#39;d like to use all of these and not use the below code.
> 
> The reason I don&#39;t want to use the below example is I cannot use the dragged in table above. I unsuccessfully tried to modify this code (the frame and table variable names) to use the above dragged-in table with its pre-existing frame. But, when I try to do that I get error messages such as &quot;non-static method add (...) cannot be referenced from a static context&quot;. I even tried instantiating a class object, but received similar error messages.

Each of those error messages can be solved by analysis and the right code changes.

Giving up on error messages is a recipe for failure.

> http://www.java-tips.org/java-se-tips/javax.swing/sorting-and-filtering-tables.html

Where's *your* code?

http://sscce.org/

> This linked example creates a table (columns which are sortable) but the new window is not docked anywhere.
> 
> Do you have a better example for me.

Do you have a better example for us?

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#16341

Fromclusardi2k@aol.com
Date2012-07-25 07:54 -0700
Message-ID<ff39b71a-20af-45b7-9123-4403d6e8b6d0@googlegroups.com>
In reply to#16259
How do I get this modified code to work.

I modified the working/good code from the attached link to the below code. The reason I do not want to use the code exactly on this link is it creates a new window and doesn't use the form that I already am using. The pre-existing form has jPanel1 (swing JPanel) containing jPanel3 (swing JPanel). And, jPanel3 contains jTable1 (swing JTable).

http://www.java-tips.org/java-se-tips/javax.swing/sorting-and-filtering-tables.html 

(jTable1 was dragged to the form from the swing control  palette. Design view shows a table with 4 columns and 4 rows, but the area is blanked out when I run the project!)

The below code compiles and runs, but the form's jTable1 is not populated with the data.

Thanks  for any help,

...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

       Runnable runner = new Runnable() {
        public void run() {
           //JFrame frame = new JFrame("Sorting JTable");
           //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           Object rows[][] = {
               {"AMZN", "Amazon", 41.28},
               {"EBAY", "eBay", 41.57},
               {"GOOG", "Google", 388.33},
               {"MSFT", "Microsoft", 26.56},
               {"NOK", "Nokia Corp", 17.13},
               {"ORCL", "Oracle Corp.", 12.52},
               {"SUNW", "Sun Microsystems", 3.86},
               {"TWX",  "Time Warner", 17.66},
               {"VOD",  "Vodafone Group", 26.02},
               {"YHOO", "Yahoo!", 37.69}
             };
           String columns[] = {"Symbol", "Name", "Price"};
           TableModel model =
               new DefaultTableModel(rows, columns) {
             public Class getColumnClass(int column) {
               Class returnValue;
               if ((column >= 0) && (column < getColumnCount())) {
                 returnValue = getValueAt(0, column).getClass();
               } else {
                 returnValue = Object.class;
               }
               return returnValue;
             }
           };

           //JTable table = new JTable(model);
           RowSorter<TableModel> sorter =
             new TableRowSorter<TableModel>(model);
           jTable1.setRowSorter(sorter);
           JScrollPane pane = new JScrollPane(jTable1);
           //frame.add(pane, BorderLayout.CENTER);
           //frame.setSize(300, 150);
           jPanel3.setVisible(true);
           jPanel3.add(pane, BorderLayout.CENTER);
           jTable1.setVisible(true);
         }
       };
       EventQueue.invokeLater(runner);
  

[toc] | [prev] | [next] | [standalone]


#16344

Fromclusardi2k@aol.com
Date2012-07-25 08:14 -0700
Message-ID<3f4ed9a5-6d4c-442d-a21c-1206d4c4bf32@googlegroups.com>
In reply to#16341
With the below, line of code I get the same result of no table displayed.

           jPanel3.add(pane);

[toc] | [prev] | [next] | [standalone]


#16347

Fromclusardi2k@aol.com
Date2012-07-25 08:37 -0700
Message-ID<8938292b-a43a-40a9-8fc6-721aceffd12f@googlegroups.com>
In reply to#16341
The below 2 additional attempts also are failing (each yields the same result).  jInternalFrame1 (class JInternalFrame) was dragged to the designer form from the NetBeans swing Palette.

(Another Attempt)  
         JTable jTable1 = new JTable(model);
           RowSorter<TableModel> sorter =
             new TableRowSorter<TableModel>(model);
           jTable1.setRowSorter(sorter);
           JScrollPane pane = new JScrollPane(jTable1);
           //frame.add(pane, BorderLayout.CENTER);
           //frame.setSize(300, 150);
           jInternalFrame1.add(pane, BorderLayout.CENTER);
           jInternalFrame1.setVisible(true);


(And Yet Another Unsuccessful Attempt)  
           //JTable table = new JTable(model);
           RowSorter<TableModel> sorter =
             new TableRowSorter<TableModel>(model);
           jTable1.setRowSorter(sorter);
           JScrollPane pane = new JScrollPane(jTable1);
           //frame.add(pane, BorderLayout.CENTER);
           //frame.setSize(300, 150);
           jInternalFrame1.add(pane, BorderLayout.CENTER);
           jInternalFrame1.setVisible(true);

[toc] | [prev] | [next] | [standalone]


#16348 — Re: NetBeans Application with sortable Table and pre-existing frame/table code

FromNigel Wade <nmw@ion.le.ac.uk>
Date2012-07-25 16:40 +0100
SubjectRe: NetBeans Application with sortable Table and pre-existing frame/table code
Message-ID<a7aib5FuoqU1@mid.individual.net>
In reply to#16341
On 25/07/12 15:54, clusardi2k@aol.com wrote:
> How do I get this modified code to work.
> 
> I modified the working/good code from the attached link to the below code. The reason I do not want to use the code exactly on this link is it creates a new window and doesn't use the form that I already am using. The pre-existing form has jPanel1 (swing JPanel) containing jPanel3 (swing JPanel). And, jPanel3 contains jTable1 (swing JTable).
> 
> http://www.java-tips.org/java-se-tips/javax.swing/sorting-and-filtering-tables.html
> 
> (jTable1 was dragged to the form from the swing control  palette. Design view shows a table with 4 columns and 4 rows, but the area is blanked out when I run the project!)
> 
> The below code compiles and runs, but the form's jTable1 is not populated with the data.
> 
> Thanks  for any help,
> 
> ...
> 
> import java.awt.BorderLayout;
> import java.awt.EventQueue;
> import javax.swing.*;
> import javax.swing.table.DefaultTableModel;
> import javax.swing.table.TableModel;
> import javax.swing.table.TableRowSorter;
> 
>         Runnable runner = new Runnable() {
>          public void run() {
>             //JFrame frame = new JFrame("Sorting JTable");
>             //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>             Object rows[][] = {
>                 {"AMZN", "Amazon", 41.28},
>                 {"EBAY", "eBay", 41.57},
>                 {"GOOG", "Google", 388.33},
>                 {"MSFT", "Microsoft", 26.56},
>                 {"NOK", "Nokia Corp", 17.13},
>                 {"ORCL", "Oracle Corp.", 12.52},
>                 {"SUNW", "Sun Microsystems", 3.86},
>                 {"TWX",  "Time Warner", 17.66},
>                 {"VOD",  "Vodafone Group", 26.02},
>                 {"YHOO", "Yahoo!", 37.69}
>               };
>             String columns[] = {"Symbol", "Name", "Price"};
>             TableModel model =
>                 new DefaultTableModel(rows, columns) {
>               public Class getColumnClass(int column) {
>                 Class returnValue;
>                 if ((column>= 0)&&  (column<  getColumnCount())) {
>                   returnValue = getValueAt(0, column).getClass();
>                 } else {
>                   returnValue = Object.class;
>                 }
>                 return returnValue;
>               }
>             };
> 
>             //JTable table = new JTable(model);
>             RowSorter<TableModel>  sorter =
>               new TableRowSorter<TableModel>(model);
>             jTable1.setRowSorter(sorter);
>             JScrollPane pane = new JScrollPane(jTable1);

I don't think you want to be doing this. 
I presume the jTable1 already exists, and has a parent.

>             //frame.add(pane, BorderLayout.CENTER);
>             //frame.setSize(300, 150);
>             jPanel3.setVisible(true);
>             jPanel3.add(pane, BorderLayout.CENTER);
>             jTable1.setVisible(true);
>           }
>         };
>         EventQueue.invokeLater(runner);
> 

Nowhere that I can see are you setting the new table model in jTable1.

The code below works. It's just a basic JFrame app. created in NetBeans, with the code 
inserted to set the model/sorter for the default table. If you don't change the model
the code will throw OOB exception because the model data in the sorter only has 3 columns,
and the default table created by Matisse has 4.


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

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author NetBeans
 */
public class NewJFrame extends javax.swing.JFrame {

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

    Runnable runner = new Runnable() {

      public void run() {
        Object rows[][] = {
          {"AMZN", "Amazon", 41.28},
          {"EBAY", "eBay", 41.57},
          {"GOOG", "Google", 388.33},
          {"MSFT", "Microsoft", 26.56},
          {"NOK", "Nokia Corp", 17.13},
          {"ORCL", "Oracle Corp.", 12.52},
          {"SUNW", "Sun Microsystems", 3.86},
          {"TWX", "Time Warner", 17.66},
          {"VOD", "Vodafone Group", 26.02},
          {"YHOO", "Yahoo!", 37.69}
        };
        String columns[] = {"Symbol", "Name", "Price"};
        TableModel model =
                new DefaultTableModel(rows, columns) {

                  public Class getColumnClass(int column) {
                    Class returnValue;
                    if ((column >= 0) && (column < getColumnCount())) {
                      returnValue = getValueAt(0, column).getClass();
                    }
                    else {
                      returnValue = Object.class;
                    }
                    return returnValue;
                  }
                };

        RowSorter<TableModel> sorter =
                new TableRowSorter<TableModel>(model);
        jTable1.setModel(model);
        jTable1.setRowSorter(sorter);
      }
    };

    EventQueue.invokeLater(runner);
  }

  /**
   * 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() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        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()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );

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

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    }
    catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
        new NewJFrame().setVisible(true);
      }
    });
  }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
}

[toc] | [prev] | [next] | [standalone]


#16349 — Re: NetBeans Application with sortable Table and pre-existing frame/table code

Fromclusardi2k@aol.com
Date2012-07-25 09:31 -0700
SubjectRe: NetBeans Application with sortable Table and pre-existing frame/table code
Message-ID<87c56b53-ba72-45d7-a587-b01000786fc2@googlegroups.com>
In reply to#16348
On Wednesday, July 25, 2012 11:40:20 AM UTC-4, Nigel Wade wrote:

It works great.
Thank you,

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web