• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Passing JTables

Can you provide me with a working example of passing a JTable?
0
mrslyme
Asked:
mrslyme
  • 13
  • 10
  • 7
  • +1
2 Solutions
 
OviCommented:
What do you mean by passing a JTable. Please explain what you need.
0
 
Giant2Commented:
See here for example about JTable:
http://www.javaalmanac.com/cgi-bin/search/find.pl?words=JTable

If for "passing" you mean:
object A
(varC)

object B
(varD)

how can use varC in object B, you can "pass" the variable varC to the objectB in different manner.
If the variable varC is public you can access it from objectB in this manner:
InstanceOfObjectA.varC
If variable varC is public static you can access it from objectB in this manner:
objectA.varC
If variable varC is private you can access it only if you have a method on objectA that return/set the value of the varC like the following:
public VarcType getVarC(){return varC;}
in this last manner you can pass the varC to the object B or other object type/kind asking for it.

Hope this could help you.
Bye, Giant.
0
 
zzynxSoftware engineerCommented:
Still not convinced? ;°)

Here you are:

/*
 * TablePassingDemo.java
 *
 */

import javax.swing.*;
import javax.swing.table.*;
/**
 *
 * @author  zzynx
 */
public class TablePassingDemo extends javax.swing.JFrame {

    private javax.swing.JPanel centerPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel southPanel;
    private javax.swing.JButton theButton;
    private javax.swing.JTable theTable;
   
    public TablePassingDemo() {
        initComponents();
    }
   
    private void initComponents() {
        centerPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        theTable = new javax.swing.JTable();
        southPanel = new javax.swing.JPanel();
        theButton = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        centerPanel.setLayout(new java.awt.BorderLayout());

        theTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Item1.1", "Item 1.2"},
                {"Item2.1", "Item 2.2"},
                {"Item 3.1", "Item 3.2"},
                {"Item 4.1", "Item 4.2"}
            },
            new String [] {
                "Title 1", "Title 2"
            }
        ));
        jScrollPane1.setViewportView(theTable);

        centerPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);

        theButton.setText("Tell me");
        theButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                theButtonActionPerformed(evt);
            }
        });

        southPanel.add(theButton);

        getContentPane().add(southPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }

    private void theButtonActionPerformed(java.awt.event.ActionEvent evt) {
        MyClass mc = new MyClass(theTable);           // <<<<<<<<<<<<<<<<<<<<< Here I pass the JTable
        String txt = "The table contains " + mc.getRows() + " rows. " +
                    "The text in the 2nd row, 1st column is: " + mc.getData(1, 0).toString();
        JOptionPane.showMessageDialog(this, txt);
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        new TablePassingDemo().show();
    }
   
    private class MyClass {
        JTable theTable;
       
        public MyClass(JTable table) {
            theTable = table;
        }
       
        public int getRows() {
            return theTable.getRowCount();
        }

        public Object getData(int row, int col) {
            return ((DefaultTableModel)theTable.getModel()).getValueAt(row, col);
        }
    }
   
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Giant2Commented:
>Still not convinced?
4 queston about the same problem?
mrslyme,
if you need some other explanation please tell us. Tell us what is not clear and we try to help you.
0
 
mrslymeAuthor Commented:
I need to see a working example of a Jtable being passed.
0
 
zzynxSoftware engineerCommented:
See my previous comment
0
 
Giant2Commented:
like zzynx says:

MyClass mc = new MyClass(theTable);           // <<<<<<<<<<<<<<<<<<<<< Here I pass the JTable

