You never initialize tabinvestment. You declare it as an instance variable, but you never initialize it in the constructor. At some point you need to call JTabbedPane tabinvestment = new JTabbedPane()
.
Adding a JTabbedPane to a JTabbedPane error
-
18-03-2022 - |
Question
Getting this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Project.reportpane.addComponent(reportpane.java:69)
I'm unable to add the rptinvestment tabbedPane to the panel tabinvestment
public class reportpane extends JPanel {
JTabbedPane rpt;
JPanel tabcashflow;
JPanel tabinvestment;
JPanel tabexchangerates;
JTabbedPane rptinvestment;
JPanel tabofficial;
JPanel tabdem;
JPanel tabcommodities;
public reportpane() {
rpt = new JTabbedPane();
tabcashflow = new JPanel();
tabinvestment = new JPanel();
tabexchangerates = new JPanel();
tabofficial = new JPanel();
tabdem = new JPanel();
tabcommodities = new JPanel();
rpt.add("Cashflow", tabcashflow);
rpt.add("Investement", tabinvestment);
rpt.add("Exchange rates", tabexchangerates);
rpt.setBackground(new Color(255, 255, 255));
rpt.setFont(new Font("sansserif", Font.PLAIN, 14));
rpt.setTabPlacement(javax.swing.JTabbedPane.TOP);
rpt.setBorder(BorderFactory.createEmptyBorder());
rpt.setSize(750, 500);
rptinvestment = new JTabbedPane();
rptinvestment.add("Official", tabofficial);
rptinvestment.add("DEM", tabdem);
rptinvestment.add("Commodities", tabcommodities);
rptinvestment.setBackground(new Color(255, 255, 255));
rptinvestment.setFont(new Font("sansserif", Font.PLAIN, 14));
rptinvestment.setTabPlacement(JTabbedPane.TOP);
rptinvestment.setBorder(BorderFactory.createEmptyBorder());
rptinvestment.setSize(750, 500);
addComponent(tabinvestment, rptinvestment, 0, 0, 675, 570);
addComponent(this, rpt, 10, 10, 675, 570);
this.setLayout(new BorderLayout());
this.setBackground(Color.WHITE);
this.setBorder(null);
this.revalidate();
this.repaint();
}
public void addComponent(Container container, Component c, int x, int y, int width, int height) {
c.setBounds(x, y, width, height);
container.add(c);
}
}
Solution
OTHER TIPS
Your attempting to add components using absolute positioning which is been overridden by the layout manager
JTabbedPane uses its own layout logic and unless your really ever to create your own UI class, can't be changed.
Equally, you've set the frames layout manager to BorderLayout, which will override any positioning values may have set (previously to otherwise)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow