How to get components of a jpanel while on runtime in Java. I've been trying to get the components in order to perform a method with them as parameters but I realized that for some reason I can't seem to get the components while on runtime. If you need more specifics let me know, I just didn't want to make it redundant to put code because I think I can just get the answer without showing it. However let me know if it's needed.
public SetupFrame2(int flats, int floors ) {
initComponents();
//Construction of the JTABLE
jPanel1.setLayout(new BorderLayout());
int rows = flats*floors;
JTable tbl = new JTable();
DefaultTableModel dtm = new DefaultTableModel();
String colomnName[] = {"Floor","Flat","Area","View","Vacant"};
dtm.setColumnIdentifiers(colomnName);
for (int count = 1; count <= rows; count++) {
dtm.addRow(new Object[] {"", "", "","",""});
}
tbl.setModel(dtm);
tbl.setPreferredScrollableViewportSize(new Dimension(100,250));
tbl.setFillsViewportHeight(true);
tbl.getTableHeader().setReorderingAllowed(false);
jPanel1.add(new JScrollPane(tbl));
}
*
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
for( Component f : jPanel1.getComponents() ) { //It isnt getting the component on run time,
if(f instanceof JTable){
if(isJTableEmpty((JTable)f) == false){ //isJtable is a function form setupFrame2's superclass and its job so far is to return false, however its not working...
JOptionPane.showMessageDialog(null, "Empty", "Error", JOptionPane.ERROR_MESSAGE);
break;
}
}
}
}
I think that your question is an example of an XY problem where you ask how to solve a specific code problem, how to get the components in a container, when the best solution may be to use a completely different approach. As for your direct question, this is simply done by calling getComponents()
on the container, a method that returns an array of Component, although if you nest containers, you'll need to drill down into the container/component tree to find your component of interest. For the overall problem -- how to get reference of interes, often there are better and cleaner solutions such as creating an ArrayList or HashMap to hold your references of interest, the details of the solution though will depend on the details of your problem and code, of which we know little.
As always, try to be sure to separate the GUI code from the logic code, and this is often best achieved through an M-V-C like program structure.
Edit:
I thought it was holding a JTable
Just look at your own code; it won't lie:
jPanel1.add(new JScrollPane(tbl));
What do you see being added directly to jPanel1? Answer: new JScrollPane(...)
Your JTable is there, but it's two containers deep from the jPanel1 since it's held by the scrollpane's JViewport which is held by the JScrollPane:
Yes, you could get to it if you recursively went through jPanel1's children, but that's fragile code.
... but I'm curious as to why you think I'm doing kluges techniques
Because you're using the GUI structure to obtain logical references, and when you do this you create very brittle code. If anyone else has to maintain this code, and then make changes to the GUI structure, the references will be broken, and they'll end up scratching their head wondering why your code is now throwing NullPointerExceptions.
Instead why not simply but the JTable (or perhaps better, its model) into a variable or a collection of some sort for easy and stable reference?