Join 150,043 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,617 people online right now. Registration is fast and FREE... Join Now!
I am sure you all probably hate me by now with all the questions. I hope this is my last one. This is my first time using JTabbedPane, JTable and all that jazz.
So to my question. I hope I can explain this well and you all can understand. I have a class TabMain. This is where I create the JFrame (in a method) and also add the JTabbedPane and of course every other tab as well (Same method as the JFrame creation...CreateAndShowGUI). Now when I create the other tabs, I place a JTable in each TabPanel. Each of the tabs are created in their own class (inside a method (CreateTab3) so basically its nice and neat. Now, when I click a button on the frame I want it to populate a certain table. Lets say for this example that it populates the third tab which is called (Tab3). Now since Tab3 is created in its own class I call the "UpdateTable" method inside the Tab3 class. For some reason I get a null exception error. Mind you the JTable, JScrollPane...etc whatever is inside the Tab3 is created in its own class.
What I am thinking is maybe the reason I get the JTable is null is because I create the JTable in the Tab3 class? Should I create each table in the TabMain and pass the JTable as a parameter?
If anyone can help me out it would be much appreciated, reCoded.
hello.. nice introduction though... well probably as others would say.. could you please post your code.. to better understand what you are trying to accomplish..
well.. Honestly.. IDK how to use JTable.. but I think I have a slight Idea bout the nullexception error.. if my guess is correct.. it is an error that occur when passing a null value into an object that doesn't accept null..
so its safe to check if your actually passing real values to your table.. another thing.. you are probably updating a Table that doesnt contain anything..
PBL so your saying I should have in the main class where the JFrame is created something like, private Tab3 tab3...and when I go to add something to that table when the button is click, tab3.??? where the ??? are is should be a method call that is like a getTableTab3()?
I have all the code in one class right now so it will take me some work to split it back up into their own classes and again and I want to make sure I understand what you are saying before I do so.
PBL so your saying I should have in the main class where the JFrame is created something like, private Tab3 tab3...and when I go to add something to that table when the button is click, tab3.??? where the ??? are is should be a method call that is like a getTableTab3()?
I have all the code in one class right now so it will take me some work to split it back up into their own classes and again and I want to make sure I understand what you are saying before I do so.
Thanks, reCoded
If everything fits in one class righ now and that all the code is about 2 hundred lines no need to really split in in multiple classes. If it exceed this it would worth to built a class Tab3 that extends JTable
So in the the class that builds the JFrame you can
Tab3 t1, t2, t3; t1 = new Tab3(.....); add(t1); t2 = new Tab3(.....); add(t2); t3 = new Tab3(.....); add(t3);
And class Tab3 extends JTable will manage the data and the JTable selection with the appropriate modelS So you won't even have to take care which tab of the JTabbedPane is showed
This post has been edited by pbl: 1 Jun, 2008 - 07:07 PM
JFrame frame = new JFrame("Tabs"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel topPanel = new JPanel();
tabModel = new DefaultTableModel(); tabModel.setDataVector(rows,columns);
JPanel tabPanel = new JPanel();
JTable jt = new JTable(tabModel); jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jt.setColumnSelectionAllowed(false);
//Set the size of the column headers for (int i=0; i<columnNames.length;i++){ TableColumn col = jt.getColumnModel().getColumn(i); int width = 130; col.setPreferredWidth(width); }
public void setjspR(JScrollPane jsPane){ jspR = jsPane; }
public JScrollPane getjspR(){ return jspR; } private void addRowR(String[] str){
tabModelR.insertRow(0,str);
}
CODE IS HERE TO ADD THE ROWS TO THIS TABLE. retrieveRows is the method name! }
I have a button also that when click calls the method retrieveRows from the Tab2 class.
I do it like this...
Tab2 tab2 = new Tab2(); tab2.retrieveRows();
inside the retrieveRows method there is a call to the addRows method that is inside the Tab2 class and it does not add the rows but instead gives me a null pointer.
Thanks and hope someone can help me tackle this problem, reCoded
This post has been edited by reCoded: 2 Jun, 2008 - 04:34 AM
public void setjspR(JScrollPane jsPane){ jspR = jsPane; }
public JScrollPane getjspR(){ return jspR; } private void addRowR(String[] str){
tabModelR.insertRow(0,str);
}
private void addRowR(String[] str){
tabModel.insertRow(0,str);
}
retrieveRows is the method name! { String[] strsR= {"asdf","asdf","asdf","asdf"}; addRowR(strsR); }
I changed it around to that and it works. When I add the row I don't get a null pointer error anymore. This time it does not populate the table. I am still trying to get this to work. PBL thanks for your help, if you know what the problem might be let me know. Should I use that fire method after I add the rows? I am guessing I am missing something simple.
Thanks, reCoded
This post has been edited by reCoded: 2 Jun, 2008 - 05:56 AM
I am really frusterated with this. It still will not add the row to the JTable that is inside of Tab2. The weird thing is I am doing the samething I did when the code was all in the same class. This is starting to annoy me and I am starting to code very sloppy because I had enough with this.
import javax.swing.*; import java.awt.*; public class Frame extends JFrame {
final static String[] tab1ColName = {"Col 1", "Col 2", "Col 3"}; final static String tab1LabelName = "This is the header of Tab 1"; final static String[] tab2ColName = {"Col A", "Col B", "Col C", "Col D"}; final static String tab2LabelName = "This is the header of Tab 2"; final static String[] tab3ColName = {"First Col", "Second col"}; final static String tab3LabelName = "This is the header of Tab 3"; final static String[] tabName = {"Tab one", "Tab two", "Tab three"};
JLabel label = new JLabel("This is the main header of the application"); label.setHorizontalAlignment(SwingConstants.CENTER); add(label, BorderLayout.NORTH);
pane = new JTabbedPane(); tab = new Tab[3]; tab[0] = new Tab(tab1ColName, tab1LabelName); pane.add(tabName[0], tab[0]); tab[1] = new Tab(tab2ColName, tab2LabelName); pane.add(tabName[1], tab[1]); tab[2] = new Tab(tab3ColName, tab2LabelName); pane.add(tabName[2], tab[2]); add(pane, BorderLayout.CENTER); }
// to do the test void test() { String[] abc = {"Col1", "Col2", "Col3", "Col4"}; // add a row tab[1].addRow(abc); // show it pane.setSelectedIndex(1); String[] str = tab[1].getRow(0); System.out.println("Rows 0 of tab2 contains: "); for(int i = 0; i < str.length; i++) System.out.print(" " + str[i]); System.out.println();
} public static void main(String[] arg) { Frame f = new Frame(); f.setVisible(true);