views:

27

answers:

1

First off, I am real new to Java Swing/AWT and not intricately familiar with the workings of JFrame, JPanel & JButton. I want to just display a simple PopUp dialog with a some text and a couple JButtons and it should exit when any of the two buttons are clicked. I have most of that logic in this class, however I'm still struggling to make the dialog show :(

Any ideas what I might be doing wrong:

public class UpgradePopupWindow extends JFrame implements ActionListener {

static final long serialVersionUID = 0;


final String upgrade = "Continue Upgrade";
final String restore = "Restore";

JPanel panels;
JButton flashMe;
JButton helpMe;
JTextArea Message;
JFrame newFrame;
FlasherThread flash;

  protected JTextArea addText(String text, boolean visible, int fontStyle) {

    JTextArea textArea = new JTextArea(text);

    textArea.setFont(new Font("SansSerif", fontStyle, 12)); //$NON-NLS-1$

    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setEditable(false);
    textArea.setBackground(Color.DARK_GRAY);
    textArea.setForeground(Color.WHITE);
    textArea.setOpaque(false);
    textArea.setVisible(visible);
    textArea.setAlignmentX(Component.CENTER_ALIGNMENT);

    add(textArea);

    return textArea;
}

public UpgradePopupWindow(Object ft) {
    super("PopUp Dialog");
    flash = (FlasherThread)ft;
    String text = "An error occurred during the attempt to update your software. We recommend the following: (1) Restore your phone to its previous version,  If you continue with the current update, only your previously backed-up data will be available.";
    //addFiller(5);
    JLabel label = addLabel(text, Font.PLAIN, 12);
    //addText(text, true, Font.PLAIN);
    //addFiller(20);
    //newFrame = frame;
    JPanel cards = new JPanel();
    cards.setLayout(null);
    cards.setOpaque(false);

    cards.setBounds(400, 200, 250, 150);
    cards.add(label);
    flashMe = new JButton(upgrade);

    flashMe.setActionCommand("upgrade");
    flashMe.addActionListener(this);
    flashMe.setEnabled(true);
    cards.add(flashMe);
    //add(flashMe);


    helpMe = new JButton(restore);
    helpMe.setActionCommand("restore");
    helpMe.addActionListener(this);
    helpMe.setEnabled(true);
    //add(helpMe);
    cards.add(helpMe);
    setContentPane(cards);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    //setOpaque(true);
    //newFrame.setContentPane(this);
    pack();
    //setVisible(true);
}

  protected JLabel addLabel(String text, int fontStyle, int size) {
    JLabel label = new JLabel(text);
    label.setFont(new Font("SansSerif", fontStyle, size)); 
    label.setAlignmentX(Component.CENTER_ALIGNMENT);
    label.setOpaque(false);
    label.setVisible(true);
    //label.setForeground(Color.BLUE);

    //add(label);
    return label;
}

  protected void addFiller(int size) {
    /*
     * create some space before the progress bar
     */
    Dimension diminsion = new Dimension(size, size);
    Filler filler = new Filler(diminsion, diminsion, diminsion);
    filler.setAlignmentX(Component.CENTER_ALIGNMENT);

    add(filler);
}

  public void actionPerformed(ActionEvent e) {

    if("restore".equals(e.getActionCommand())) {
        System.out.println("restore button selected");
        //flash.setUpgradeRestoreChoice("restore");
        //newFrame.dispose();
        dispose();
    } else if ("upgrade".equals(e.getActionCommand())) {
        System.out.println("upgrade button selected");
        flash.setUpgradeRestoreChoice("upgrade");
        //newFrame.dispose();
        dispose();
    }
}

 }
+2  A: 

You should use a JOptionPane instead of rolling your own.

davetron5000