caramel1995
caramel1995

Reputation: 3055

Java GUI Components and Panel Alignment

I'm quite new to Java GUI and I'm having trouble aligning the GUI of this program I created . The code is as follow :

The Code :

import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
public class SalesScreen{

private JPanel mainPanel = new JPanel();

//Top Member Panel
private JPanel memberPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JLabel memberID =  new JLabel();
private JTextField memberIDField = new JTextField(10);
private JLabel memberName = new JLabel();
private JTextField memberNameField = new JTextField(30);
private JButton memberButton = new JButton("OK");

//Center Table Panel
private JPanel tablePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
private JTable itemList;
String [] columnNames = {"Barcode" , "Name" , "Price (RM)" , "Quantity"};
int numRows = 30;
DefaultTableModel model = new DefaultTableModel(numRows , columnNames.length);
JScrollPane scroll;

//Bottom Right 
private JPanel infoPanel = new JPanel(new BorderLayout());
private JPanel infoGridPanel = new JPanel( new GridLayout(4,2));
private JPanel infoFlowPanel = new JPanel(new FlowLayout());
private JLabel total = new JLabel("          Total (RM) :");
private JLabel discount = new JLabel("          Discount Rate (%) : ");
private JLabel overall = new JLabel("          Overall (RM) :");
private JLabel payment = new JLabel("          Payment (RM) :");
private JButton process = new JButton("PROCESS");
private JTextField totalPrice = new JTextField(5);
private JTextField discountRate = new JTextField(5);
private JTextField overallPrice = new JTextField(5);
private JTextField paymentMade = new JTextField(5);

//Bottom
private JPanel itemPanel = new JPanel(new BorderLayout());
private JPanel testPanel = new JPanel(new GridLayout(2 , 4 , 4 , 20));
private JLabel emptyLabel = new JLabel("");
private JLabel emptyLabel2 = new JLabel("");
private JLabel barcode = new JLabel("Enter Barcode :");
private JTextField barcodeScanner = new JTextField(5);
private JLabel quantity = new JLabel("Quantity :");
private JTextField itemQuantity = new JTextField(5);
private JButton addItem = new JButton("Add");
private JButton deleteItem = new JButton("Delete");

    //                                           //
   //                                           //
  //    Beginning of Constructor and Method    //
 //                                           //
//                                           //

SalesScreen(JTabbedPane tabp , JFrame f) //tabp is Jtabbedpanel , f is the Jframe
{
    mainPanel.setLayout(new BorderLayout());
    memberID.setText("          Member ID :");
    memberIDField.setText("");
    memberName.setText("Member Name :");
    memberNameField.setText("");
    memberNameField.setEditable(false);
    memberPanel.add(memberID);
    memberPanel.add(memberIDField);
    memberPanel.add(memberName);
    memberPanel.add(memberNameField);
    memberPanel.add(memberButton);
    //
    model.setColumnIdentifiers(columnNames);
    itemList = new JTable(model);
    scroll = new JScrollPane(itemList); 
    //tablePanel.add(scroll);
    //
    total.setLabelFor(totalPrice);
    totalPrice.setEditable(false);
    discountRate.setEditable(false);
    overallPrice.setEditable(false);
    process.setPreferredSize(new Dimension(100, 100));
    discount.setLabelFor(discountRate);
    overall.setLabelFor(overallPrice);
    payment.setLabelFor(paymentMade);
    infoGridPanel.add(total);
    infoGridPanel.add(totalPrice);
    infoGridPanel.add(discount);
    infoGridPanel.add(discountRate);
    infoGridPanel.add(overall);
    infoGridPanel.add(overallPrice);
    infoGridPanel.add(payment);
    infoGridPanel.add(paymentMade);
    infoFlowPanel.add(infoGridPanel);
    infoFlowPanel.add(process);
    infoPanel.add(infoFlowPanel , "South");
    //

    testPanel.setMaximumSize( new Dimension(  100, 100) );
    testPanel.add(barcode);
    testPanel.add(barcodeScanner);
    testPanel.add(addItem);
    testPanel.add(deleteItem);
    testPanel.add(quantity);
    testPanel.add(itemQuantity);
    testPanel.add(emptyLabel);
    testPanel.add(emptyLabel2);
    itemPanel.add(testPanel , "West");
    //
    mainPanel.add(memberPanel , "North");
    mainPanel.add(scroll , "Center");
    mainPanel.add(infoPanel , "East");
    mainPanel.add(itemPanel , "South");
    tabp.add("Sales" , mainPanel); 
    f.add(tabp);
}

}

The Question :

1.)As you can see , there will be a Jtable at the center , a JPanel at bottom right panel and also one at the bottom . What I wanna do is to eliminate the empty space available at the end of the Jtable by moving the bottom panel slightly above . I keep try and try to no avail .

Upvotes: 0

Views: 2441

Answers (2)

Arpit
Arpit

Reputation: 12797

