Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looking at your code, it seems you are doing it wrong. First of all don't write these lines</p> <pre><code>private JPanel panel1; private JPanel panel2; </code></pre> <p>instead write : </p> <pre><code>private A a = new A(); private B b = new B(a); </code></pre> <p>Since a and b, themselves are Panels now, as they extending JPanel class.</p> <p>So add this to your tabbedPane now : </p> <pre><code>jTabbedPane1.addTab("Tab 1", null, a/*This is your Panel1*/, "Just Panel"); jTabbedPane1.addTab("Tab 2", null, b/*This is your Panel2*/, "Button"); </code></pre> <p>Simply add a JPanel variable to your B class and change the constructor of your class B as follows : </p> <pre><code>JPanel panel1; public B(JPanel panel) { pane1 = panel; initComponents(); // make this method return void in it's definition, in both the classes. } </code></pre> <p>Now inside the actionPerformed() method do this :</p> <pre><code>jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub panel1.setBackground(Color.RED); } }); </code></pre> <p>Here is small program modified from the previous submission, that resembles your case : </p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TabbedPaneExample extends JPanel { private Panel1 panel1; private Panel2 panel2; public TabbedPaneExample() { super(new GridLayout(1, 1)); JTabbedPane tabbedPane = new JTabbedPane(); //panel1 = getPanel("Panel Number 1"); panel1 = new Panel1("Panel Number 1"); tabbedPane.addTab("Tab 1", null, panel1, "Just Panel"); tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); //panel2 = getPanelWithButton("COLOR"); panel2 = new Panel2("COLOR", panel1); tabbedPane.addTab("Tab 2", null, panel2, "Button"); tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); add(tabbedPane); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); } private static void createAndDisplayGUI() { JFrame frame = new JFrame("Tabbed Pane Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TabbedPaneExample(), BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndDisplayGUI(); } }); } } class Panel1 extends JPanel { public JLabel label; public Panel1(String text) { label = new JLabel(text); label.setHorizontalAlignment(JLabel.CENTER); setLayout(new GridLayout(1, 1)); setBackground(Color.RED); add(label); } } class Panel2 extends JPanel { public JButton button; public JPanel panel1; public Panel2(String text, JPanel panel) { panel1 = panel; button = new JButton(text); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { panel1.setBackground(Color.BLUE); } }); setLayout(new GridLayout(1, 1)); add(button); } } </code></pre> <p>Hope this will help you explain what you doing wrong.</p> <p>Here is the image of the program as it is started : </p> <p><img src="https://i.stack.imgur.com/2Aq9g.png" alt="AT START"></p> <p>Here is the image of the second tab with button : </p> <p><img src="https://i.stack.imgur.com/Jbdxv.png" alt="TAB WITH BUTTON ON IT"></p> <p>Here is the image of the first tab as you click on tab2's button to change tab1's background color to blue : </p> <p><img src="https://i.stack.imgur.com/DiidC.png" alt="BACKGROUND COLOR OF TAB1 CHANGED"></p> <p>Hope this might help you in your endeavour.</p> <p>Regards</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload