views:

37

answers:

2

I'm fairly new to Java Swing/AWT et al and hence the question. I have a simple dialog with some text and a couple JButton. I am using the GridbagLayout for this dialog.

But when I look at the dialog, the JButtons are kind of having a weird shape and I cannot seem to right justify the text either.

Is this a limitation of the Layout I am using. Is there much simpler and elegant solution for my problem with either the buttons or the textlayout?

A screenshot of the dialog is attached below.

The code for my dialog Class is:

 public class UpgradePopupWindow extends JPanel implements ActionListener {
static final long serialVersionUID = 0;


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

JPanel panels;
JButton upgradeButton;
JButton restoreButton;
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;
}

protected JTextArea addMultiLineLabel(String text, int fontStyle, int fontSize, Object constraints) {

    JTextArea textArea = new JTextArea(text);

    textArea.setFont(new Font("SansSerif", fontStyle, fontSize));

    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setEditable(false);
    textArea.setBackground(new Color(0, 0, 0, 0)); // Zero alpha = transparent background
    textArea.setOpaque(false);
    textArea.setBorder(new TitledBorder(""));
    textArea.setAlignmentX(Component.CENTER_ALIGNMENT);

    add(textArea, constraints);

    return textArea;
}


private UpgradePopupWindow(JFrame frame, Object ft) {
    super(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    flash = (FlasherThread)ft;
    String text = "An error occurred during the attempt to update your device's 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);
    gbc.gridy = 0;
    gbc.gridx = 0;
    gbc.gridwidth = 2;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    addMultiLineLabel(text, Font.PLAIN, 12, gbc);

    //addText(text, true, Font.PLAIN);
    addFiller(20);
    newFrame = frame;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.NONE;

    upgradeButton = new JButton(upgrade);

    upgradeButton.setActionCommand("upgrade");
    upgradeButton.addActionListener(this);
    upgradeButton.setEnabled(true);
    upgradeButton.setBackground(Color.WHITE);
    upgradeButton.setForeground(Color.GRAY);
    add(upgradeButton,gbc);

    ++ gbc.gridx;
    restoreButton = new JButton(restore);
    restoreButton.setActionCommand("restore");
    restoreButton.addActionListener(this);
    restoreButton.setEnabled(true);
    //restoreButton.setForeground(Color.DARK_GRAY);
    restoreButton.setBackground(Color.DARK_GRAY);
    add(restoreButton,gbc);
    setOpaque(true);
    newFrame.setContentPane(this);
    //newFrame.getContentPane().setBackground(Color.LIGHT_GRAY);
}

protected void addFiller(int size) {
    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(440, 180));
    //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][1]

alt text

+1  A: 

There are a lot of much more elegant solutions out there .. ;) You should really never use GridbagLayout if not absolutely required (or only with a gui editor)! It's simply a pain in the ass! Rather I suggest using one of the following LayoutManagers:

MigLayout: http://www.miglayout.com/

TableLayout: https://tablelayout.dev.java.net/

Creating a pleasing layout is much simpler and intuitive than with the GridbagLayout and its configuration overhead.

Javaguru
like i said, im new to the whole Java Swing/AWT. So I wasnt even aware of a MigLayout existing. SO how would you say implement this simple dialog in a tableLayout or a GridLayout?
LambeauLeap
A: 

As @Javaguru said - use MigLayout. For the text DO NOT use JTextArea. This component is mostly for text editing. JLabel is enough for your purpose - you can even use HTML inside of it.

You don't have to create this kind of dialog from scratch. There are many options. One of them is my Swing TaskDialog framework ;)

But if you still insist on implementing it yourself here a simple implementation using MigLayout ( even with MigLayout it can be done using several differnt ways):

    import java.awt.Color;
    import java.awt.Dimension;

    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;

    import net.miginfocom.swing.MigLayout;

public class TestDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private final JLabel label = new JLabel("Your text here");
    private final JButton restoreButton = new JButton("Restore Previous Version");
    private final JButton ugradeButton = new JButton("Continue Upgrade");



    public TestDialog( String title ) {
        super( (JDialog)null, title );
        setContentPane(createContent());
    }

    private JPanel createContent() {
        JPanel content = new JPanel();
        content.setPreferredSize( new Dimension(400, 100));
        content.setBackground(Color.WHITE);
        content.setLayout(new MigLayout("", "[400px,grow][pref!]", "[grow][pref!]"));
        this.label.setVerticalAlignment(SwingConstants.TOP);
        content.add(this.label, "cell 0 0 2 1,grow");
        content.add(this.restoreButton, "cell 1 1,alignx left,aligny top");
        content.add(this.ugradeButton, "cell 0 1,alignx right,aligny top");
        return content;
    }


    // very simplified test
    public static void main(String[] args) {

        TestDialog dlg =  new TestDialog("Popup Dialog");
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.pack();
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);

    }

}
eugener
I'm doing development a Linux box and my java compiler struggling importing net.miginfocom.swing.MigLayout; library.I cannot even list MigLayout with the rest of the rest of JAVA API, am I missing something?
LambeauLeap
download the latest library jar from http://www.migcalendar.com/miglayout/versions/You need the one for Swing
eugener