0

I want to put JTabbedPane in the middle,and clicking any tab I want to change will reflect in both above and below panel of tabbedpane.

I tried it but it works only on the below panel.

How to overcome from this problem? Please help me.

Thanks in advance.

Here is my code:

    jTabbedPane1 = new javax.swing.JTabbedPane();
jTabbedPane1.addTab("Daily Market", jScrollPane1);
    jTabbedPane1.addTab("Weekly Market", jScrollPane2);
Aritra
  • 163
  • 4
  • 18
  • 1
    Didn't really get what you mean... Could you explain your problem a bit more detailed and add some more code that presents the actual problem? – Mikle Garin Apr 12 '12 at 07:48

1 Answers1

2

On assumption that you want to change something in the panels above and below your tabbed pane. Sample code changing text of a label in top and bottom panel below:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestJTabbedPane extends JFrame {

    /**
     * 
     */
    private static final long   serialVersionUID    = 1L;

    private void init(){
        this.setLayout(new BorderLayout());
        JPanel topPanel = new JPanel();
        final JLabel topLabel = new JLabel("North");
        topPanel.add(topLabel);
        this.add(topPanel, BorderLayout.NORTH);

        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel firstTabCont = new JPanel();
        firstTabCont.add(new JLabel("First"));
        tabbedPane.addTab("First", firstTabCont);

        JPanel secondTabCont = new JPanel();
        secondTabCont.add(new JLabel("Second"));
        tabbedPane.addTab("Second", secondTabCont);

        this.add(tabbedPane, BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel();
        final JLabel bottomLabel = new JLabel("South");
        bottomPanel.add(bottomLabel);
        this.add(bottomPanel, BorderLayout.SOUTH);

        tabbedPane.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent evt) {
                JTabbedPane pane = (JTabbedPane)evt.getSource();
                int selectedIndex = pane.getSelectedIndex();
                if(selectedIndex == 0){
                    topLabel.setText("");
                    topLabel.setText("Hi");

                    bottomLabel.setText("");
                    bottomLabel.setText("Bye");
                } else {
                    topLabel.setText("");
                    topLabel.setText("Bye");

                    bottomLabel.setText("");
                    bottomLabel.setText("Hi");
                }

            }
        });
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new TestJTabbedPane().init();
    }
}
mprabhat
  • 20,107
  • 7
  • 46
  • 63