I am using nested private classes to build a Java Swing application. The tabbedPane does not appear when running the application. When run, the application shows a blank frame. The app exits on close.
The main question is, what are the options to get the tabbedPane to appear? A second question is if nested classes are a good approach/ am I using them correctly?
The following are the relevant classes.
Constructor
package personalApp;
//imports
public class SwingApp {
//Attributes
//----- CONSTRUCTOR ------
SwingApp() throws IOException{
f1 = new Jframe();
JTabbedPane z = new tabbedPane();
f1.add(z);
f1.setVisible(true);
f1.setSize(400,400);
f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
tabbedPane
private class tabbedPane extends JTabbedPane {
@Override
public void setVisible(boolean aFlag) {
super.setVisible(true);
}
@Override
public Component add(String title, Component component) {
super.add("Contacts", new panel2());
return new panel2();
}
}
panel2
private class panel2 extends JPanel{
@Override
public void setLayout(LayoutManager mgr) {super.setLayout(new GridBagLayout());}
@Override
public void setBackground(Color bg) {super.setBackground(mainColor);}
@Override
public void setVisible(boolean aFlag) {super.setVisible(true);}
public void add(Component comp, Object constraints) {
//There is a JTable, JScrollPane(JTable),JToolbar and JButton added within this method. They all follow the same structure.
openButtonPhonebook = new JButton("Open");
//multiple lines of GridBagConstriants. c = GridBagconstraints.
super.add(openButtonNotebook,c);
}
}
Main
public static void main(String[] args) throws IOException {new SwingApp();}
I have tried adding the components in multiple add methods. This gives an error for overwriting the same method.
I have tried and was successful with adding components within the constructor method. This is causing the code to become hard to navigate. I want to use more abstraction to improve the readability of the code.
Ultimately, I am building a swing app with too much code to put in a constructor method. If there are alternatives to nested classes which improve readability, I am open to them.