Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:47:50+00:00 2026-06-09T09:47:50+00:00

I have written the below code which has a text-field and a button. as

  • 0

I have written the below code which has a text-field and a button. as soon as a character is entered and the button is pressed a tab is created with the title same as what is entered in the field.

Several tabs can be create the same way…..now in the new tab again, a text-field and a button exist a long with a text pane to show the result….

I want to show the text entered to the text-field in its text-pane in each tab…

Now please lead me learn how and where I put the listener for the button of the tab…. and recommend any other required Listeners(I think there should be another Listener to direct me to the focused or selected tab).

It should be mentioned that I have added these tabs to an array-list for any reuse but I don’t know if I did right or how I can use it?

package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class TestGUI extends JFrame {


    private JTextField jTextField1;
    private JButton jButton1;
    static ArrayList<JPanel> ary = new ArrayList<JPanel>();
    private int tabIndex = 0;
    static int index = 0;
    private JTabbedPane tabbedPane;

    /**
    * @param args
    */
    public TestGUI() {

        super("Testing Tab Frame");
        setLayout(null);

        Handler but1 = new Handler();

        jTextField1 = new JTextField();
        jTextField1.setVisible(true);
        jTextField1.setBounds(12, 12, 85, 30);
        add(jTextField1);

        jButton1 = new JButton("Button1");
        jButton1.setVisible(true);
        jButton1.setBounds(130, 12, 85, 30);
        add(jButton1);
        jButton1.addActionListener(but1);

        tabbedPane = new JTabbedPane();
        tabbedPane.setBounds(12, 54, 200, 150);
        tabbedPane.setVisible(false);
        add(tabbedPane);
        pack();
        setSize(250, 110);
        setLocationRelativeTo(null);

    }

    private class Handler implements ActionListener {

        public void actionPerformed(ActionEvent evt) {
            String input = jTextField1.getText();
            if (!input.isEmpty()) {
                setSize(250, 250);
                JPanel inst = createPanel(input);
                inst.setVisible(true);
                tabbedPane.addTab(Integer.toString(tabIndex), inst);
                tabbedPane.setVisible(true);
            }

        }
    }

    protected JPanel createPanel(String input) {
        JPanel inst = new JPanel();
        inst.setVisible(true);
        JTextField textField = new JTextField();
        textField.setVisible(true);
        textField.setBounds(12, 12, 80, 30);
        JButton button = new JButton();
        button.setVisible(true);
        button.setBounds(100, 12, 80, 30);
        JTextPane textPane = new JTextPane();
        textPane.setBounds(12, 54, 168, 40);
        inst.add(textPane);
        textPane.setVisible(true);
        inst.setLayout(null);
        inst.add(button);
        inst.add(textField);
        ary.add(inst);
        tabIndex = index;
        index++;
        return inst;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGUI inst = new TestGUI();
        inst.setVisible(true);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:47:52+00:00Added an answer on June 9, 2026 at 9:47 am

    You would add the ActionListener to the button inside your createPanel method. So your method would be something like this (making some assumptions about what you actually want to do with the text since it wasn’t clear):

    protected JPanel createPanel(String input) {
        JPanel inst = new JPanel();
        inst.setVisible(true);
        final JTextField textField = new JTextField();
        textField.setVisible(true);
        textField.setBounds(12, 12, 80, 30);
        JButton button = new JButton();        
        button.setVisible(true);
        button.setBounds(100, 12, 80, 30);
        final JTextPane textPane = new JTextPane();
        textPane.setBounds(12, 54, 168, 40);
        inst.add(textPane);
        textPane.setVisible(true);
    
        button.addActionListener(new ActionListener(){
    
            @Override
            public void actionPerformed(ActionEvent arg0) {
                textPane.setText(textPane.getText() + textField.getText());
            }});
    
        inst.setLayout(null);
        inst.add(button);
        inst.add(textField);
        ary.add(inst);
        tabIndex = index;
        index++;
        return inst;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have written such a code below but it returns wrong output which
I have written the java code below, which executes another java program named Newsworthy_RB.
I have a small piece of code, written in python which has a Generate
Hi I have written below code function unique(th){ var obj = {}; for(var i
I am trying to fetch wcf service from jquery. I have written below code
I have written grammar for a language (sample code below) //this is a procedure
I have written a very simple WCF service, that worked fine (code below), then
I have written a MapReduce program, code is below: import java.io.IOException; import java.util.Iterator; import
I have the code below which I am using to upload files to my
I have written code which saves a user's answers as well as a recorded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.