views:

69

answers:

5

What would be the easiest way for creating a dialog:
- in one window I'm giving data for envelope addressing, also set font type from list of sizes
- when clicked OK, in the same window or in next window I get preview of how the envelope would look like with the given names, and used selected font size

It should look similarly to this :

alt text

Should I use Jdialog ? Or will JOptionPane will be enough ? The next step will be to choose color of font and background so I must keep that in mind.

A: 

You will need to use JDialog. No point messing about with JOptoinPane - it's not meant for gathering more than a simple string. Additionally, use either MigLayout, TableLayout, or JGoodies forms - it will help you get a nice layout that's easy to code.

mdma
A: 

If it is allowed to use a GUI builder I would recommend you IntelliJ IDEA's

You can create something like that in about 5 - 10 mins.

If that's not possible ( maybe you want to practice-learn-or-something-else ) I would use a JFrame instead ) with CardLayout

Shouldn't be that hard to do.

OscarRyz
+2  A: 
aioobe
+1 But you shouldn't inherit a class if you're not adding new behavior ( ie re-implment the `paintComponent` method ) and simple composition is enough. ie `JDialog d = new JDialog(frame, "Envelope addressing"); d.add(new JLabel("Name");...etc`
OscarRyz
That's a good point!
aioobe
A: 

You can use JOptionPane. You can add any Swing component to it.

Create a panel with all the components you need except for the buttons and then add the panel to the option pane. The only problem with this approach is that focus will be on the buttons by default. To solve this problem see the solution presented by Dialog Focus.

camickr
A: 

If you need to use JOptionPane :

import java.awt.*;
import javax.swing.*;

public class Main extends JFrame {

    private static JTextField nameField = new JTextField(20);
    private static JTextField surnameField = new JTextField();
    private static JTextField addr1Field = new JTextField();
    private static JTextField addr2Field = new JTextField();
    private static JComboBox sizes = new JComboBox(new String[] { "small", "medium", "large", "extra-large" });

    public Main(){
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        getContentPane().add(mainPanel);

        JPanel addrPanel = new JPanel(new GridLayout(0, 1));
        addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver"));
        addrPanel.add(new JLabel("Name"));
        addrPanel.add(nameField);
        addrPanel.add(new JLabel("Surname"));
        addrPanel.add(surnameField);
        addrPanel.add(new JLabel("Address 1"));
        addrPanel.add(addr1Field);
        addrPanel.add(new JLabel("Address 2"));
        addrPanel.add(addr2Field);
        mainPanel.add(addrPanel);
        mainPanel.add(new JLabel(" "));
        mainPanel.add(sizes);

        String[] buttons = { "OK", "Cancel"};

        int c = JOptionPane.showOptionDialog(
                null,
                mainPanel,
                "My Panel",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                buttons,
                buttons[0]
         );

        if(c ==0){
            new Envelope(nameField.getText(), surnameField.getText(), addr1Field.getText()
                    , addr2Field.getText(), sizes.getSelectedIndex());
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }
}
owca
Same thing here. There is no need to extend JFrame when this was possible: JFrame frame = new JFrame(); ... ; frame.getContentPane().add( mainPanel ); Inheritance is the strongest form of coupling.
OscarRyz
I suggested using the JOptionPane 7 hours before owca's suggestion. I see you expect to be spoon fed code. Good to know I won't bother helping in the future. Why do people continue to spoon feed code when it is completely unnecessary? All that has happened is that Mike hasn't had to do any thinking on his own and is now taking up our valuable time with a follow up question: stackoverflow.com/questions/2926182/…. This is what happens when you spoon feed simple code. No incentive to learn
camickr