I am a newbie at Java Swing/AWT and I have this code following working for a simple PopUp dialog which closes on any of the JButtons beind clicked, but displays real wonky. Does anybody have suggestions on what and how to fix?
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Box.Filler;
public class UpgradePopupWindow extends JPanel implements ActionListener {
//public static UpgradePopupWindow mainWindow;
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;
}
private UpgradePopupWindow(JFrame frame, Object ft) {
flash = (FlasherThread)ft;
String text = "An error occurred during the attempt to update your software. We recommend the following: (1) Restore your device to its previous version, (2) back up important data, and then (3) try updating your device again. If you continue with the current update, only your previously backed-up data will be available.";
addFiller(5);
addLabel(text, Font.PLAIN, 12);
//addText(text, true, Font.PLAIN);
addFiller(20);
newFrame = frame;
flashMe = new JButton(upgrade);
flashMe.setActionCommand("upgrade");
flashMe.addActionListener(this);
flashMe.setEnabled(true);
add(flashMe);
helpMe = new JButton(restore);
helpMe.setActionCommand("restore");
helpMe.addActionListener(this);
helpMe.setEnabled(true);
add(helpMe);
setOpaque(true);
newFrame.setContentPane(this);
}
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 static void createGUI(Object obj) {
//Create and set up the frame.
JFrame frame = new JFrame("PopUp Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 200));
//create and setup the content pane
UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(frame, obj);
popUpContentPane.setOpaque(true);
frame.setContentPane(popUpContentPane);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if("restore".equals(e.getActionCommand())) {
System.out.println("restore button selected");
flash.setUpgradeRestoreChoice("restore");
newFrame.dispose();
} else if ("upgrade".equals(e.getActionCommand())) {
System.out.println("upgrade button selected");
flash.setUpgradeRestoreChoice("upgrade");
newFrame.dispose();
}
}
}alt text