0

i have added a JTabbedPane with a JPanel in each tab. and a JText area within each JPanel. the tabs can be dynamically created in the same template.

There is also a menu bar with a menu. it has options to replace an occurance of a string (eg replace "<" with "<") it worked perfectly when i just used a JPanel and textArea.

Now that i hav added the tabbedPane,... i dont know how to replace the content of the active tab alone,..

i have tried getting the selected component(getSelectedComponent() method and getComponentAt() method) and replacing the text,.. i didnt work

can some one help me

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

8

getSelectedIndex() and getSelectedComponent() should work. Check out How to Use Tabbed Panes tutorial, it has good examples.

EDIT: demo of getSelectedComponent and AbstractAction

import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

public class TabbedPaneDemo {

    static class TextDemoPanel extends JPanel{
        private JTextArea textArea;

        public TextDemoPanel(String text){
            textArea = new JTextArea(5, 20);
            textArea.setText(text);
            JScrollPane scrollPane = new JScrollPane(textArea);

            add(scrollPane);
        }

        public JTextArea getTextArea() {
            return textArea;
        }
    }

    static class SetTextAction extends AbstractAction {
        private JTabbedPane tabbedPane;

        public SetTextAction(JTabbedPane tabbedPane){
            super("Set text");
            this.tabbedPane = tabbedPane;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String value = JOptionPane.showInputDialog(tabbedPane, "Text", "New text");
            if (value != null){
                TextDemoPanel panel = (TextDemoPanel)tabbedPane.getSelectedComponent();
                if (panel != null)
                    panel.getTextArea().setText(value);
            }
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TabbedPaneDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTabbedPane tabbedPane = new JTabbedPane();

        tabbedPane.addTab("Tab 1", new TextDemoPanel("Tab 1 text"));
        tabbedPane.addTab("Tab 2", new TextDemoPanel("Tab 2 text"));
        tabbedPane.addTab("Tab 3", new TextDemoPanel("Tab 3 text"));

        frame.add(tabbedPane, BorderLayout.CENTER);

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        menuBar.add(menu);
        JMenuItem item = new JMenuItem(new SetTextAction(tabbedPane));

        menu.add(item);

        frame.setJMenuBar(menuBar);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • 1
    +1 Also consider [`Action`](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) to encapsulate the functionality. – trashgod Jul 21 '12 at 17:29
  • getSelectedComponent will return the top level component of the tab. In this excellent demo, that would be the JScrollPane. You're then going have to extract he JTextArea from it yourself – MadProgrammer Jul 21 '12 at 20:32
  • @MadProgrammer in the code above it actually returns `TextDemoPanel`. – tenorsax Jul 21 '12 at 20:44
  • @Max Sorry, you're absolutely right - 3 month baby just woke us up for there early morning feed :P – MadProgrammer Jul 21 '12 at 20:47
  • @MadProgrammer no worries, hope you get back to sleep soon :) – tenorsax Jul 21 '12 at 22:17