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]));

            }
        });
wuno
  • 9,547
  • 19
  • 96
  • 180
  • Do you want add an array or a raw? – ديناصور الأمة Mar 23 '14 at 11:26
  • Take a look at this link: http://stackoverflow.com/questions/3549206/how-to-add-row-in-jtable. It seems like what you want to do. – ديناصور الأمة Mar 23 '14 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. – wuno Mar 23 '14 at 11:27
  • Can you post the whole code of class where you use `Jtable`? – ديناصور الأمة Mar 23 '14 at 11:29
  • thanks for helping me. I edited and added the gui – wuno Mar 23 '14 at 11:30
  • I'm can't see there anything about JTable (excl. JTable tbl1 = new JTable();), read official Oracle tutorial How to use Table – mKorbel Mar 23 '14 at 11:42
  • I add the Jtable then I am trying to set the text. I have read the tutorial I am not understanding what I am missing. thanks for your input though. tbl1.setText(ShippingLabel(sl)); – wuno Mar 23 '14 at 11:43
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Mar 23 '14 at 11:53
  • `p.setLayout(null);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 23 '14 at 11:54
  • I think your saying that the way im making this is wrong. this is for school and the things im am illustrating are just fine the way they are. the last thing I need to work is to print the address when I click a button. all i need to know how to do is make the button work. – wuno Mar 23 '14 at 11:56

1 Answers1

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]));
    
        }
    });
    
Braj
  • 46,415
  • 5
  • 60
  • 76