Member Avatar

I am making my own component similar to the JOptionPane, but with some added GUI components specialised for my current project. I have finished with the GUI of the component, but I do not know how to make wait for the user to enter input before continuing, as the JOptionPane does... I want my component to exit when my user provides input. I've also attempted at making it a singleton class to ensure that there can only be one visible at a time. My code goes as follows:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class NewChatter extends JFrame {

    private static NewChatter instance  = new NewChatter();

    private JTextField text;
    private JComboBox<String> box;
    private JButton ok,
    private JPanel pnl1,
    private JLabel lbl;

    private Dimension size = new Dimension(600, 160);
    private static String[] names,
    private static String address = "";

        //constructor sets up the interface
        //all the components have action listeners
        //each value assigns a value to the "address" variable
        /* Components: 
        * 1 JTextField
        * 1 JComboBox
        * 2 JButtons (ok, cancel)
        * 3 JPanels to hold the different components

    public static NewChatter getInstance(){
        return instance;

    public String showInput(){   //this method is called in the main program

        if(!address.equals("--")){   //my attempt at making the program wait
            String hold = address;
            address = "--";
            return hold;
            //Tried to use recursive programming

        return address;   //supposed to return the address only, 
    }                     //once the user has entered a value

Recommended Answers

Use a modal JDialog to hold the components (see API doc for details)

Jump to Post

All 2 Replies

Member Avatar

Thanks, didn't think of using JDialog since I have only used JFrame so far

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.