Results 1 to 6 of 6
  1. #1
    kiruba is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Post how to get reference from outside class

    hi friends.... im new to java.. i could search many site but i cant find the solution.. my problem is..im using one class its..

    class class_name{
    JTextField tf=null;
    class_name() //constractor
    {
    tf=new JTextField(10);
    tf.setText("Hello");
    }
    public JTextField getText() //return method
    {
    rerturn tf;
    }
    }

    when i call the method geText() into another JTextField in another class . i can't get the reference of old JTextField....it show null pointer error.....

    here im calling the reference name..

    class class_name2{
    class_name2()
    {
    class_name cn=new class_name();
    JTextField tf2=cn.getText();
    System.out.println(""+tf2.getText());
    }
    }

  2. #2
    ClickerMonkey is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Are using an IDE for this? Should we assume that you typed that code directly into the forum post? You have some syntax errors and I'm not sure if they're related to this problem or not...

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    Could you please post your main method code here?
    Mak
    (Living @ Virtual World)

  4. #4
    kiruba is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks for your reply... i got it..... just i post program here is just a example what i need.. actually what i made mistake is i created object for the class..and then again i created another object for the class.. im using new object to call the method to class..so what happened here what are all values in created for that textfield is overridded. so i get new values...but here i need old value which i insert earlier..

    finally i got it... again here im post program is sample as what i need...

  5. #5
    kiruba is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    im just type over the text box.. im not used any ide to post the coding..

  6. #6
    kiruba is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    another doubt in JTable guys....
    tell me how can i remove Table header...
    i tried columnRemove method .it removed the column but when ever i add new column using model.addColumn("new Field"); but what happened here old header and new header are append...
    what i need means i need only the new header only...
    and this is my code:::::

    package sample;

    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;

    public class TableColumnRemove {

    DefaultTableModel model;
    JTable table;

    public static void main(String[] args) {
    new TableColumnRemove();
    }

    public TableColumnRemove() {
    JFrame frame = new JFrame("Remove a column from a JTable");
    JPanel panel = new JPanel();
    String data[][] = {{"Vinod", "MCA", "Computer"},
    {"Deepak", "PGDCA", "History"},
    {"Ranjan", "M.SC.", "Biology"},
    {"Radha", "BCA", "Computer"}};
    String col[] = {"Name.", "Course", "Subject"};
    model = new DefaultTableModel(data, col);
    table = new JTable(model);
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    JScrollPane pane = new JScrollPane(table);
    panel.add(pane);
    frame.add(panel);
    frame.setSize(500, 150);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRoot Pane.PLAIN_DIALOG);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    int columnCount = table.getColumnCount();
    for (int i = 0; i < columnCount; i = 0) { // here im remove columns
    Remove(table, i);
    columnCount = table.getColumnCount();
    }
    model.addColumn("new Field"); //after add new column

    }

    private void Remove(JTable table, int col_index) {
    TableColumn tcol = table.getColumnModel().getColumn(col_index);
    table.removeColumn(tcol);

    }
    }

Similar Threads

  1. Replies: 3
    Last Post: 12-08-2010, 11:28 PM
  2. Replies: 1
    Last Post: 10-13-2009, 11:38 AM
  3. Replies: 3
    Last Post: 08-17-2009, 11:59 PM
  4. Replies: 3
    Last Post: 04-24-2009, 07:28 PM
  5. Passing Class Reference to method
    By nekt in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2009, 06:08 AM

Posting Permissions

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