Can't display enough JLabels/TextFields properly - java

I am trying to display roughly 7 or 8 multiple text fields and JLabels in a single frame. I am having trouble getting them all to show up. The only way I know to display the individual parts of the GUI is to create a JFrame, then create a container within that frame, then create multiple panels and add them to the container. Is there a better way to do this? One that isn't so restricting and allows me to display more than 4 frames? (I'm limited to only choosing NORTH, EAST, SOUTH, WEST and only one item will show up at those locations)
Here is my code:
Container content = n.getContentPane();
Container contentTwo = n.getContentPane();
JTextField JField = new JTextField(agentID);
JTextField stateField = new JTextField("Running");
JTextField transField = new JTextField(5);
JTextField opsField = new JTextField("0");
stateField.setEnabled(false);
transField.setEnabled(false);
JField.setEnabled(false);
opsField.setEnabled(false);
JLabel stateLabel = new JLabel("State:");
JLabel transLabel = new JLabel("Amount transferred:");
JLabel opsLabel = new JLabel("Operations Completed:");
JPanel fundsPanel = new JPanel(new BorderLayout());
JLabel agentLabel = new JLabel("Agent ID: ");
agentLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
stateLabel.setLabelFor(stateField);
transLabel.setLabelFor(transField);
agentLabel.setLabelFor(JField);
opsLabel.setLabelFor(opsField);
fundsPanel.add(agentLabel, BorderLayout.WEST);
fundsPanel.add(JField, BorderLayout.CENTER);
content.add(fundsPanel, BorderLayout.NORTH);
JLabel accLabel = new JLabel("Amount in $: ");
JPanel accPanel = new JPanel(new BorderLayout());
JPanel accPanelTwo = new JPanel(new BorderLayout());
JPanel accPanelThree = new JPanel(new BorderLayout());
accLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
JFormattedTextField accTextField = new JFormattedTextField(amount);
accTextField.setEnabled(false);
JLabel opLabel = new JLabel("Operations per second:");
opLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
JTextField accTextFieldTwo = new JTextField(Double.toString(opsPerSec), 5);
accTextFieldTwo.setEnabled(false);
accLabel.setLabelFor(accTextField);
opLabel.setLabelFor(accTextFieldTwo);
accPanel.add(accLabel, BorderLayout.WEST);
accPanel.add(accTextField, BorderLayout.CENTER);
accPanelTwo.add(opLabel, BorderLayout.WEST);
accPanelTwo.add(accTextFieldTwo, BorderLayout.CENTER);
accPanelTwo.add(opsLabel, BorderLayout.EAST);
accPanelTwo.add(opsField, BorderLayout.NORTH);
accPanelThree.add(stateLabel, BorderLayout.WEST);
accPanelThree.add(stateField, BorderLayout.CENTER);
content.add(accPanel, BorderLayout.CENTER);
content.add(accPanelTwo, BorderLayout.EAST);
contentTwo.add(accPanelThree, BorderLayout.EAST);
JButton jButton1 = new JButton("Stop agent");
JButton jButton2 = new JButton("Dismiss");
jButton1.addActionListener(handler);
jButton2.addActionListener(handler);
buttonPanel.setLayout(new GridLayout(2, 3, 3, 3));
n.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(jButton2, null);
buttonPanel.add(jButton1, null);
n.pack();

