0

In my Food Tab, I wanted to achieve this
Click Here

But I only able to get this
Click Here

How can I increase the width of the JTextField which are in Food Tab ? Below is my code

public class FoodOrdering {

    static private JFrame frame;
    static private JTextField textField;
    static private GridBagConstraints gbc;
    static private JLabel[] foodLabel;
    static private JLabel[] labels;
    static private JTextField[] qtyField;
    static private JLabel[] foodImage;
    static private File[] file;
    private static final int ELEMENTS = 9;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FoodOrdering window = new FoodOrdering();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     * 
     * @throws IOException
     */
    public FoodOrdering() throws IOException {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     * 
     * @throws IOException
     */
        static void initialize() throws IOException {
        frame = new JFrame();
        frame.setBounds(100, 100, 700, 550);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setLocationRelativeTo(null);

        JLabel lblFoodOrdered = new JLabel("Food Ordered");
        lblFoodOrdered.setBounds(529, 11, 81, 14);
        frame.getContentPane().add(lblFoodOrdered);

        TextArea textArea = new TextArea();
        textArea.setBounds(462, 31, 199, 275);
        frame.getContentPane().add(textArea);

        JLabel lblTotal = new JLabel("Total  : ");
        lblTotal.setBounds(519, 315, 46, 14);
        frame.getContentPane().add(lblTotal);

        textField = new JTextField();
        textField.setBounds(575, 312, 86, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JButton btnOrder = new JButton("Order");
        btnOrder.setBounds(521, 352, 89, 23);
        frame.getContentPane().add(btnOrder);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        addIt(tabbedPane, "Foods");
        addIt1(tabbedPane, "Drinks");
        addIt1(tabbedPane, "Desserts");
        tabbedPane.setBounds(23, 11, 400, 450);
        frame.getContentPane().add(tabbedPane);

        frame.setVisible(true);
    }

    static void addIt1(JTabbedPane tabbedPane, String text) {
        JLabel label = new JLabel(text);
        JButton button = new JButton(text);
        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(button);
        tabbedPane.addTab(text, panel);
    }

    static void addIt(JTabbedPane tabbedPane, String text) throws IOException {
        JPanel panel = new JPanel(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(1, 1, 1, 1);
        foodImage = new JLabel[ELEMENTS];
        foodLabel = new JLabel[ELEMENTS];
        labels = new JLabel[ELEMENTS];
        qtyField = new JTextField[ELEMENTS];
        file = new File[ELEMENTS];

        try {
            file[0] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
            file[1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
            file[2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
            file[3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
            file[4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
            file[5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
            file[6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
            file[7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
            file[8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");

            foodLabel[0] = new JLabel("Salad");
            foodLabel[1] = new JLabel("Japanese Noodles");
            foodLabel[2] = new JLabel("Spaghetti");
            foodLabel[3] = new JLabel("Spaghetti Meat Balls");
            foodLabel[4] = new JLabel("Noodles");
            foodLabel[5] = new JLabel("Kids Spaghetti");
            foodLabel[6] = new JLabel("Chicken Rice");
            foodLabel[7] = new JLabel("Thai Food");
            foodLabel[8] = new JLabel("Vietnam Food");

        } catch (Exception e) {
            e.printStackTrace();
        }

        for (int i = 0; i < ELEMENTS; i++) {
            Image image = ImageIO.read(file[i]);
            Image imageScaled = image.getScaledInstance(80, 95, Image.SCALE_SMOOTH);
            ImageIcon imageIcon = new ImageIcon(imageScaled);
            qtyField[i] = new JTextField(3);
            foodImage[i] = new JLabel(imageIcon);
        }

        gbc.gridx = 0;
        for (int i = 0; i < ELEMENTS; i++) {
            if (i % 3 == 0) {
                gbc.gridy += 2;
                gbc.gridx = 0;
            }
            panel.add(foodImage[i], gbc);
            gbc.gridy++;
            panel.add(foodLabel[i], gbc);
            gbc.gridy--;
            gbc.gridx++;
            panel.add(qtyField[i], gbc);
            gbc.gridx++;
            tabbedPane.addTab(text, panel);
        }

    }

    public void setVisible(boolean b) throws IOException {

    }
}

Edited by John_165

2
Contributors
3
Replies
27
Views
6 Months
Discussion Span
Last Post by JamesCherrill
0

You have made the fundamental mistake of using a null Layout Manager. By hard-coding pixel coordinates you eliminate any portability to computers with screen resolutions or fonts different from yours. How this is going to play with using a GridBagLayout at the same time is anybody's guess.

Line 142 - is the -- intentional?

0

Line 142 - is the -- intentional?

What did you meant ?

Edited by John_165

0

You have a gbc.gridy--; on line 142. It's unusual to see code that loads a form from the bottom upwards, also I can't quite see how that works with the +=2 every third pass on line 136. It seems that every 3 passes you add 2 but subtract 1 three times, so won't gridy go negative? (-1 for grid coordinates means "relative").
I'm not saying its wrong, just that it looked odd to me.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.