-1

Java: I have two classes. The first BaseForm(BaseForm.java) extends JFrame, the second ErrorM(ErrorM.java) extends BaseForm. I want to close ErrorM by a click on OK button.

I know I am supposed to use the setVisible(false) but I do not know what is the object to invoke it.

The classes:

package interfaceClasses;

import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.Color;

public class BaseForm extends JFrame {

    public BaseForm() throws HeadlessException {
        Dimension d = new Dimension(1100, 850);

            setTitle("Employee Payroll System");
            setSize(d);
            setLocationRelativeTo(null);
            getContentPane().setBackground(new Color(107, 142, 35));
            getContentPane().setLayout(null);
            setBackground(new Color(107, 142, 35)); 
        }
    }

and

package interfaceClasses;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ErrorM extends BaseForm{
    private final JPanel contentPanel = new JPanel();

    public ErrorM(String errT, String errM) {
        setTitle("Error");
        setLocationRelativeTo(null);


        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBackground(new Color(107, 142, 35));
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JLabel errorText = new JLabel("Error Text 1");
            errorText.setBounds(6, 6, 438, 53);
            errorText.setHorizontalAlignment(SwingConstants.CENTER);
            errorText.setForeground(Color.WHITE);
            errorText.setFont(new Font("Helvetica Neue", Font.PLAIN, 20));
            errorText.setText(errT);
            contentPanel.add(errorText);
        }
        {
            JLabel errorMessage = new JLabel("Error Text 2");
            errorMessage.setBounds(6, 67, 438, 166);
            errorMessage.setHorizontalAlignment(SwingConstants.CENTER);
            errorMessage.setForeground(Color.WHITE);
            errorMessage.setFont(new Font("Helvetica Neue", Font.BOLD, 15));
            errorMessage.setText(errM);
            contentPanel.add(errorMessage);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                    }
                });
                okButton.setFont(new Font("Helvetica Neue", Font.BOLD, 15));
                okButton.setForeground(new Color(85, 107, 47));
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
                }
            }
        }
}

Thanks for your answers

Zini
  • 909
  • 7
  • 15
Georgino
  • 3
  • 2
  • 6
    I don't know, but this seems like something that could easily be accomplished with a `JOptionPane`. Also I am almost certain you are incorrectly/unnecessarily using inheritance in this context. – Paul Samsotha Apr 25 '14 at 12:11
  • You are going about building a Swing application all wrong. Perhaps this article, [Qlocktwo with Java Swing](http://java-articles.info/articles/?p=555) will help you to see how to put a Swing application together. – Gilbert Le Blanc Apr 25 '14 at 13:56
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Apr 25 '14 at 14:24

1 Answers1

0
JOptionPane.showMessageDialog(null, "This is an error message!",
                             "This is the title!", JOptionPane.ERROR_MESSAGE);

From memory that is the method that I use in order to show error messages (very straightforward and simple).

Though if you need to close dialogs or frames, you can use the dispose() method on that object...

I hope this helps...

Hunsu
  • 3,281
  • 7
  • 29
  • 64
Rich Pickering
  • 237
  • 2
  • 6