java
java

Reputation: 1165

Swing - JTabbedPane - panel does not show up for both tabs

Why doesnt the southPanel shows up for the first tab - only for the second?

If I instead call the southPanel() for every tab, yes - the southpanel shows up BUT - the listeners does not work (which I think is due to allocate gui-objects such as buttons two times with new memory addresss).

So - whatever I do theres a problem - either the southpanel does not show up or the it shows up but the callbacks doesnt work att all, for the first tab.

Any solutions to this

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

public class MyApp extends JFrame implements ActionListener, ItemListener {

private JTabbedPane pane;
private JPanel tab1Panel, tab2Panel, centerPanel, southPanel, inputPanel, checkPanel;;
private JScrollPane scrollPane1, scrollPane2, scrollPane3;
private JTextArea area1, area2;
private JButton quit, ok;
private JCheckBox check;
private JTextField input;
private JLabel input_lbl, check_lbl;

public static void main(String[] args) {

    new MyApp().setVisible(true);
}

public MyApp() {

    initComp();
    setListeners();
}

private void initComp() {

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(dim.width / 2, dim.height / 2);
    setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

    southPanel();

    tab1();
    tab2();

    pane = new JTabbedPane();
    pane.addTab("info om xml", tab1Panel);
    pane.addTab("rita", tab2Panel);

    Container contentPane = getContentPane();
    contentPane.add(pane);

    setDefaultCloseOperation(EXIT_ON_CLOSE);    
}

private void tab1() {

    area1 = new JTextArea();
    area2 = new JTextArea();

    scrollPane1 = new JScrollPane(area1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane2 = new JScrollPane(area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    centerPanel = new JPanel(new GridLayout(1, 2));
    centerPanel.add(scrollPane1);
    centerPanel.add(scrollPane2);

    tab1Panel = new JPanel(new BorderLayout());
    tab1Panel.add(centerPanel, BorderLayout.CENTER);
    tab1Panel.add(southPanel, BorderLayout.SOUTH);
}

private void tab2() {

    centerPanel = new JPanel();
    centerPanel.add(new JLabel("this is for drawing"));

    tab2Panel = new JPanel(new BorderLayout());
    tab2Panel.add(centerPanel, BorderLayout.CENTER);
    tab2Panel.add(southPanel, BorderLayout.SOUTH);
}

private void southPanel() {

    ok = new JButton("Ok");
    quit = new JButton("Avsluta");

    check_lbl = new JLabel("Change");
    input_lbl = new JLabel("Document");

    input = new JTextField("");
    input.setEditable(false);

    check = new JCheckBox();

    inputPanel = new JPanel(new BorderLayout());
    inputPanel.add(input_lbl, BorderLayout.WEST);
    inputPanel.add(input, BorderLayout.CENTER);

    checkPanel = new JPanel(new BorderLayout());
    checkPanel.add(check_lbl, BorderLayout.WEST);
    checkPanel.add(check, BorderLayout.CENTER);

    southPanel = new JPanel(new GridLayout(1, 4));
    southPanel.add(quit);
    southPanel.add(ok);
    southPanel.add(inputPanel);
    southPanel.add(checkPanel);
}

@Override
public void actionPerformed(ActionEvent aE) {

    String theEvent = aE.getActionCommand();

    if (theEvent.equals("Avsluta")) {
        System.exit(0);
    } else if (theEvent.equals("Ok")) {

        System.out.println("ok!");
    }   
}

@Override
public void itemStateChanged(ItemEvent iE) {

    if (iE.getStateChange() == iE.SELECTED) {
        input.setEditable(true);
    } else {
        input.setEditable(false);
    }
}

private void setListeners() {

    check.addItemListener(this);
    ok.addActionListener(this);
    quit.addActionListener(this);
}   

}

Edit: Maybe I should put the question in another way - How could I make these two tabs share the same southPanel?

Upvotes: 0

Views: 508

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

How could I make these two tabs share the same southPanel?

The short answer is, you can't. A component can only reside within a single container. You would need to create two distinct instances of your southPanel for each tab

A better solution, would be separate the components into re-usable components (ie make the southPanel it's own component) and contain the functionality within in. This way, you can re-create it simply and even modify it's functionality through inheritance.

Upvotes: 2

Related Questions