import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
class SalesScreen{

private JPanel mainPanel = new JPanel();

//Top Member Panel
private JPanel memberPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JLabel memberID =  new JLabel();
private JTextField memberIDField = new JTextField(10);
private JLabel memberName = new JLabel();
private JTextField memberNameField = new JTextField(30);
private JButton memberButton = new JButton("OK");

//Center Table Panel
private JPanel tablePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
private JTable itemList;
String [] columnNames = {"Barcode" , "Name" , "Price (RM)" , "Quantity"};
int numRows = 50;
DefaultTableModel model = new DefaultTableModel(numRows , columnNames.length);
JScrollPane scroll;

//Bottom Right 
private JPanel infoPanel = new JPanel(new BorderLayout());
private JPanel infoGridPanel = new JPanel( new GridLayout(4,2));
private JPanel infoFlowPanel = new JPanel(new FlowLayout());
private JLabel total = new JLabel("          Total (RM) :");
private JLabel discount = new JLabel("          Discount Rate (%) : ");
private JLabel overall = new JLabel("          Overall (RM) :");
private JLabel payment = new JLabel("          Payment (RM) :");
private JButton process = new JButton("PROCESS");
private JTextField totalPrice = new JTextField(5);
private JTextField discountRate = new JTextField(5);
private JTextField overallPrice = new JTextField(5);
private JTextField paymentMade = new JTextField(5);

//Bottom
private JPanel itemPanel = new JPanel(new BorderLayout());
private JPanel testPanel = new JPanel(new GridLayout(2 , 4 , 4 , 20));
private JLabel emptyLabel = new JLabel("");
private JLabel emptyLabel2 = new JLabel("");
private JLabel barcode = new JLabel("Enter Barcode :");
private JTextField barcodeScanner = new JTextField(5);
private JLabel quantity = new JLabel("Quantity :");
private JTextField itemQuantity = new JTextField(5);
private JButton addItem = new JButton("Add");
private JButton deleteItem = new JButton("Delete");

    //                                           //
   //                                           //
  //    Beginning of Constructor and Method    //
 //                                           //
//                                           //

SalesScreen(JTabbedPane tabp , JFrame f) //tabp is Jtabbedpanel , f is the Jframe
{
    mainPanel.setLayout(new BorderLayout());
    memberID.setText("          Member ID :");
    memberIDField.setText("");
    memberName.setText("Member Name :");
    memberNameField.setText("");
    memberNameField.setEditable(false);
    memberPanel.add(memberID);
    memberPanel.add(memberIDField);
    memberPanel.add(memberName);
    memberPanel.add(memberNameField);
    memberPanel.add(memberButton);
    //
    model.setColumnIdentifiers(columnNames);
    itemList = new JTable(model);
    scroll = new JScrollPane(itemList); 
    //tablePanel.add(scroll);
    //
    total.setLabelFor(totalPrice);
    totalPrice.setEditable(false);
    discountRate.setEditable(false);
    overallPrice.setEditable(false);
    process.setPreferredSize(new Dimension(100, 100));
    discount.setLabelFor(discountRate);
    overall.setLabelFor(overallPrice);
    payment.setLabelFor(paymentMade);
    infoGridPanel.add(total);
    infoGridPanel.add(totalPrice);
    infoGridPanel.add(discount);
    infoGridPanel.add(discountRate);
    infoGridPanel.add(overall);
    infoGridPanel.add(overallPrice);
    infoGridPanel.add(payment);
    infoGridPanel.add(paymentMade);
    infoFlowPanel.add(infoGridPanel);
    infoFlowPanel.add(process);
    infoPanel.add(infoFlowPanel , "South");
    //

    testPanel.setMaximumSize( new Dimension(  100, 100) );
    testPanel.add(barcode);
    testPanel.add(barcodeScanner);
    testPanel.add(addItem);
    testPanel.add(deleteItem);
    testPanel.add(quantity);
    testPanel.add(itemQuantity);
    testPanel.add(emptyLabel);
    testPanel.add(emptyLabel2);
    itemPanel.add(testPanel , "West");
    itemPanel.add(infoPanel , "East");
    //
    mainPanel.add(memberPanel , "North");
    mainPanel.add(scroll , "Center");
  //  mainPanel.add(infoPanel , "East");
    mainPanel.add(itemPanel , "South");
    tabp.add("Sales" , mainPanel); 
    f.add(tabp);
    f.setVisible(true);
}
    public static void main(String[] args){
SalesScreen s=new SalesScreen(new JTabbedPane(),new JFrame("test"));
}
}

Try this code..

Upvotes: 0

Reimeus
Reimeus

Reputation: 159864

If you wish to move the bottom panel up slightly, you can simply reduce the vertical gap in the GridLayout for testPanel. Replace

private JPanel testPanel = new JPanel(new GridLayout(2 , 4 , 4 , 20));

with

private JPanel testPanel = new JPanel(new GridLayout(2, 4, 4, 5));

Also better to override getPreferredSize() for components rather than calling setPreferredSize. This ensures that the preferred size cannot be set by another component.

Calling JFrame.pack will complete the task by sizing the window by arranging all the components according to their preferred sizes.

Upvotes: 1

Related Questions