Results 1 to 6 of 6
- 01-07-2011, 04:43 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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());
}
}
- 01-07-2011, 05:42 AM #2
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
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...
- 01-07-2011, 06:35 AM #3
Could you please post your main method code here?
Mak
(Living @ Virtual World)
- 01-07-2011, 07:11 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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...
- 01-07-2011, 07:12 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
im just type over the text box.. im not used any ide to post the coding..
- 01-07-2011, 07:17 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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
-
How to Instance and reference a class by its name????
By lrocha in forum Advanced JavaReplies: 3Last Post: 12-08-2010, 10:28 PM -
deploy existing web project[class reference problem]
By runfast in forum EclipseReplies: 1Last Post: 10-13-2009, 10:38 AM -
Static and non staic reference, Synchronized and unsynchronized, abstarct class,
By Basit56 in forum New To JavaReplies: 3Last Post: 08-17-2009, 10:59 PM -
DefaultListModel being updated in another class (by reference?)
By rickyoswald in forum Advanced JavaReplies: 3Last Post: 04-24-2009, 06:28 PM -
Passing Class Reference to method
By nekt in forum Advanced JavaReplies: 5Last Post: 03-26-2009, 05:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks