I have a java class being used as a container, it is coded to have a JPanel and a TabbedPane
I have another java class that builds a JPanel containing JLabels and JTextFields that I want added to the TabbedPane of the container class. I want a new instance of the class that adds the JPanel, so I can add multiple tabs to the container class.
I have reviewed the following: Can we reuse Java Swing application components in other instances of same application?, Multiple instances of model components in Java Swing?, Multiple instance of ONE JFrame, and multiple web authors. I am not a student doing an assignment, I am an old dude, new to Java, moving my top-down skills to OOP.
Here is my code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class TestContainer extends JFrame {
private JPanel contentPane;
public static JTabbedPane tabbedPane1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
TestContainer frame = new TestContainer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestContainer() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollBasePane = new JScrollPane();
scrollBasePane.setBounds(10, 11, 414, 239);
contentPane.add(scrollBasePane);
JPanel panelBase = new JPanel();
scrollBasePane.setViewportView(panelBase);
panelBase.setLayout(null);
tabbedPane1 = new JTabbedPane(JTabbedPane.TOP);
tabbedPane1.setBounds(10, 11, 392, 215);
panelBase.add(tabbedPane1);
// add new JPanel to TabbedPane via a reusable Class
MoreTabs mt1 = new MoreTabs();
tabbedPane1.addTab( mt1() ); //non-static
}
}
And
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MoreTabs{
private JPanel panel = new JPanel();
private JLabel lblPutStuffOn = new JLabel("Put stuff on this panel, T3-MoreTabs.");
private JTextField text = new JTextField();
panel.add(lblPutStuffOn);
panel.add(text);
public String getLblText() {
String lblText;
lblText = lblPutStuffOn.getText();
return lblText;
}
public void setLblText(String s){
lblPutStuffOn.setText(s);
}
public String getTxtText() {
String txtText;
txtText = text.getText();
return txtText;
}
public void setTxtText(String s){
text.setText(s);
}
}