I just coded the first few fields of your panel using the GridBagLayout to show you how to use the layout.
You'll have to finish the coding to get your full form created.
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridBagPanel {
protected static final Insets leftInsets = new Insets(10, 10, 0, 0);
protected static final Insets rightInsets = new Insets(10, 10, 0, 10);
protected JPanel mainPanel;
protected String agentID;
public GridBagPanel() {
createPartControl();
}
private void createPartControl() {
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
int gridy = 0;
JLabel stateLabel = new JLabel("State:");
addComponent(mainPanel, stateLabel, 0, gridy, 1, 1, leftInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField stateField = new JTextField("Running");
stateField.setEnabled(false);
stateLabel.setLabelFor(stateField);
addComponent(mainPanel, stateField, 1, gridy++, 1, 1, rightInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JLabel transLabel = new JLabel("Amount transferred:");
addComponent(mainPanel, transLabel, 0, gridy, 1, 1, leftInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField transField = new JTextField(5);
transField.setEnabled(false);
transLabel.setLabelFor(transField);
addComponent(mainPanel, transField, 1, gridy++, 1, 1, rightInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JLabel opsLabel = new JLabel("Operations Completed:");
addComponent(mainPanel, opsLabel, 0, gridy, 1, 1, leftInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField opsField = new JTextField("0");
opsField.setEnabled(false);
opsLabel.setLabelFor(opsField);
addComponent(mainPanel, opsField, 1, gridy++, 1, 1, rightInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JLabel agentLabel = new JLabel("Agent ID: ");
addComponent(mainPanel, opsLabel, 0, gridy, 1, 1, leftInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField agentField = new JTextField(agentID);
agentField.setEnabled(false);
agentLabel.setLabelFor(agentField);
addComponent(mainPanel, agentField, 1, gridy++, 1, 1, rightInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
}
protected void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
}

Related

More efficient GUI design for JTextArea project [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm currently working on enhancing a project at work which requires me to create 6 different GUI's. I should start by saying I am extremely bad at GUI design. I have tried to learn Grid Bag but really found it difficult.
In this GUI I am using BorderLayout and Flow Layout and it works fine. I am able to create the GUI's and accomplish the objective but I can't help feeling like this method is not professional and is going to have a lot of unnecessary code if my other GUI's end up having a lot more JTextFields and JLabels (which some will).
I would like to know if there are any layouts that can reduce the amount of panels I have here. I have explored Grid layouts but am concerned about all cells being the same size. If someone can help by letting me know, from their experience, the best layout for this project and maybe show me how to use it I would appreciate it. I've tried doing layout tutorials like GridBag layout but have problems actually implementing it on my own projects.
Main Class
package mainClasses;
import gui.AllGUI;
public class TesterClass
{
public static void main(String args[])
{
AllGUI guiALL = new AllGUI();
guiALL.createAllGUI();
}
}
GUI
package gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AllGUI
{
public void createAllGUI(){
JFrame frame = new JFrame("All File Types Selection");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel panelOne = new JPanel(new BorderLayout());
JPanel panelLine1 = new JPanel(new FlowLayout());
JPanel panelLine2 = new JPanel(new FlowLayout());
JPanel panelLine3 = new JPanel(new FlowLayout());
JPanel panelLine4 = new JPanel(new FlowLayout());
JPanel panelLine5 = new JPanel(new FlowLayout());
JButton confirmButton = new JButton("Confirm");
JLabel groupMessageIdTitle = new JLabel("Group Message Id:");
JLabel isoDateTimeTitle = new JLabel("ISO Creation Date/Time:");
JLabel notificationIdTitle = new JLabel("Notification Id:");
JLabel notificationAcctIdTitle = new JLabel("Notification Account Id:");
JLabel numberOfEntriesTitle = new JLabel("Number of Entries:");
JLabel sumOfAmountsTitle = new JLabel("Sum of Amounts:");
JLabel fileTypeTitle = new JLabel("Camt54 File Type:");
JTextField groupMessageIdText = new JTextField("",10);
JTextField isoDateTimeText = new JTextField("",10);
JTextField notificationIdText = new JTextField("",10);
JTextField notificationAcctIdText = new JTextField("",10);
JTextField numberOfEntriesText = new JTextField("",10);
JTextField sumOfAmountsText = new JTextField("",10);
String[] fileTypes = {"OTC-R Message", "Home-Banking", "Cleared Checks"};
JComboBox fileTypesComboBox = new JComboBox(fileTypes);
panelLine1.add(groupMessageIdTitle);
panelLine1.add(groupMessageIdText);
panelLine1.add(isoDateTimeTitle);
panelLine1.add(isoDateTimeText);
panelLine2.add(notificationIdTitle);
panelLine2.add(notificationIdText);
panelLine2.add(notificationAcctIdTitle);
panelLine2.add(notificationAcctIdText);
panelLine3.add(numberOfEntriesTitle);
panelLine3.add(numberOfEntriesText);
panelLine3.add(sumOfAmountsTitle);
panelLine3.add(sumOfAmountsText);
panelLine4.add(fileTypeTitle);
panelLine4.add(fileTypesComboBox);
panelLine5.add(confirmButton);
panelOne.add(panelLine1,BorderLayout.NORTH);
panelOne.add(panelLine2,BorderLayout.CENTER);
panelOne.add(panelLine3,BorderLayout.SOUTH);
mainPanel.add(panelOne,BorderLayout.NORTH);
mainPanel.add(panelLine4,BorderLayout.CENTER);
mainPanel.add(panelLine5,BorderLayout.SOUTH);
frame.add(mainPanel);
frame.setVisible(true);
frame.setSize(630,210);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
I guess it is an opinion, but I think this GUI looks more professional.
The labels and text fields are aligned in columns. I used the GridBagLayout to create the form portion of the GUI.
Here's the code. I grouped the JFrame code, the JPanel code, and the Swing components within the JPanel together so the code is easier to understand.
I put the main method with this code so I'd only have one file to paste.
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class AllGUI {
private static final Insets normalInsets = new Insets(10, 10, 0, 10);
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new AllGUI().createAllGUI();
}
};
SwingUtilities.invokeLater(runnable);
}
public void createAllGUI() {
JFrame frame = new JFrame("All File Types Selection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel formPanel = new JPanel(new GridBagLayout());
int gridy = 0;
JLabel groupMessageIdTitle = new JLabel("Group Message Id:");
addComponent(formPanel, groupMessageIdTitle, 0, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField groupMessageIdText = new JTextField("", 10);
addComponent(formPanel, groupMessageIdText, 1, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel isoDateTimeTitle = new JLabel("ISO Creation Date/Time:");
addComponent(formPanel, isoDateTimeTitle, 2, gridy, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField isoDateTimeText = new JTextField("", 10);
addComponent(formPanel, isoDateTimeText, 3, gridy++, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel notificationIdTitle = new JLabel("Notification Id:");
addComponent(formPanel, notificationIdTitle, 0, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField notificationIdText = new JTextField("", 10);
addComponent(formPanel, notificationIdText, 1, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel notificationAcctIdTitle = new JLabel("Notification Account Id:");
addComponent(formPanel, notificationAcctIdTitle, 2, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField notificationAcctIdText = new JTextField("", 10);
addComponent(formPanel, notificationAcctIdText, 3, gridy++, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel numberOfEntriesTitle = new JLabel("Number of Entries:");
addComponent(formPanel, numberOfEntriesTitle, 0, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField numberOfEntriesText = new JTextField("", 10);
addComponent(formPanel, numberOfEntriesText, 1, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel sumOfAmountsTitle = new JLabel("Sum of Amounts:");
addComponent(formPanel, sumOfAmountsTitle, 2, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField sumOfAmountsText = new JTextField("", 10);
addComponent(formPanel, sumOfAmountsText, 3, gridy++, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel fileTypeTitle = new JLabel("Camt54 File Type:");
addComponent(formPanel, fileTypeTitle, 0, gridy, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
String[] fileTypes = { "OTC-R Message", "Home-Banking",
"Cleared Checks" };
JComboBox<String> fileTypesComboBox = new JComboBox<>(fileTypes);
addComponent(formPanel, fileTypesComboBox, 1, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JPanel confirmPanel = new JPanel();
JButton confirmButton = new JButton("Confirm");
confirmPanel.add(confirmButton);
mainPanel.add(formPanel, BorderLayout.CENTER);
mainPanel.add(confirmPanel, BorderLayout.SOUTH);
return mainPanel;
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 0.0D, 0.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
}

JFrame Layout management

With the following code I tried to create a layout for a calculator frame.
The output I get seems as if there is something going extremely wrong but I can't spot it.
Output:
My question is can you try to compile this and see if you get the same output as me?
If you do is there anything I should be checking when getting this sort of problem. Thanks
Code:
package mycalculator;
import javax.swing.*;
import java.awt.*;
public class MyCalculatorGUI extends JFrame {
//initialise all the components
JPanel mainPanel = new JPanel();
JPanel memPanel = new JPanel();
JPanel advFuncPanel = new JPanel();
JPanel wordFuncPanel = new JPanel();
JPanel numPanel = new JPanel();
JPanel basicFuncPanel = new JPanel();
JTextField txtDisplay = new JTextField(100);
JToggleButton mPlus = new JToggleButton("M+");
JButton mClear = new JButton("CM");
JButton mA = new JButton("A");
JButton mB = new JButton("B");
JButton mC = new JButton("C");
JButton mD = new JButton("D");
JButton aSq = new JButton("x"+ "\u00B2");
JButton aCu = new JButton("x" + "\u00B3");
JButton aPowa = new JButton("x" + "\u00AA");
JButton aInv = new JButton("x" + "\u00AF" + "\u00B9");
JButton aSqrt = new JButton("\u221A" + "x");
JButton aFact = new JButton("x!");
JButton btnSin = new JButton("sin(x)");
JButton btnCos = new JButton("cos(x)");
JButton btnTan = new JButton("tan(x)");
JButton btnaSin = new JButton("arcsin(x)");
JButton btnaCos = new JButton("arccos(x)");
JButton btnaTan = new JButton("arctan(x)");
JToggleButton hyp = new JToggleButton("hyp");
JButton btnAbs = new JButton("abs(x)");
JButton btnExp = new JButton("exp(x)");
JButton btnLog = new JButton("log(x)");
JButton btnLn = new JButton("ln(x)");
JButton btnLoga = new JButton("loga(x)");
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 btn0 = new JButton("0");
JButton btnDot = new JButton(".");
JButton btnCl = new JButton("Cl");
JButton btnBs = new JButton("\u00AB");
JButton btnPlus = new JButton("+");
JButton btnMinus = new JButton("-");
JButton btnMult = new JButton("*");
JButton btnDiv = new JButton("\u00F7");
JButton btnEq = new JButton("=");
public static void main(String[] args) {
new MyCalculatorGUI();
}
public MyCalculatorGUI() {
// create the frame for the Calculator
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("Calculator");
// parent panel containes every other panel
mainPanel.setLayout(new GridBagLayout());
this.add(mainPanel);
// text display
txtDisplay.setEditable(false);
addComp(mainPanel, txtDisplay, 0, 0, 4, 1);
// --------------------------------------------------------------------------
// memory panel, flow layout, 6 buttons same size, spans gridwidth 2
memPanel.setLayout(new FlowLayout());
// add button to panel
memPanel.add(mPlus);
memPanel.add(mClear);
memPanel.add(mA);
memPanel.add(mB);
memPanel.add(mC);
memPanel.add(mD);
// add panel to main panel
addComp(mainPanel, memPanel, 0, 1, 2, 1);
// --------------------------------------------------------------------------
// advFuncPanel, flow layout, 6 buttons same size, spans gridwidth 2
advFuncPanel.setLayout(new FlowLayout());
// add button to panel
advFuncPanel.add(aSq);
advFuncPanel.add(aCu);
advFuncPanel.add(aPowa);
advFuncPanel.add(aInv);
advFuncPanel.add(aSqrt);
advFuncPanel.add(aFact);
// add panel to main panel
addComp(mainPanel, advFuncPanel, 0, 2, 2, 1);
// --------------------------------------------------------------------------
// wordFuncPanel, grid layout (3 rows, 4 columns), 12 buttons same size, spans gridwidth 2
wordFuncPanel.setLayout(new GridLayout(3, 4));
// add buttons to panel
wordFuncPanel.add(btnSin);
wordFuncPanel.add(btnaSin);
wordFuncPanel.add(hyp);
wordFuncPanel.add(btnLog);
wordFuncPanel.add(btnCos);
wordFuncPanel.add(btnaCos);
wordFuncPanel.add(btnAbs);
wordFuncPanel.add(btnLn);
wordFuncPanel.add(btnTan);
wordFuncPanel.add(btnaTan);
wordFuncPanel.add(btnExp);
wordFuncPanel.add(btnLoga);
// add panel to main panel
addComp(mainPanel, wordFuncPanel, 0, 3, 2, 1);
// --------------------------------------------------------------------------
// numPanel, gridbaglayout, spans gridheight 3
numPanel.setLayout(new GridBagLayout());
// add buttons to panel
addComp(numPanel, btn1, 0, 0, 1, 1);
addComp(numPanel, btn2, 1, 0, 1, 1);
addComp(numPanel, btn3, 2, 0, 1, 1);
addComp(numPanel, btn4, 0, 1, 1, 1);
addComp(numPanel, btn5, 1, 1, 1, 1);
addComp(numPanel, btn6, 2, 1, 1, 1);
addComp(numPanel, btn7, 0, 2, 1, 1);
addComp(numPanel, btn8, 1, 2, 1, 1);
addComp(numPanel, btn9, 2, 2, 1, 1);
addComp(numPanel, btn0, 0, 3, 2, 1);
addComp(numPanel, btnDot, 2, 3, 1, 1);
addComp(numPanel, btnCl, 0, 4, 2, 1);
addComp(numPanel, btnBs, 2, 4, 1, 1);
// add panel to mainpanel
addComp(mainPanel, numPanel, 2, 1, 1, 3);
// --------------------------------------------------------------------------
//basicFuncPanel, verticalboxlayout, spans gridheight 3
basicFuncPanel.setLayout(new BoxLayout(basicFuncPanel, BoxLayout.Y_AXIS));
// add buttons to panel
basicFuncPanel.add(btnPlus);
basicFuncPanel.add(btnMinus);
basicFuncPanel.add(btnMult);
basicFuncPanel.add(btnDiv);
basicFuncPanel.add(btnEq);
// add panel to main panel
addComp(mainPanel, basicFuncPanel, 3, 1, 1, 3);
// --------------------------------------------------------------------------
this.pack();
this.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight) {
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = xPos;
gridConstraints.gridy = yPos;
gridConstraints.gridwidth = compWidth;
gridConstraints.gridheight = compHeight;
gridConstraints.weightx = 100;
gridConstraints.weighty = 100;
gridConstraints.insets = new Insets(5, 5, 5, 5);
gridConstraints.anchor = GridBagConstraints.CENTER;
gridConstraints.fill = GridBagConstraints.BOTH;
panel.add(comp, gridConstraints);
}
private JComponent[] components = { mPlus, mClear, mA, mB, mC, mD, aSq, aCu, aPowa, aInv, aSqrt, aFact,
btnSin, btnCos, btnTan, btnaSin, btnaCos, btnaTan, hyp, btnAbs, btnExp, btnLog, btnLn, btnLoga,
btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnDot, btnCl, btnBs, btnPlus, btnMinus,
btnMult, btnDiv, btnEq };
public JComponent[] getComponents() {
return components;
}
}
Just do one change and everything will work fine.
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MyCalculatorGUI();
}
});
}
Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly.
Read more
Why to use SwingUtilities.invokeLater in main method?
SwingUtilities.invokeLater
EDIT
Some time this.setResizable(false); create issues while re-sizing or packing the components in the JFrame.

How to resize JPanel when JFrame is maximized?

I am new to java swing, recently I try to create a swing app to format text.
When I click the maximum button, the text panel's length does not resize, and the middle panel takes large space.
And seems setResizable(false) does not work
Code
public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;
private JTextArea fromTextArea;
private JTextArea toTextArea;
public MainFrame() {
super("jFormatter");
Panel mainPanel = new Panel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
setContentPane(mainPanel);
fromTextArea = createTextArea();
lines.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.LIGHT_GRAY));
lines.setEditable(false);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
lines.setFont(f);
JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
fromTextAreaScrollPanel.getViewport().add(fromTextArea);
fromTextAreaScrollPanel.setRowHeaderView(lines);
mainPanel.add(fromTextAreaScrollPanel);
// control panel
mainPanel.add(createCtrlPanel());
toTextArea = createTextArea();
mainPanel.add(createTextAreaPanel(toTextArea));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
}
private JPanel createCtrlPanel() {
final JComboBox jComboBox = new JComboBox(formatters.keySet().toArray());
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));
JButton fmtButton = new JButton("Format >>");
JPanel ctrPanel = new JPanel(new GridBagLayout());
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
//gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);
return ctrPanel;
}
private JScrollPane createTextAreaPanel(JTextArea textArea) {
JScrollPane fromTextAreaScrollPanel = new JScrollPane(textArea);
//fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
return fromTextAreaScrollPanel;
}
private JTextArea createTextArea() {
JTextArea textArea = new JTextArea(20, 40);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
textArea.setFont(f);
//fromTextArea.setLineWrap(true);
//textArea.setBackground(Color.LIGHT_GRAY);
textArea.setMargin(new Insets(0, 10, 0, 10));
return textArea;
}
public static void main(String[] args) {
new MainFrame();
}
}
result:
You should probably use BorderLayout or GridBagLayout for this. BoxLayout just lays out components one after the other at their preferred size. It doesn't make any attempt to resize the components or make them fill their parent.
Try to make a layout like this:
Code:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
gbc_scrollPane.weightx=1;
contentPane.add(scrollPane, gbc_scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.gridheight = 2;
gbc_panel.insets = new Insets(0, 0, 5, 5);
//gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JComboBox comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 2;
gbc_comboBox.gridy = 0;
gbc_comboBox.weightx=0.0;
panel.add(comboBox, gbc_comboBox);
JButton btnNewButton = new JButton(">>");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
gbc_btnNewButton.gridx = 2;
gbc_btnNewButton.gridy = 1;
panel.add(btnNewButton, gbc_btnNewButton);
JScrollPane scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.gridheight = 2;
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 2;
gbc_scrollPane_1.gridy = 0;
gbc_scrollPane_1.weightx=1;
contentPane.add(scrollPane_1, gbc_scrollPane_1);
JTextArea textArea_1 = new JTextArea();
scrollPane_1.setViewportView(textArea_1);
pack();
}
}

Difficulty aligning componenents within multiple JPanels

I am trying to create a layout where I have two panels with borders around them. I would like the rectangular borders to be of equal size. I would also like the JTextField in the bottom panel to be of that smaller width.
The problem is that, whenever I fill the bottom panel horizontally (using GridBagConstraints.HORIZONTAL), the checkbox and label inside that panel become misaligned with the checkbox and label in the panel above it.
I was hoping an experienced developer could offer insight into how I could make the two JPanels and their rectangular borders equal in size with one another, while also having their checkboxes and labels aligned.
Here is what it looks like now:
Here is code to reproduce the problem:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class Example
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
MyFrame frame = new MyFrame();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
class MyFrame
{
private JFrame frame;
private MyDialog dialog;
public MyFrame()
{
frame = new JFrame();
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
dialog = new MyDialog(frame);
}
}
class MyDialog
{
private JDialog dialog;
private JPanel mainPanel,
panel1,
inputPanel1,
panel2,
inputPanel2;
private JLabel titleLabel,
label1,
label2;
private JCheckBox checkBox1,
checkBox2;
private JTextField textField1,
textField2;
public MyDialog(JFrame frame)
{
dialog = new JDialog(frame, true);
buildPanel();
dialog.add(mainPanel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void buildPanel()
{
mainPanel = new JPanel(new GridBagLayout());
titleLabel = new JLabel("Title");
checkBox1 = new JCheckBox("checkBox1");
label1 = new JLabel("label1:");
textField1 = new JTextField(15);
inputPanel1 = new JPanel(new GridBagLayout());
inputPanel1.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0)); // indent the label and textfield
inputPanel1.add(label1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
inputPanel1.add(textField1, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel1 = new JPanel(new GridBagLayout());
Border border1 = BorderFactory.createEtchedBorder();
panel1.setBorder(border1);
panel1.add(checkBox1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel1.add(inputPanel1, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
checkBox2 = new JCheckBox("checkBox2");
label2 = new JLabel("label2:");
textField2 = new JTextField(8);
inputPanel2 = new JPanel(new GridBagLayout());
inputPanel2.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0)); // indent the label and textfield
inputPanel2.add(label2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
inputPanel2.add(textField2, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel2 = new JPanel(new GridBagLayout());
Border border2 = BorderFactory.createEtchedBorder();
panel2.setBorder(border2);
panel2.add(checkBox2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel2.add(inputPanel2, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
mainPanel.add(titleLabel, getConstraints(0, 0, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE));
mainPanel.add(panel1, getConstraints(0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
// When I fill panel2 horizontally, the checkbox becomes misaligned with the above checkbox:
//mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL));
}
private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.anchor = anchor;
gbc.fill = fill;
return gbc;
}
}
What I did was
Make your inputPanelX have FlowLayout with FlowLayout.LEADING instead of using GirdBagLayout for them.
inputPanel1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
And the scond one I just made the same Dimension as the first one
inputPanel2 = new JPanel(new FlowLayout(FlowLayout.LEADING)){
Dimension dim = new Dimension(inputPanel1.getPreferredSize());
public Dimension getPreferredSize(){
return new Dimension(dim);
}
};
That's all I changed. See full code below
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class Example
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
MyFrame frame = new MyFrame();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
class MyFrame
{
private JFrame frame;
private MyDialog dialog;
public MyFrame()
{
frame = new JFrame();
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
dialog = new MyDialog(frame);
}
}
class MyDialog
{
private JDialog dialog;
private JPanel mainPanel,
panel1,
inputPanel1,
panel2,
inputPanel2;
private JLabel titleLabel,
label1,
label2;
private JCheckBox checkBox1,
checkBox2;
private JTextField textField1,
textField2;
public MyDialog(JFrame frame)
{
dialog = new JDialog(frame, true);
buildPanel();
dialog.add(mainPanel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void buildPanel()
{
mainPanel = new JPanel(new GridBagLayout());
titleLabel = new JLabel("Title");
checkBox1 = new JCheckBox("checkBox1");
label1 = new JLabel("label1:");
textField1 = new JTextField(15);
inputPanel1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
inputPanel1.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0)); // indent the label and textfield
inputPanel1.add(label1);
inputPanel1.add(textField1);
panel1 = new JPanel(new GridBagLayout());
Border border1 = BorderFactory.createEtchedBorder();
panel1.setBorder(border1);
panel1.add(checkBox1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel1.add(inputPanel1, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
checkBox2 = new JCheckBox("checkBox2");
label2 = new JLabel("label2:");
textField2 = new JTextField(8);
inputPanel2 = new JPanel(new FlowLayout(FlowLayout.LEADING)){
Dimension dim = new Dimension(inputPanel1.getPreferredSize());
public Dimension getPreferredSize(){
return new Dimension(dim);
}
};
inputPanel2.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0)); // indent the label and textfield
inputPanel2.add(label2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
inputPanel2.add(textField2, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel2 = new JPanel(new GridBagLayout());
Border border2 = BorderFactory.createEtchedBorder();
panel2.setBorder(border2);
panel2.add(checkBox2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel2.add(inputPanel2, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
mainPanel.add(titleLabel, getConstraints(0, 0, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE));
mainPanel.add(panel1, getConstraints(0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
// When I fill panel2 horizontally, the checkbox becomes misaligned with the above checkbox:
//mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL));
}
private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.anchor = anchor;
gbc.fill = fill;
return gbc;
}
}

Multiple JPanels one of top of the other

Been trying to make 4 JPanels two on top and other 2 on the bottom and manipulate each independently of each other inputing text etc...beginner with java so if anybody can help me out that would be awesome. I posted another question before but now I am stuck again. lol
package project2;
import javax.swing.JOptionPane;
import java.awt.FlowLayout; // specifies how components are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.JPanel; // Displays a Panel
import javax.swing.SwingConstants; // common constants used with Swing
import javax.swing.Icon; // interface used to manipulate images
import javax.swing.ImageIcon; // loads images
import java.awt.Color;
import java.awt.CardLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.Container;
import java.awt.Dimension;
public class GUI
{
private JPanel Checks; //Panel to Hold Checks
private JPanel Transactions;
private JPanel History;
private JPanel Graphics;
private JLabel CLabel;
public void displayGUI()
{
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(
new GridLayout(2, 2, 5, 5));
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
Checks = getPanel(Color.YELLOW.darker().darker());
Transactions = getPanel(Color.RED);
History = getPanel(Color.GREEN.darker().darker());
Graphics = getPanel(Color.MAGENTA);
CLabel = new JLabel("Label with on red text");
contentPane.add(Checks);
contentPane.add(Transactions);
contentPane.add(History);
contentPane.add(Graphics);
frame.setSize(1400,690); //set frame size
frame.setVisible(true); //display frame
frame.setContentPane(contentPane);
}
private JPanel getPanel(Color bColor) {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(bColor);
return panel;
}
Expected Output :
Please have a look at this answer, and see if you need this or something else. Do let us know, we might try to add a bit more deeper.
import java.awt.*;
import java.util.Random;
import javax.swing.*;
/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 9/30/13
* Time: 6:02 PM
* To change this template use File | Settings | File Templates.
*/
public class UTBInCheck {
private JPanel contentPane;
private JPanel topLeftPanel;
private GridBagConstraints gbc;
private JTextArea infoArea;
private JScrollPane infoScroller;
private JTextField payOrderField;
private JTextField amountField;
private JButton orderSubmitButton;
private JButton orderClearButton;
private JPanel topRightPanel;
private JTextArea recentTransArea;
private JScrollPane recentTransScroller;
private JButton updateButton;
private JPanel bottomLeftPanel;
private JTextField atmWithdrawalField;
private JTextField atmDepositField;
private JTextField directDepositField;
private JTextField wireTransferField;
private JTextField electronicBillField;
private JTextField bankFeeField;
private JButton etSubmitButton;
private JButton etClearButton;
private JPanel bottomRightPanel;
private Random random;
private static final int GAP = 5;
private int checkNumber;
public UTBInCheck() {
random = new Random();
gbc = new GridBagConstraints();
gbc.insets = new Insets(GAP, GAP, GAP, GAP);
checkNumber = 811;
}
private void displayGUI() {
JFrame frame = new JFrame("University of Texas at Brownsville");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = getPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
contentPane.setLayout(new GridLayout(2, 2, GAP, GAP));
topLeftPanel = getPanel();
setTopLeftPanel(topLeftPanel);
topRightPanel = getPanel();
setTopRightPanel(topRightPanel);
bottomLeftPanel = getPanel();
setBottomLeftPanel(bottomLeftPanel);
bottomRightPanel = getPanel();
contentPane.add(topLeftPanel);
contentPane.add(topRightPanel);
contentPane.add(bottomLeftPanel);
contentPane.add(bottomRightPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void setTopLeftPanel(JPanel panel) {
panel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
panel.setLayout(new BorderLayout(GAP, GAP));
JPanel topPanel = getPanel();
topPanel.setLayout(new GridBagLayout());
JLabel universityLabel = getLabel("University of Texas at Brownsville");
addComp(topPanel, universityLabel, 0, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST);
JLabel checkNumberLabel = getLabel("Check Number : " + checkNumber);
addComp(topPanel, checkNumberLabel, 1, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHEAST);
infoArea = new JTextArea(5, 10);
infoArea.setLineWrap(true);
infoArea.setWrapStyleWord(true);
infoScroller = new JScrollPane();
infoScroller.setViewportView(infoArea);
addComp(topPanel, infoScroller, 0, 1, 2, 1, 1.0, 0.4,
GridBagConstraints.BOTH, GridBagConstraints.WEST);
JPanel centerPanel = getPanel();
centerPanel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
centerPanel.setLayout(new GridLayout(0, 2, GAP, GAP));
JLabel payOrderLabel = getLabel("Pay to the order of : ");
payOrderField = new JTextField(10);
JLabel amountLabel = getLabel("Amount : ");
amountField = new JTextField(10);
JLabel chrisBankLabel = getLabel("Cris' Bank");
JPanel buttonPanel = getPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
orderSubmitButton = new JButton("Submit");
orderClearButton = new JButton("Clear");
buttonPanel.add(orderSubmitButton);
buttonPanel.add(orderClearButton);
centerPanel.add(payOrderLabel);
centerPanel.add(payOrderField);
centerPanel.add(amountLabel);
centerPanel.add(amountField);
centerPanel.add(chrisBankLabel);
centerPanel.add(buttonPanel);
addComp(topPanel, centerPanel, 0, 2, 2, 1, 1.0, 0.4,
GridBagConstraints.BOTH, GridBagConstraints.SOUTHWEST);
panel.add(topPanel, BorderLayout.CENTER);
}
private void addComp(JPanel panel, JComponent comp,
int gridx, int gridy, int gridwidth, int gridheight,
double weightx, double weighty, int fill, int anchor) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
gbc.anchor = anchor;
panel.add(comp, gbc);
}
private void setTopRightPanel(JPanel panel) {
panel.setBorder(BorderFactory.createTitledBorder("Recent Transactions"));
panel.setLayout(new BorderLayout(GAP, GAP));
recentTransArea = new JTextArea(10, 10);
recentTransArea.setLineWrap(true);
recentTransArea.setWrapStyleWord(true);
recentTransScroller = new JScrollPane();
recentTransScroller.setViewportView(recentTransArea);
JPanel buttonPanel = getPanel();
updateButton = new JButton("Update");
buttonPanel.add(updateButton);
panel.add(recentTransScroller, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.PAGE_END);
}
private void setBottomLeftPanel(JPanel panel) {
panel.setBorder(
BorderFactory.createTitledBorder("Electronic Transactions"));
panel.setLayout(new GridLayout(0, 2, GAP, GAP));
JLabel atmWithdrawLabel = getLabel("ATM Withdrawals : ");
atmWithdrawalField = new JTextField(10);
JLabel atmdepositLabel = getLabel("ATM Deposit : ");
atmDepositField = new JTextField(10);
JLabel directDepositLabel = getLabel("Direct Deposit : ");
directDepositField = new JTextField(10);
JLabel wireTransferLabel = getLabel("Wire Transfers : ");
wireTransferField = new JTextField(10);
JLabel electronicBillLabel = getLabel("Electronic Bills : ");
electronicBillField = new JTextField(10);
JLabel bankFeeLabel = getLabel("Bank Fees : ");
bankFeeField = new JTextField(10);
JPanel buttonPanel = getPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
etSubmitButton = new JButton("Submit");
etClearButton = new JButton("Clear");
buttonPanel.add(etSubmitButton);
buttonPanel.add(etClearButton);
panel.add(atmWithdrawLabel);
panel.add(atmWithdrawalField);
panel.add(atmdepositLabel);
panel.add(atmDepositField);
panel.add(directDepositLabel);
panel.add(directDepositField);
panel.add(wireTransferLabel);
panel.add(wireTransferField);
panel.add(electronicBillLabel);
panel.add(electronicBillField);
panel.add(bankFeeLabel);
panel.add(bankFeeField);
//Adding JPanel just to occupy space;
panel.add(getPanel());
panel.add(buttonPanel);
}
private JLabel getLabel(String text) {
return (new JLabel(text, JLabel.LEFT));
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(getRandomColor());
/*panel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));*/
return panel;
}
private Color getRandomColor() {
return (new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new UTBInCheck().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
OUTPUT :

Categories

Resources