0

I have an array holding an address, line 1 and line 2 of the address.

I want to add the array to a Jtable when a btn is clicked. I have read documentation and I am still not getting it please help me.

import java.awt.*;
import java.awt.event.*;
import java.awt.EventQueue;

import javax.swing.*;

public class Driver extends Family {

    private JFrame f;
    private JPanel p;

    JButton btn1 = new JButton("Business Shipping");
    JButton btn2 = new JButton("Family Shipping");

    JLabel lbl1 = new JLabel("Business Name:");
    JLabel lbl2 = new JLabel("URL:");
    JLabel lbl3 = new JLabel("Professional Name");
    JLabel lbl4 = new JLabel("First Name:");
    JLabel lbl5 = new JLabel("Last Name:");
    JLabel lbl6 = new JLabel("Email:");
    JLabel lbl7 = new JLabel("Phone:");
    JLabel lbl8 = new JLabel("Relationship:");
    JLabel lbl9 = new JLabel("Answer Call:");

    JTextField jt1 = new JTextField(businessName);
    JTextField jt2 = new JTextField(website);
    JTextField jt3 = new JTextField (toString());
    JTextField jt4 = new JTextField(First());
    JTextField jt5 = new JTextField(Last());
    JTextField jt6 = new JTextField(Email());
    JTextField jt7 = new JTextField(Phone());
    JTextField jt8 = new JTextField(Relationship());
    String txt = Boolean.toString(AnswerCall(Relationship()));//converting boolean to string value
    JTextField jt9 = new JTextField(txt);
    JTable tbl1 = new JTable();



    public Driver () {      
        gui();      
    }
      btn1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn1MouseClicked(evt);
                tbl1.setText(ShippingLabel(sl));                
            }
        });

    public void gui() { 
        f = new JFrame("Address Book");     
        p = new JPanel();   
        f.add(p);
        p.setLayout(null);

        p.add(btn1);
        p.add(btn2);

        p.add(tbl1);

        p.add(lbl1);
        p.add(lbl2);
        p.add(lbl3);
        p.add(lbl4);
        p.add(lbl5);
        p.add(lbl6);
        p.add(lbl7);
        p.add(lbl8);
        p.add(lbl9);

        p.add(jt1);
        p.add(jt2);
        p.add(jt3);
        p.add(jt4);
        p.add(jt5);
        p.add(jt6);
        p.add(jt7);
        p.add(jt8);
        p.add(jt9);

        lbl1.setLocation(27, 20);
        lbl2.setLocation(27, 40);
        lbl3.setLocation(27, 60);
        lbl4.setLocation(27, 80);
        lbl5.setLocation(27, 100);
        lbl6.setLocation(27, 120);
        lbl7.setLocation(27, 140);
        lbl8.setLocation(27, 160);
        lbl9.setLocation(27, 180);

        btn1.setLocation(27, 200);
        btn2.setLocation(27, 220);

        jt1.setLocation(223, 20);
        jt2.setLocation(223, 40);
        jt3.setLocation(223, 60);
        jt4.setLocation(223, 80);
        jt5.setLocation(223, 100);
        jt6.setLocation(223, 120);
        jt7.setLocation(223, 140);
        jt8.setLocation(223, 160);
        jt9.setLocation(223, 180);
        tbl1.setLocation(223, 200);


        lbl1.setSize(230, 20);
        lbl2.setSize(230, 20);
        lbl3.setSize(230, 20);
        lbl4.setSize(230, 20);
        lbl5.setSize(230, 20);
        lbl6.setSize(230, 20);
        lbl7.setSize(230, 20);
        lbl8.setSize(230, 20);
        lbl9.setSize(230, 20);

        btn1.setSize(150, 20);
        btn2.setSize(150, 20);

        jt1.setSize(230, 20);
        jt2.setSize(230, 20);
        jt3.setSize(230, 20);
        jt4.setSize(230, 20);
        jt5.setSize(230, 20);
        jt6.setSize(230, 20);
        jt7.setSize(230, 20);
        jt8.setSize(230, 20);
        jt9.setSize(230, 20);
        tbl1.setSize(1, 1);


        // pack the frame for better cross platform support
        f.pack();
        // Make it visible
        f.setVisible(true);
        f.setSize(600,500); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } // End of Gui Method

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                new Driver();

           }
        });
    } // End main Method

       } // End class Driver

array in the address class

    public void ShippingLabel(String[]args) {
  Object [][] sl = {{"555 Hello Ln", "Capital, TX 77777"}};
    }

in the main class

  btn1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn1MouseClicked(evt);
                tbl1.setText(ShippingLabel(sl[0]));

            }
        });
10
  • Do you want add an array or a raw? Commented Mar 23, 2014 at 11:26
  • Take a look at this link: stackoverflow.com/questions/3549206/how-to-add-row-in-jtable. It seems like what you want to do. Commented Mar 23, 2014 at 11:27
  • I dont know what raw it.. I am basically just trying to print a address on two lines the easiest way possible using a onclick function. Commented Mar 23, 2014 at 11:27
  • Can you post the whole code of class where you use Jtable? Commented Mar 23, 2014 at 11:29
  • thanks for helping me. I edited and added the gui Commented Mar 23, 2014 at 11:30

1 Answer 1

1

Use DefaultTableModel to add a new row in JTable

Sample code:

  • Here table having 4 existing rows
  • A new row is added on button click

    Object data[][] = { { "111Hello Ln", "Capital", "TX 77777" },
        { "222 Hello Ln", "Capital", "TX 77777" },
        { "333 Hello Ln", "Capital", "TX 77777" },
        { "444 Hello Ln", "Capital", "TX 77777" } };
    String col[] = { "Name", "Capital", "TX" };
    
    final DefaultTableModel model = new DefaultTableModel(data, col);
    final JTable table = new JTable(model);
    
     ....
    
    btn1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            btn1MouseClicked(evt);
    
            Object [][] sl = {{"555 Hello Ln", "Capital", "TX 77777"}};
            model.addRow(ShippingLabel(sl[0]));
    
        }
    });
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.