More efficient GUI design for JTextArea project [closed] - java

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);
}
}

Related

Doesn't show scroll in JScrollPane with JTable after Upgrade Java 7 to Java 8

I've to upgrade project to Java 8. After running my application, scroll from JScrollPane doesn't show. Same piece of code works in Java 7 and everything is ok.
public static void main(String[] a){
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new javax.swing.BoxLayout(contentPane, javax.swing.BoxLayout.Y_AXIS));
JPanel aggregates = new JPanel("Aggregates", new Insets(40, 0, 0, 0));
GridBagConstraints gbc_aggregates = new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, 1, new Insets(10, 10, 10, 10), 0, 0);
gbl_panel.setConstraints(aggregates, gbc_aggregates);
GridBagLayout gbl_agregats = new GridBagLayout();
addParametersLayout(gbl_agregats, new int[]{0,0,0}, new int[]{0, 0},new double[]{ 0.0, 0.0, Double.MIN_VALUE}, new double[]{0.0, Double.MIN_VALUE} );
aggregates.setLayout(gbl_agregats);
JTable table = new JTable(new MyTableModel(null));
table.setPreferredSize(new Dimension(850, Const.rowHeight));
JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(850, 10 *Const.rowHeight));
GridBagConstraints gbc_scrollPane= new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, 1, new Insets(5, 5, 20, 5), 0, 0);
gbl_agregats.setConstraints(scrollPane, gbc_scrollPane);
aggregates.add(scrollPane);
JPanel main_panel = new JPanel();
main_panel.add(aggregates);
JScrollPane scrPane = new JScrollPane(main_panel);
scrPane.getVerticalScrollBar().setUnitIncrement(16);
scrPane.setBorder(BorderFactory.createEmptyBorder());
scrPane.getInsets().set(0, 0, 0, 0);
contentPane.add(scrPane);
setContentPane(contentPane);
}
public static void addParametersLayout(GridBagLayout gbl,
int[] columnWidths, int[] rowHeights, double[] columnWeights,
double[] rowWeights) {
gbl.columnWidths = columnWidths;
gbl.rowHeights = rowHeights;
gbl.columnWeights = columnWeights;
gbl.rowWeights = rowWeights;
}
Question: What was changed in Java 8 ? Why same scroll looks ok in older Java version ?
Edited:
Maybe i'm not too specific.
When i compile my project i see something like that (just bar):
I can scroll my table because it's working, but it's invisible (i see just a bar).
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTab
import javax.swing.border.EmptyBorder;
public class source8 {
public static void main(String[] a){
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new javax.swing.BoxLayout(contentPane,
javax.swing.BoxLayout.Y_AXIS));
JPanel aggregates = new JPanel((LayoutManager) new Insets(40, 0,0,
0));
GridBagConstraints gbc_aggregates = new GridBagConstraints(0, 2,
1, 1, 0.0, 0.0, GridBagConstraints.WEST, 1, new Insets(10, 10, 10,
10), 0, 0);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.setConstraints(aggregates, gbc_aggregates);
GridBagLayout gbl_agregats = new GridBagLayout();
addParametersLayout(gbl_agregats, new int[]{0,0,0}, new int[]{0,
0},new double[]{ 0.0, 0.0, Double.MIN_VALUE}, new double[]{0.0,
Double.MIN_VALUE} );
aggregates.setLayout(gbl_agregats);
JTable table = new JTable(new MyTableModel(null));
table.setPreferredSize(new Dimension(850, Const.rowHeight));
JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(850, 10
*Const.rowHeight));
GridBagConstraints gbc_scrollPane= new GridBagConstraints(0, 1, 3,
1, 0.0, 0.0, GridBagConstraints.WEST, 1, new Insets(5, 5, 20, 5),
0, 0);
gbl_agregats.setConstraints(scrollPane, gbc_scrollPane);
aggregates.add(scrollPane);
JPanel main_panel = new JPanel();
main_panel.add(aggregates);
JScrollPane scrPane = new JScrollPane(main_panel);
scrPane.getVerticalScrollBar().setUnitIncrement(16);
scrPane.setBorder(BorderFactory.createEmptyBorder());
scrPane.getInsets().set(0, 0, 0, 0);
contentPane.add(scrPane);
setContentPane(contentPane);
}
public static void addParametersLayout(GridBagLayout gbl,
int[] columnWidths, int[] rowHeights, double[] columnWeights,
double[] rowWeights) {
gbl.columnWidths = columnWidths;
gbl.rowHeights = rowHeights;
gbl.columnWeights = columnWeights;
gbl.rowWeights = rowWeights;
}
private static void setContentPane(JPanel contentPane) {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
}
some change in code hope it works for you §§thanks
If anyone has the same problem. I added default ScrollBar size for my LookAndFeel and it works.
UIDefaults defaults = UIManager.getLookAndFeel().getDefaults();
defaults.put("ScrollBar.minimumThumbSize", new Dimension(30, 30));