I add this comment about zzynx code:
the class MyClass contain an instance (reference) of a table. When the MyClass is created this instance is valorized.
    private class MyClass {
        JTable theTable;//<------the instance/reference of a table in MyClass
       
        public MyClass(JTable table) {//<-----when create MyClass the JTable is passed in the constructor
            theTable = table;//<--- here the JTable passed in the constructor is linked to the "local" instance "thetable"
        }

You could pass the instance even if MyClass has a setter method like:
public void setTheTable(JTable table){
theTable = table;
}

so to pass the jtable to this MyClass you can do:
JTable tab=new JTable();
MyClass cl=new MyClass();
cl.setTheTable(tab);
In this manner you create an instance of class MyClass and after (not in the cretor method) you pass the jtable using the method setTheTable.

Is it clear now?

Bye, Giant.
0
 
mrslymeAuthor Commented:
A JTable has never been passed from one class to a second class. If a working example exists, please display it.
0
 
zzynxSoftware engineerCommented:
Hey man,

And what do you think this does?

private class MyClass {
        JTable theTable;
       
        public MyClass(JTable table) {   // <<<<<<<<<<<<< Here the incoming parameter is passed to the MyClass variable theTable.
            theTable = table;
        }
       
        public int getRows() {
            return theTable.getRowCount();  // <<<<<<<< Here we work with theTable
        }

        public Object getData(int row, int col) {
            return ((DefaultTableModel)theTable.getModel()).getValueAt(row, col);     // <<<<<<< Here too
        }
    }

What is theTable according to you, if it isn't the passed table?
0
 
zzynxSoftware engineerCommented:
I admit it is maybe somewhat confusing because I used twice theTable

Change MyClass as follows:

private class MyClass {
        JTable localTable;
       
        public MyClass(JTable table) {   // <<<<<<<<<<<<< Here the incoming parameter is passed to the MyClass variable theTable.
            localTable = table;
        }
       
        public int getRows() {
            return localTable.getRowCount();  // <<<<<<<< Here we work with theTable
        }

        public Object getData(int row, int col) {
            return ((DefaultTableModel)localTable.getModel()).getValueAt(row, col);     // <<<<<<< Here too
        }
    }

And don't tell me that this works because of the fact that MyClass is in the same code file.
You place that code in another MyClass.java file and see if it still works.
0
 
zzynxSoftware engineerCommented:
Didn't change the comments:

public MyClass(JTable table) {   // <<<<<<<<<<<<< Here the incoming parameter is passed to the MyClass variable localTable.

return localTable.getRowCount();  // <<<<<<<< Here we work with localTable
0
 
zzynxSoftware engineerCommented:
>> A JTable has never been passed from one class to a second class.
Yes it is!
the data member "theTable" of the TablePassingDemo class
has been passed to the MyClass class (into the localTable data member)

>> If a working example exists, please display it.
I did!!!
0
 
Giant2Commented:
>A JTable has never been passed from one class to a second class

This is a schematic example
hope more clear:
class MyClassA {
JTable theTableOfClassA;
public MyClassA(){}
public setTheTableeee(JTable thepassedTable){
theTableOfClassA=thepassedTable;
}
}

class MyClassB{
public MyClassB(){}
public void workWithJTable(){
MyClassA cl=new MyClassA();
JTable tryToPassThis=new JTable();<---create a new JTable
cl.setTheTableeee(tryToPassThis);<----pass this created JTable to the object of kind MyClassA
//from this point the object of kind MyClassA work over the JTable passed before.
}
}
0
 
mrslymeAuthor Commented:
I ran the code. It did not pass a JTable.
0
 
zzynxSoftware engineerCommented:
>> I ran the code.
Which code? Mine?
>> It did not pass a JTable.
What makes you conclude that?

If you press the "Tell me!" button, do you see a message telling you how many rows there are and what text is in a certain cell?
If yes, the table is passed. Otherwise you would get an NPE.
0
 
Giant2Commented:
>I ran the code. It did not pass a JTable.
My code (pseudocode) is only to explain you little better.
The code of zzynx goes. It pass the JTable to MyClass.

mrslyme,
from where you say that the code do not pass the JTable? Maybe you are expection something and not see this. But what? Explain us better, please.
0
 
Giant2Commented:
>expection
expecting
0
 
zzynxSoftware engineerCommented:
mrslyme?
0
 
Giant2Commented:
mrslyme is the nickname of the owner of this thread.
;)
0
 
zzynxSoftware engineerCommented:
LOL. I do know that of course :°D
I was asking for some input of mrslyme
0
 
Giant2Commented:
today I'm really happy, so the comments I post today will be more LOLler than normal.
;>
0
 
Giant2Commented:
TOC TOC,
mrslyme.
Give us your presence.
0
 
Giant2Commented:
I think a split or delete without refund.
0
 
zzynxSoftware engineerCommented:
Although the author says my example wasn't OK, I was the first to give him an example of what he asked.
So, I think my comment deserves the points.
0
 
mrslymeAuthor Commented:
http://www.dogpoundsports.com/sportsbook/setOdds.cfm

Cold fusion offers the result I need. But java does not. Yet.
0
 
mrslymeAuthor Commented:
I'll tell you the answer to the problem:

Create a two dimensional array of the listed figure. Assign an identifier to each group and store them.
 Retrieve the numbered groups. Place inside an identical table.
0
 
zzynxSoftware engineerCommented:
>> So, I think my comment deserves the points.

I meant *all* the points. Since the question was
>>provide me with a working example of passing a JTable?
0
 
zzynxSoftware engineerCommented:
mrslyme, are you sure your comments are for this thread?
I don't understand a word of it.
What has your last comment to do with passing a JTable?
8°/ *absolutely confused*
0
 
mrslymeAuthor Commented:
Do I get a check for 250 pionts?
0
 
zzynxSoftware engineerCommented:
???
0
 
mrslymeAuthor Commented:
Thank you for identifying the term thread.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 13
  • 10
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now