3

I'm new to Java. So I copied source code on the internet and changed it to what I wanted.

I'm developing a point of sales system in java and already done with a cash register system.

I wanted to make a table arrangement thing like if someone clicks a Table1 button, they can go to the cash register page and continue to order, payment stuffs. I didn't develop event part yet because I'm stuck at changing the size of the panels :(

I was trying to use setPreferredSize but failed.

This is the code I've been working on and I'll attach a screenshot what I want to make.

package table;

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


public class tablearray extends JApplet {

    private JPanel parentPanel, titlePanel, tablePanel, tablefor4Panel, barPanel;
    private JPanel table4andbarPanel, tablefor2Panel, bottomPanel;
    private JLabel lblTitle;
    private JButton btntable1, btntable2, btntable3, btntable4, btntable5, btntable6;
    private JButton btntable7, btntable8, btntable9;
    private JButton btnbar1, btnbar2, btnbar3, btnbar4;
    private ButtonGroup grpTablefor4, grpTablefor2, grpBar;

    /**
     * Constants
     */

    // Applet Size
    private final Dimension appletSize = new Dimension(800, 600);


    // Bottom buttons size
    //private final Dimension BUTTON_SIZE = new Dimension(50,50);            

    // flag, if the user does not enter a number in the dialog
    private final int DIALOG_NOT_NUM = -1;                            

    // flag, if the user cancels the dialog
    private final int DIALOG_CANCEL = -2;                            


     public void init()
        {
            // set applet size
            this.setSize(appletSize);

            /**
            * Create Panels
            */
            parentPanel = new JPanel();
            titlePanel = new JPanel();
            tablePanel = new JPanel();
            tablefor4Panel = new JPanel();
            barPanel = new JPanel();
            table4andbarPanel = new JPanel();
            tablefor2Panel = new JPanel();
            bottomPanel = new JPanel();


            /**
            * Set Panel Layouts
            */

            parentPanel.setLayout(new BorderLayout());
            titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
            tablePanel.setLayout(new GridLayout(1,2,50,50));
            table4andbarPanel.setLayout(new BorderLayout());
            tablefor4Panel.setLayout(new GridLayout(2,3,100,100));
            barPanel.setLayout(new GridLayout(1,4,30,30));
            tablefor2Panel.setLayout(new GridLayout(4,1,100,100));
            bottomPanel.setLayout(new BorderLayout(5,5));


            /**
            * Set Panel Borders
            */
            // Tablefor2's Border
            TitledBorder billBorder = new TitledBorder(BorderFactory.createEtchedBorder(),
                    "Tablefor2", TitledBorder.CENTER, TitledBorder.CENTER);
            tablefor2Panel.setBorder(billBorder);

            // Table's Border
            TitledBorder orderBorder = new TitledBorder(BorderFactory.createEtchedBorder(),
                    "Table management", TitledBorder.LEFT, TitledBorder.CENTER);
            tablePanel.setBorder(orderBorder);

            // Tablefor4's Border
            TitledBorder mainBorder = new TitledBorder(BorderFactory.createEtchedBorder(),
                    "Tablefor4", TitledBorder.LEFT, TitledBorder.CENTER);
            tablefor4Panel.setBorder(mainBorder);

            // Bar's Border
            TitledBorder drinkBorder = new TitledBorder(BorderFactory.createEtchedBorder(),
                    "Bar", TitledBorder.LEFT, TitledBorder.CENTER);
            barPanel.setBorder(drinkBorder);

            /**
            * Create Components
            */
            // Title
            lblTitle = new JLabel("- Tasting Room -");
            lblTitle.setFont (new Font ("Book Antiqua", Font.BOLD, 18));
            lblTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
            titlePanel.add(lblTitle);

            //Tablefor4
            grpTablefor4 = new ButtonGroup();
            btntable1 = new JButton("T1");
            btntable2 = new JButton("T2");
            btntable3 = new JButton("T3");
            btntable4 = new JButton("T4");
            btntable5 = new JButton("T5");
            btntable6 = new JButton("T6");
            btntable1.setPreferredSize(new Dimension(100,100));


            grpTablefor4.add(btntable1);
            grpTablefor4.add(btntable2);
            grpTablefor4.add(btntable3);
            grpTablefor4.add(btntable4);
            grpTablefor4.add(btntable5);
            tablefor4Panel.add(btntable1);
            tablefor4Panel.add(btntable2);
            tablefor4Panel.add(btntable3);
            tablefor4Panel.add(btntable4);
            tablefor4Panel.add(btntable5);
            tablefor4Panel.add(btntable6);



            //tablefor2
            grpTablefor2 = new ButtonGroup();
            btntable7 = new JButton("T7");
            btntable8 = new JButton("T8");
            btntable9 = new JButton("T9");
            btntable7.setPreferredSize(new Dimension(30,30));


            grpTablefor2.add(btntable7);
            grpTablefor2.add(btntable8);
            grpTablefor2.add(btntable9);
            tablefor2Panel.add(btntable7);
            tablefor2Panel.add(btntable8);
            tablefor2Panel.add(btntable9);

            // bar
            grpBar = new ButtonGroup();
            btnbar1 = new JButton("B1");
            btnbar2 = new JButton("B2");
            btnbar3 = new JButton("B3");
            btnbar4 = new JButton("B4");


            grpBar.add(btnbar1);
            grpBar.add(btnbar2);
            grpBar.add(btnbar3);
            grpBar.add(btnbar4);

            barPanel.add(btnbar1);
            barPanel.add(btnbar2);
            barPanel.add(btnbar3);
            barPanel.add(btnbar4);


             /** Add Panels
             */
            table4andbarPanel.add(tablefor4Panel, BorderLayout.NORTH);
            table4andbarPanel.add(barPanel,BorderLayout.CENTER);
            tablePanel.add(table4andbarPanel, 0);
            tablePanel.add(tablefor2Panel, 1);
            parentPanel.add(titlePanel, BorderLayout.NORTH);
            parentPanel.add(tablePanel, BorderLayout.CENTER);
            parentPanel.add(bottomPanel, BorderLayout.SOUTH);
            add(parentPanel);
        }



}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rina
  • 41
  • 3
  • 1
    I'd probably use a `GridBagLayout` for this. Is the GUI resizable? If so, how should extra space be assigned? What components are the various boxes? – Andrew Thompson Aug 28 '16 at 13:39
  • 2
    BTW 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). – Andrew Thompson Aug 28 '16 at 13:41
  • 1
    `private final Dimension appletSize = new Dimension(800, 600);` The size of an applet is set by the web page that hosts it. The applet itself should not try to force a size. That'd be like the guest in a guest house knocking down a wall to the next room because they want to party.. – Andrew Thompson Aug 28 '16 at 13:43
  • I see titled borders in the code. Are they supposed to be in the final GUI? – Andrew Thompson Aug 28 '16 at 13:49
  • hmm I found some code on the internet about what I wanted to develop and wanted to use it as a base because I had no idea how to start. and......for titled borders, I just keep it there because it was there from the beginning :( like I said, I'm so new to Java.....started learning it a week ago haha, thanks for the comments! – Rina Aug 28 '16 at 18:23
  • 1
    I'm still waiting for answers on the 3 questions in my first comment, and the question in my second comment. I cannot provide a good answer (as opposed to the `null` layout answer) until those questions are addressed. – Andrew Thompson Aug 29 '16 at 02:24
  • okay, so 'is the GUI resizable?' does it mean I'm willing to change the size? Yes, it just needs to be the similar way with the screen shot. The boxes are tables at a restaurant! (table for 4, table for 2, and bar.) and I think I answered the second comment in my first comment. English is not my first language so if I answered in a wrong way, leave comments. :) – Rina Aug 29 '16 at 16:13
  • Why code an applet? – Andrew Thompson Aug 30 '16 at 00:24
  • Well like I said I found an original code by googling and it was made with an applet by some programmer. I changed it a bit to make that I want :). – Rina Aug 30 '16 at 02:13
  • *"I found an original code by googling.."* My advice it to put it back where you found it and start fresh. There is a lot of rubbish code on the internet, and that shows distinct signs of rubbish. Applets are a lot harder to develop and debug than applications, and most browsers don't support them at all anymore. – Andrew Thompson Aug 30 '16 at 02:19
  • Ugh I was so happy to find it though :( So what parts would you recomment me refering or using? Like swing? I'm kinda confused right now watching all the discussions from my question. – Rina Aug 30 '16 at 03:43
  • That applet *does use Swing*. An AWT applet is a `java.applet.Applet` while a Swing applet is a `javax.swing.JApplet` (most components starting with `J` are Swing based). For the major part of the change - use a `JFrame` as the top level container instead of the `JApplet`. Further details can be found in the [Creating a GUI With JFC/Swing](https://docs.oracle.com/javase/tutorial/uiswing/) trail of the tutorial (which is a good place to get code examples, BTW) but unfortunately it is too long a topic to go into in comments or even a single question. – Andrew Thompson Aug 30 '16 at 03:47
  • 1
    Ah, okay. I'll try to change that parts after my classes! :) thanks for the help. It's like an eye opening of Java. – Rina Aug 30 '16 at 03:50

2 Answers2

0

This is the closest layout that I could come up with MigLayout:

package com.zetcode;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

/*
Demonstrating layout in MigLayout manager.
Author: Jan Bodnar
Website: zetcode.com
 */
public class MigLayoutBoxesEx extends JFrame {

    public MigLayoutBoxesEx() {

        initUI();
    }

    private void initUI() {

        JButton btn1 = new JButton("1");
        JButton btn2 = new JButton("2");
        JButton btn3 = new JButton("3");
        JButton btn4 = new JButton("4");
        JButton btn5 = new JButton("5");
        JButton btn6 = new JButton("6");

        JButton btn7 = new JButton("7");
        JButton btn8 = new JButton("8");
        JButton btn9 = new JButton("9");

        JButton btn10 = new JButton("10");
        JButton btn11 = new JButton("11");
        JButton btn12 = new JButton("12");
        JButton btn13 = new JButton("13");

        createLayout(btn1, btn2, btn3, btn4, btn5,
                btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13);

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        setLayout(new MigLayout("gap 10lp 15lp, ins 15lp"));

        add(arg[0], "w 90lp, h 90lp");
        add(arg[1], "w 90lp, h 90lp");
        add(arg[2], "w 90lp, h 90lp");
        add(arg[6], "w 50lp, h 50lp, growy, split 3, flowy, spany 2");
        add(arg[7], "w 50lp, h 50lp, growy");
        add(arg[8], "w 50lp, h 50lp, growy, wrap");

        add(arg[3], "w 90lp, h 90lp");
        add(arg[4], "w 90lp, h 90lp");
        add(arg[5], "w 90lp, h 90lp, wrap");

        add(arg[9], "w 50lp, h 20lp, growx, split 4, span 3");
        add(arg[10], "w 50lp, h 20lp, growx");
        add(arg[11], "w 50lp, h 20lp, growx");
        add(arg[12], "w 50lp, h 20lp, growx");

        pack();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutBoxesEx ex = new MigLayoutBoxesEx();
            ex.setVisible(true);
        });
    }
}

