NOOBprogrammer
NOOBprogrammer

Reputation: 71

Actionlistener for the state of a JDialog

I want to change the text of the labels in the JDialog when i click the JButton since the labels are on the other class i don't know how would i access it from the frame class. So i have come up with an idea of action listeners that check the state of the dialog. - when the JDialog is visible retrieve this data and setText this data to the labels. Is this possible?

Here's the code of my room class.

public void rooms()
    {
        bh = new ButtonHandler();
        presidentialRoom = new JButton[presidentialRoomNo.length];      
        deluxeRoom = new JButton[deluxeRoomNo.length];
        classicRoom = new JButton[classicRoomNo.length];
        for(int x = 0;x<classicRoomNo.length;x++){
            //classic rooms
            ImageIcon imageC = new ImageIcon("C:\\Users\\John\\workspace" +
                    "\\SystemTest\\src\\Images\\classicRooms.JPG"); // image
            classicRoom[x] = new JButton(classicRoomNo[x],imageC);
            classicRoom[x].setBackground(Color.WHITE);
            classicRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            classicRoom[x].addActionListener(bh);
            classicSubPanel.add(classicRoom[x]);
            //deluxe rooms
            ImageIcon imageD = new ImageIcon("C:\\Users\\John\\workspace" +
                    "\\SystemTest\\src\\Images\\deluxeRooms.JPG"); // image
            deluxeRoom[x] = new JButton(deluxeRoomNo[x],imageD);
            deluxeRoom[x].setBackground(Color.WHITE);
            deluxeRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            deluxeRoom[x].addActionListener(bh);
            deluxeSubPanel.add(deluxeRoom[x]);
            //presidential rooms
            ImageIcon imageP = new ImageIcon("C:\\Users\\John\\workspace" +
                    "\\SystemTest\\src\\Images\\presidentialRooms.JPG"); // image
            presidentialRoom[x] = new JButton(presidentialRoomNo[x],imageP);
            presidentialRoom[x].setBackground(Color.WHITE);
            presidentialRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            presidentialRoom[x].addActionListener(bh);
            presidentialSubPanel.add(presidentialRoom[x]);

        }
    }

each button here access the RoomProfile class

public class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {       
            RoomProfile rooms = new RoomProfile();
                room.setVisible(true);
        }
    }

heres the a piece of code from RoomProfile:

public void createLabels()
    {
        labels = new JLabel[topTextLabels.length];
        inputLabels = new JLabel[topTextLabels.length];
        for(int x = 0; x<topTextLabels.length;x++)
        {
            labels[x] = new JLabel(topTextLabels[x]);
            labels[x].setForeground(Color.WHITE);
            inputLabels[x] = new JLabel("test");
            inputLabels[x].setForeground(Color.WHITE);
        }
    }

The text the i want to change is the "inputLabels[]" when ever click the buttons from the room class i want to let the user see the profile of that room.

The input label will show the data from the database.

Upvotes: 2

Views: 1657

Answers (1)

Thorn
Thorn

Reputation: 4057

When making a custom dialog window, I usually extend JDialog and then add whatever functionality I need, as suggested by AKJ.

Normally whatever needs to be displayed in the dialog should be determined before it is displayed. A dialog is usually modal (meaning that when visible no other part of your user interface can be used). You can make it non-modal, but the fact that they are often modal illustrates the way dialog boxes are usually used. The user chooses some options, clicks o.k., and the dialog is gone. If the Dialog is to remain visible and is a main component of your user interface, then I think it makes more sense to use a JFrame instead of a JDialog.

To check if a dialog is visible, use if(yourDialog.isVisible())

Below is an example of a non-modal dialog I wrote:

/**
 * This class allows for some plain text (a note) to be placed into a ScrollPane inside
 * a dialog.  The dialog is non-modal.
 * @author L. LaSpina
 */
public class NoteViewDialog extends javax.swing.JDialog {

    int returnValue;
    public static final int OKPRESSED = 1;
    public static final int CANCELPRESSED = 2;

    /** Creates new form NoteDialog */
    public NoteViewDialog(java.awt.Frame parent, String note) {
        super(parent, false);
        initComponents();
        this.setTitle("Faculty Assignment Summary");
        setLabel("Error List");
        setNote(note);
        setSize(500,300);       
    }

    public void setNote(String s) {
        textArea.setText(s);
    }
    public String getNote() {
        return textArea.getText();
    }

    public void setLabel(String s) {
        headingLabel.setText(s);
    }

    public int getReturnValue() {
        return returnValue;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        headingLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        textArea.setColumns(20);
        textArea.setLineWrap(true);
        textArea.setRows(5);
        textArea.setWrapStyleWord(true);
        scrollPane.setViewportView(textArea);

        getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);

        okButton.setText("Close");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        getContentPane().add(buttonPanel, java.awt.BorderLayout.PAGE_END);

        headingLabel.setText("Error Report");
        getContentPane().add(headingLabel, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>                        

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.dispose();
    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JLabel headingLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea textArea;
    // End of variables declaration                   
}

Upvotes: 2

Related Questions