How to set the last button's height of a row in GridBagLayout component?

I'm going to develop a Calculator program in Java, I used GridBagLayout, I want put a big "=" button at the bottom right, but that button's height won't expend to the last space as expected, my major code is pasted as below, please help to advise how to figure that:
my problem one is like this
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import org.omg.CORBA.PUBLIC_MEMBER;
public class MyCalculater {
private Frame frame = new Frame("My-Calculator");
private GridBagConstraints gBagConstraints = new GridBagConstraints();
private GridBagLayout gBagLayout = new GridBagLayout();
private Panel screen = new Panel();
private Panel arithSymbolArea = new Panel();
private Panel digitalArea = new Panel();
private JButton[] digitalButtons = new JButton[10];
public void init() {
// Put a screen for output
screen.add(new TextField(40));
frame.add(screen, BorderLayout.NORTH);
// To put the Arith symbols
arithSymbolArea.setLayout(new GridLayout(6, 4));
frame.add(arithSymbolArea);
digitalArea.setLayout(gBagLayout);
initDigitalArea();
frame.add(digitalArea, BorderLayout.EAST);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
private void initDigitalArea() {
JButton mc = new JButton("mc");
JButton mplus = new JButton("m+");
JButton mminus = new JButton("m-");
JButton mr = new JButton("mr");
JButton ac = new JButton("AC");
JButton pn = new JButton("+/-");
JButton divide = new JButton("÷");
JButton times = new JButton("×");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton equal = new JButton("=");
JButton dot = new JButton(".");
for (int i = 0; i < 10; i++) {
digitalButtons[i] = new JButton(String.valueOf(i));
}
gBagConstraints.fill = GridBagConstraints.BOTH;
gBagConstraints.weightx = 1;
addButton(mc);
addButton(mplus);
addButton(mminus);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
addButton(mr);
gBagConstraints.gridwidth = 1;
gBagConstraints.gridheight = 1;
addButton(ac);
addButton(pn);
addButton(divide);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
addButton(times);
gBagConstraints.gridwidth = 1;
gBagConstraints.gridheight = 1;
addButton(digitalButtons[7]);
addButton(digitalButtons[8]);
addButton(digitalButtons[9]);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
addButton(minus);
gBagConstraints.gridwidth = 1;
gBagConstraints.gridheight = 1;
addButton(digitalButtons[4]);
addButton(digitalButtons[5]);
addButton(digitalButtons[6]);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
addButton(plus);
gBagConstraints.gridwidth = 1;
gBagConstraints.gridheight = 1;
addButton(digitalButtons[1]);
addButton(digitalButtons[2]);
addButton(digitalButtons[3]);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gBagConstraints.gridheight = 2;
addButton(equal);
gBagConstraints.gridheight = 1;
gBagConstraints.gridwidth = 2;
addButton(digitalButtons[0]);
gBagConstraints.gridwidth = 1;
addButton(dot);
}
private void addButton(JButton button) {
gBagLayout.setConstraints(button, gBagConstraints);
digitalArea.add(button);
}
public static void main(String[] args) {
new MyCalculater().init();
}
private void calculate() {
}
class DigitalButton extends JButton {
public DigitalButton() {
// TODO Auto-generated constructor stub
this.setBackground(new Color(0, 0, 0));
}
}
class ArithSymbol extends JButton {
public ArithSymbol() {
// TODO Auto-generated constructor stub
this.setBackground(new Color(128, 128, 128));
}
}
}
Try this one
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
public class MyCalculater
{
private static final Insets insets = new Insets(0, 0, 0, 0);
private Frame frame = new Frame("My-Calculator");
private GridBagConstraints gBagConstraints = new GridBagConstraints();
private GridBagLayout gBagLayout = new GridBagLayout();
private Panel screen = new Panel();
private Panel arithSymbolArea = new Panel();
private Panel digitalArea = new Panel();
private JButton[] digitalButtons = new JButton[10];
public void init() {
// Put a screen for output
screen.add(new TextField(40));
frame.add(screen, BorderLayout.NORTH);
// To put the Arith symbols
arithSymbolArea.setLayout(new GridLayout(6, 4));
frame.add(arithSymbolArea);
digitalArea.setLayout(gBagLayout);
initDigitalArea();
frame.add(digitalArea, BorderLayout.EAST);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
private void initDigitalArea() {
JButton mc = new JButton("mc");
JButton mplus = new JButton("m+");
JButton mminus = new JButton("m-");
JButton mr = new JButton("mr");
JButton ac = new JButton("AC");
JButton pn = new JButton("+/-");
JButton divide = new JButton("÷");
JButton times = new JButton("×");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton equal = new JButton("=");
JButton dot = new JButton(".");
for (int i = 0; i < 10; i++) {
digitalButtons[i] = new JButton(String.valueOf(i));
}
addComponent(mc, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(mplus, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(mminus, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(mr, 3, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(ac, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(pn, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(divide, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(times, 3, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[7], 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[8], 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[9], 2, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(minus, 3, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[4], 0, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[5], 1, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[6], 2, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(plus, 3, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[1], 0, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[2], 1, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[3], 2, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(equal, 3, 4, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(digitalButtons[0], 0, 5, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(dot, 2, 5, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
}
private void addComponent(JButton button, int gridx, int gridy,
int gridwidth, int gridheight, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
anchor, fill, insets, 0, 0);
gBagConstraints = gbc;
addButton(button);
}
private void addButton(JButton button) {
gBagLayout.setConstraints(button, gBagConstraints);
digitalArea.add(button);
}
public static void main(String[] args) {
new MyCalculater().init();
}
private void calculate() {
}
class DigitalButton extends JButton {
public DigitalButton() {
// TODO Auto-generated constructor stub
this.setBackground(new Color(0, 0, 0));
}
}
class ArithSymbol extends JButton {
public ArithSymbol() {
// TODO Auto-generated constructor stub
this.setBackground(new Color(128, 128, 128));
}
}
}

Increase the size of column using gridbaglayout using java swings

I created one program consisting of 3 columns. Now I want only 2nd column to increase its size. Can anyone suggest me the right code? Here is my code
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class PizzaGridBagLayout extends JFrame {
public static void main(String[] args) {
new PizzaGridBagLayout();
}
JTextField name = new JTextField(10), phone = new JTextField(10), pup=new JTextField(10);JComboBox address = new JComboBox(new String[]{"ComboBox 1","hi","hello"});
JComboBox address1 = new JComboBox(new String[]{"ComboBox 2","hi","hello"});
JButton labels=new JButton("labels");
JLabel label1 = new JLabel("label1");
JLabel label2 = new JLabel("label2");
JLabel label3 = new JLabel("Label3");
JLabel label4 = new JLabel("Label4");
JLabel label5 = new JLabel("Label5");
JTextArea area=new JTextArea(1,10);
JTextArea area1=new JTextArea(1,10);
JRadioButton yes = new JRadioButton("yes"),
no = new JRadioButton("no");
JCheckBox box1 = new JCheckBox("box1"), box2 = new JCheckBox("box2"),
box3 = new JCheckBox("box3");
JButton okButton = new JButton("OK"), closeButton = new JButton("Close");
public PizzaGridBagLayout() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// HERE I WANT TO INCREASE THE SIZE OF 2nd COLUMN CONSISTING OF NAME,PHONE,ADDRESS,ADDRESS1,OKBUTTON //
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
addItem(panel1, name, 1, 0,1);
addItem(panel1, phone, 1, 2,1);
addItem(panel1, address, 1, 1,1);
addItem(panel1, address1, 1, 4,1);
addItem(panel1, okButton, 1, 5,1);
Box sizeBox = Box.createVerticalBox();
addItem(panel1, label1, 0, 0,1);
addItem(panel1, label2, 0, 1,1);
addItem(panel1, label3, 0, 2,1);
addItem(panel1, label4, 0, 3,1);
addItem(panel1, label5, 0, 4,1);
Box styleBox = Box.createHorizontalBox();
styleBox.add(yes);
styleBox.add(no);
styleBox.setBorder(BorderFactory.createTitledBorder(""));
addItem(panel1, styleBox, 1, 3,1);
Box topBox = Box.createVerticalBox();
ButtonGroup topGroup = new ButtonGroup();
topBox.add(box1);
topBox.add(box2);
topBox.setBorder(BorderFactory.createTitledBorder(""));
addItem(panel1, topBox, 2, 0,2);
addItem(panel1, pup,2,2,1 );
addItem(panel1, area, 2, 4,1);
addItem(panel1,closeButton,2,5,1);
this.add(panel1);
this.pack();
this.setVisible(true);
}
private void addItem(JPanel p, JComponent c, int x, int y,int height) {
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridheight=height;
gc.weightx = 1.0;
gc.weighty = 1.0;
gc.insets = new Insets(2, 2, 2, 2);
gc.fill = GridBagConstraints.BOTH;
p.add(c, gc);
}
private void addItem1(JPanel p, JComponent c, int x, int y, int z, int a){
GridBagConstraints gc1 = new GridBagConstraints();
gc1.gridwidth=z;
gc1.gridheight=a;
gc1.fill=GridBagConstraints.NONE;
p.add(c,gc1);
}
}
This is the output I'm getting
If I enlarge it 1st column is expanding but I want 2nd column to expand. Can anyone suggest me. Thanks in advance.
gc.weightx = 1.0;
The weightx constraint controls this. You set the value to 1.0 for all columns so each column gets the extra space.
You want the value to be:
0.0 for columns 0, 2 and
1.0 for column 1.
Read the section from the Swing tutorial on How to Use GridBagLayout for more information about the constraints.

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;
}
}

Can't display enough JLabels/TextFields properly

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);
}
}

Categories

Resources