Screenshot:

Example screnshot

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
-2

There are a few different ways to keep your Swing components from resizing. One way is to make use of absolute positioning. This basically involves removing any layout management (GridLayout, FlowLayout, BorderLayout, etc.). If you want to keep your layout manager but also disable component resizing, you need to force your component to a static size. Keep in mind that both of these solutions will disable any component resizing when resizing the parent window.

public class TableButton extends JButton {

    private Dimension staticSize;

    public TableButton(String text, Dimension size) {
        super(text);
        staticSize = size;
    }

    @Override
    public Dimension getSize() {
        return staticSize;
    }

    @Override
    public Dimension getMinimumSize() {
        return staticSize;
    }

    @Override 
    public Dimension getMaximumSize() {
        return staticSize;
    }

    @Override 
    public Dimension getPreferredSize() {
        return staticSize;
    }

    @Override
    public int getWidth() {
        return staticSize.width;
    }

    @Override
    public int getHeight() {
        return staticSize.height;
    }
}

Dimension largeTableSize = new Dimension(80,80);
btntable1 = new TableButton("T1", largeTableSize);
btntable2 = new TableButton("T2", largeTableSize);
btntable3 = new TableButton("T3", largeTableSize);
btntable4 = new TableButton("T4", largeTableSize);
btntable5 = new TableButton("T5", largeTableSize);
btntable6 = new TableButton("T6", largeTableSize);
eighthrazz
  • 331
  • 1
  • 6
  • 3
    Don't use absolute positioning. Swing was designed to be used with layout managers. – camickr Aug 28 '16 at 18:25
  • Thanks for the comments! :) I used absolute positioning you mentioned and made it! But the thing is I want to type some numbers( like customer's number or something) on each of the buttons and show them on it. I tried to search it but just found textarea. Also, I have another java page functioning a cash register. Can I bring a total price from the cash register page to the button on table arrangement page? – Rina Aug 28 '16 at 18:29
  • @camickr whatttt? Is there any disadvantage to use absolute positioning in this situation? I want to set up mouse click event (actionperformed) and get a variable from other class( or page whatever called....). Should I make it again by using layout managers? – Rina Aug 29 '16 at 01:13
  • 2
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). *"Is there any disadvantage to use absolute positioning in this situation?"* Yes. The next PC on which this is displayed. Please stop giving advice on cross-platform layouts until you understand the pitfalls. – Andrew Thompson Aug 29 '16 at 02:20
  • Layout managers work great when dealing with traditional GUIs (forms, tables, lists, dialogs, etc.). But the moment you want to think outside the side the box and build a non-traditional GUI, such as a restaurant table arrangement, layout managers can be very difficult to work with. Especially for someone who is "new to Java". Absolute positioning is an easy concept to understand when building a GUI that is different from the established norm. – eighthrazz Aug 29 '16 at 16:56
  • 1
    Absolute positioning is the same as using a null layout, see [Null Layout is Evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing). It might seem simple and easy but when you face an error due to using it, you might need to redo the whole thing using a layout manager! So, NO! Simply **don't** use absolute positioning / null layouts! – Frakcool Aug 29 '16 at 23:09