Results 1 to 17 of 17

Thread: Netbeans Jtable

  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post Netbeans Jtable

    Hi people,
    I have created a table and populated it with some data using netbeans.I want to do some calculations on the data but i dont know how to get the data from the table.Anyone with an idea on how to solve this problem?
    Thanks in advance

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Simply use getValueAt() method with the correct row/column index. It's return an Object, you have to cast into the correct type you want.

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Simply use getValueAt() method with the correct row/column index. It's return an Object, you have to cast into the correct type you want.
    Hi,thank you for your reply.I have tried using getValueAt() method but still i cant see the output.Could you please write an example code that gets some values from a table and displays it may be in a text area.
    Thank you in advance

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
        public void GetTableValues() {
            String str = (String) jTable1.getValueAt(1, 0);
            JOptionPane.showMessageDialog(null, str);
        }
    Can you show your code here to see what you have done?

  5. #5
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Java Code:
        public void GetTableValues() {
            String str = (String) jTable1.getValueAt(1, 0);
            JOptionPane.showMessageDialog(null, str);
        }
    Can you show your code here to see what you have done?
    I still get an error."Illigal start of expression".In my code i want when i click the button,to get the values in the table and do some calculations on it.Can you figure out why the error msg is showing.Here is my code

    /*public Object GetData(JTable xenon, int row_index, int col_index){
    return xenon.getModel().getValueAt(row_index, col_index);
    } */

  6. #6
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Quote Originally Posted by Manfizy View Post
    I still get an error."Illigal start of expression".In my code i want when i click the button,to get the values in the table and do some calculations on it.Can you figure out why the error msg is showing.Here is my code

    /*public Object GetData(JTable xenon, int row_index, int col_index){
    return xenon.getModel().getValueAt(row_index, col_index);
    } */
    Sorry that was the wrong code.It should be like this:


    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    public void GetTableValues() {
    String str = (String) Xenon.getValueAt(1, 0);
    JOptionPane.showMessageDialog(null, str);
    }

  7. #7
    radix is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default

    Hi Manfizy,

    Try this:
    Java Code:
    JOptionPane.showMessageDialog(null,jTable1.getValueAt(1,0).toString());

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Manfizy View Post
    Sorry that was the wrong code.It should be like this:


    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    public void GetTableValues() {
    String str = (String) Xenon.getValueAt(1, 0);
    JOptionPane.showMessageDialog(null, str);
    }
    This is wrong. How can you call a method like this.

    Java Code:
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt)  {
          GetTableValues();
    }
    
    public void GetTableValues() {
            String str = (String) Xenon.getValueAt(1, 0);
            JOptionPane.showMessageDialog(null, str);   
    }

  9. #9
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    This is wrong. How can you call a method like this.

    Java Code:
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt)  {
          GetTableValues();
    }
    
    public void GetTableValues() {
            String str = (String) Xenon.getValueAt(1, 0);
            JOptionPane.showMessageDialog(null, str);   
    }
    Hi Eranga,
    Thank you so much.its working now

  10. #10
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Thank you for your replies,am progressing.
    Another question:
    my table can only display fixed data.i want it to update and store data each time i run the program.how can i do that?
    Thank you in advance

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  12. #12
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    In that case you have to use a data model.
    Data model?i tried using a table model but i didnt know how to make it work.How do i go about this?

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  14. #14
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Can you show here what you have tried.
    Ok this is the code:


    package MyXenonConc;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.ListSelectionModel;
    import javax.swing.table.TableModel;

    public class XenonConcUI extends javax.swing.JFrame {
    final String[] columnNames = {"Time(hh:mm)",
    "Power Level(%)"};

    final Object[][] data = {
    { new Float(5), new Integer(20)},
    { new Float(3), new Integer(23)},
    { new Float(2), new Integer(27)},
    {new Float(20), new Integer(35)},
    {new Float(10), new Integer(41)}
    };

    private boolean DEBUG = false;
    private boolean ALLOW_COLUMN_SELECTION = true;
    private boolean ALLOW_ROW_SELECTION = true;

    /** Creates new form XenonConcUI */
    public XenonConcUI() {
    initComponents();
    JTable Xenon = new JTable(new XenonConcUI());
    public void valueChanged(ListSelectionEvent e) {
    //Ignore extra messages.
    if (e.getValueIsAdjusting()) return;

    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
    if (lsm.isSelectionEmpty()) {
    System.out.println("No columns are selected.");
    } else {
    int selectedCol = lsm.getMinSelectionIndex();
    System.out.println("Column " + selectedCol
    + " is now selected.");
    }
    }

    This was just trial and error.Could you please show me how to go about it?
    Thank you

  15. #15
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    Quote Originally Posted by Manfizy View Post
    Ok this is the code:


    package MyXenonConc;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.ListSelectionModel;
    import javax.swing.table.TableModel;

    public class XenonConcUI extends javax.swing.JFrame {
    final String[] columnNames = {"Time(hh:mm)",
    "Power Level(%)"};

    final Object[][] data = {
    { new Float(5), new Integer(20)},
    { new Float(3), new Integer(23)},
    { new Float(2), new Integer(27)},
    {new Float(20), new Integer(35)},
    {new Float(10), new Integer(41)}
    };

    private boolean DEBUG = false;
    private boolean ALLOW_COLUMN_SELECTION = true;
    private boolean ALLOW_ROW_SELECTION = true;

    /** Creates new form XenonConcUI */
    public XenonConcUI() {
    initComponents();
    JTable Xenon = new JTable(new XenonConcUI());
    public void valueChanged(ListSelectionEvent e) {
    //Ignore extra messages.
    if (e.getValueIsAdjusting()) return;

    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
    if (lsm.isSelectionEmpty()) {
    System.out.println("No columns are selected.");
    } else {
    int selectedCol = lsm.getMinSelectionIndex();
    System.out.println("Column " + selectedCol
    + " is now selected.");
    }
    }

    This was just trial and error.Could you please show me how to go about it?
    Thank you
    How can i diplay several values on the same pane?with the code show below i can only display the value of one cell

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    GetTableValues();
    }

    public void GetTableValues() {
    String str = (String) Xenon.getValueAt(1, 0);
    JOptionPane.showMessageDialog(null, str);
    }
    Thank you

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you want to deal with different cells, you need to change index values on getValueAt() method. It's designing is depends on your application. You should think about a way to get all values.

    If you want to get all cell values, use a loop. May be two for loops, one for rows and the other columns.

  17. #17
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Default

    As i told you earlier that am very new to java,i tried using loops but it couldnt work the way i expected.Using the code displaying a single cell value,could you please write an example so that i can get an idea?
    Thank you

Similar Threads

  1. Row Name In Jtable
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 08-16-2008, 01:23 AM
  2. Regarding JTable
    By adeeb in forum AWT / Swing
    Replies: 12
    Last Post: 06-19-2008, 08:39 AM
  3. Regarding JTable
    By adeeb in forum SWT / JFace
    Replies: 0
    Last Post: 06-18-2008, 07:13 PM
  4. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  5. How to add in a new row in Jtable?
    By Ry4n in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2008, 01:26 PM

Posting Permissions

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