Put space between label in GridLayout - java

package committeeGUI;
import static committeeGUI.CommitteeGUI.comList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StudentMemberFrame extends JFrame {
public StudentMemberFrame() {
super("Add Student");
setSize(450, 500);
setLocation(561, 150);
super.setResizable(false);
addStudentMember();
}
public void addStudentMember() {
CommitteeGUI.frame.setEnabled(false);
final JPanel showConsoleArea = new JPanel(new FlowLayout(FlowLayout.LEFT));
showConsoleArea.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//creating border and size of the border
showConsoleArea.setBorder(BorderFactory.createLineBorder(Color.black, 3));
add(showConsoleArea); //, BorderLayout.CENTER);
//setting a size to showConsoleArea.
showConsoleArea.setSize(500, 500);
final JLabel lblheading = new JLabel("STUDENT");
// showConsoleArea.add(lblheading,BorderLayout.CENTER);
/*
* creating components of company form
*/
final JLabel lblCommitteeName = new JLabel("Committee name");
final JTextField txtName = new JTextField(15);
final JLabel lblMemberName = new JLabel("Student name");
final JTextField txtMemberName = new JTextField(15);
final JLabel lblMemberNumber = new JLabel("Student number");
final JTextField txtMemberNumber = new JTextField(15);
final JLabel lblMemberCourse = new JLabel("Student course");
final JTextField txtMemberCourse = new JTextField(15);
final JButton buttAdd = new JButton("SAVE");
final JButton buttCancel = new JButton("CANCEL");
// adding components to the display area
c.gridx = 1;
c.gridy = 0;
showConsoleArea.add(lblheading, c);
c.gridx = 0;
c.gridy = 1;
showConsoleArea.add(lblCommitteeName, c);
c.gridx = 1;
c.gridy = 1;
showConsoleArea.add(txtName, c);
c.gridx = 0;
c.gridy = 2;
showConsoleArea.add(lblMemberName, c);
c.gridx = 1;
c.gridy = 2;
showConsoleArea.add(txtMemberName, c);
c.gridx = 0;
c.gridy = 3;
showConsoleArea.add(lblMemberNumber, c);
c.gridx = 1;
c.gridy = 3;
showConsoleArea.add(txtMemberNumber, c);
c.gridx = 0;
c.gridy = 4;
showConsoleArea.add(lblMemberCourse, c);
c.gridx = 1;
c.gridy = 4;
showConsoleArea.add(txtMemberCourse, c);
c.gridx = 0;
c.gridy = 5;
showConsoleArea.add(buttAdd, c);
c.gridx = 1;
c.gridy = 5;
showConsoleArea.add(buttCancel, c);
/*
* able to displaying the company frame
*/
this.show();
buttAdd.addActionListener((ActionEvent e) -> {
if (txtName.getText().equals("")
|| txtMemberName.getText().equals("")
|| txtMemberNumber.getText().equals("")
|| txtMemberCourse.getText().equals("")) //validating the data
{
CommitteeGUI.frame.setEnabled(false);
setEnabled(false);
messagebox("Enter a valid data", 0);
return;
}
if (!txtMemberNumber.getText().matches("\\d+")) {
CommitteeGUI.frame.setEnabled(false);
setEnabled(false);
messagebox("Member number must be a integer", 0);
return;
}
for (Committee com : comList) {
if (com.getName().equals(txtName.getText())) {
Student st = new Student();
st.setName(txtMemberName.getText());
st.setAcademicNo(Integer.parseInt(txtMemberNumber.getText()));
st.setCourse(txtMemberCourse.getText());
com.memberList.add(st);
messagebox("Member added successfully", 1);
setEnabled(false);
return;
}
}
messagebox("No Committee found with given name", 1);
});
//creating ActionListner to Cancel button
buttCancel.addActionListener((ActionEvent e) -> {
//frame is enabled for user.
CommitteeGUI.frame.setEnabled(true);
dispose(); //disposing the frame
} //pass the action to actionPerformed method and perform it.
);
}
#SuppressWarnings("deprecation")
public void messagebox(String label, final int conform) {
final JDialog infoBox = new JDialog();//message box
infoBox.setSize(400, 90);
infoBox.setAlwaysOnTop(true);
infoBox.setResizable(false);
infoBox.setLocation(675, 258);
JLabel space = new JLabel(" ");
JLabel label1 = new JLabel(label);
JButton buttOk = new JButton("Ok");
buttOk.addActionListener((ActionEvent e) -> {
if (conform == 1) {
// making frame operation enable.
CommitteeGUI.frame.setEnabled(true);
dispose();
}
setEnabled(true);
infoBox.hide();
});
JPanel holder = new JPanel(new FlowLayout());
holder.add(label1);
holder.add(buttOk);
infoBox.add(holder);
infoBox.show();
}
}
Above is my code. I want to put space between the heading (STUDENT) and the fields.
Attached is the snapshot of the frame:
I am not familiar with this layout. Help is much appreciated.

public void addStudentMember() {
final JPanel showConsoleArea = new JPanel(new FlowLayout(FlowLayout.LEFT));
showConsoleArea.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Add below line of code change appropriate spacing
c.insets = new Insets(10, 10, 10, 10);

Related

Getting rid of extra spaces between components when the JFrame is resized

I am using the GridBagLayout to arrange some components in a frame.
When the frame is first created, the components have a decent space in between them.
But as soon as I resize the frame there are alot of unwanted space between the components
I tried adjusting the weights and insets as suggested by some users, but it does not seem to fix the problem
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JTextArea;
public class Frame1 extends JFrame {
JLabel one = new JLabel("one");
JLabel two = new JLabel("two");
JLabel three = new JLabel("three");
JTextField oneF = new JTextField(20);
JTextField twoF = new JTextField(20);
JTextField threeF = new JTextField(20);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("menu");
GridBagConstraints c = new GridBagConstraints();
public Frame1() {
setTitle("GridBagLayout Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
menuBar.add(menu);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = c.REMAINDER;
c.fill = c.HORIZONTAL;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(menuBar, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(one, c);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(oneF, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(two, c);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(twoF, c);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(three, c);
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(threeF, c);
//setResizable(false);
pack();
setVisible(true);
}
}
ps:- I am new to GUI programming, so please forgive me for any noob mistakes.
edit 1: This is the what I want to have after I am done. I know the currently it does not look anyway near what I have in mind... I am still working on it
Thanks
use an nested layout (combinations of a few LayoutManagers), your picture talks me about,
still you can use GridBagLayout for components placed into left side,
in my code (simplest idea as is possible) JComponents placed on left side can't be resizable because are restricted from LayoutManager`s defaults, more in Oracle tutorial
.
.
painted from SSCCE/MCVE
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame {
private static final long serialVersionUID = 1L;
private JFrame myFrame = new JFrame("Whatever");
private JPanel parentPanel = new JPanel(new BorderLayout(10, 10)) {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
#Override
public Color getBackground() {
return new Color(255, 000, 000);
}
};
private JPanel leftPanel = new JPanel(/*default is FlowLayout*/) {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 400);
}
#Override
public Color getBackground() {
return new Color(255, 255, 000);
}
};
private JPanel leftChildPanel = new JPanel() {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 400);
}
#Override
public Color getBackground() {
return new Color(255, 255, 225);
}
};
private JPanel rightPanel = new JPanel(new BorderLayout(10, 10)) {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 380);
}
#Override
public Color getBackground() {
return new Color(000, 255, 225);
}
};
public MyFrame() {
parentPanel.add(leftPanel, BorderLayout.WEST);
leftPanel.add(leftChildPanel);
parentPanel.add(rightPanel);
myFrame.add(parentPanel);
myFrame.setLocation(150, 150);
myFrame.pack();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new MyFrame();
});
}
}
The idea is to add empty row / columns that will grow to fill the available space:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextField;
public class Frame1 extends JFrame {
JLabel one = new JLabel("one");
JLabel two = new JLabel("two");
JLabel three = new JLabel("three");
JTextField oneF = new JTextField(20);
JTextField twoF = new JTextField(20);
JTextField threeF = new JTextField(20);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("menu");
public Frame1() {
setTitle("GridBagLayout Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0}; //this defines 4 rows
//make 2 last empty row grow
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0,1.0};
//do the same for columns
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0,1.0};
getContentPane().setLayout(gridBagLayout);
menuBar.add(menu);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 5;
c.fill = c.HORIZONTAL;
c.anchor = c.NORTH;
c.insets = new Insets(5, 5, 5, 0);
getContentPane().add(menuBar, c);
//better have a new GridBagConstraints for each component added
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 1;
c1.gridwidth = 1;
c1.fill = c1.NONE;
c1.anchor = c1.NORTH;
c1.insets = new Insets(5, 5, 0, 5);
getContentPane().add(one, c1);
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 1;
c2.gridy = 1;
c2.fill = c2.NONE;
c2.anchor = GridBagConstraints.NORTHWEST;
c2.insets = new Insets(5, 5, 0, 5);
getContentPane().add(oneF, c2);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Frame1();
}
}
EDIT: in response to your edit: use the additional "growing" column for the "cover art"
The problem is your assignments of c.weightx and c.weighty. weightx and weighty determine how extra space is allocated to grid cells in a GridBagLayout when the container is made larger than necessary to accommodate the preferred sizes of the components.
The weightx and weighty should be zero for all cells except those cells which you want to grow larger when the window is made larger.
I have no real idea on how it is supposed to look like, but you could try to set for the labels c.anchor=GridBagConstraints.EAST and c.anchor=GridBagConstraints.WEST for the textfields.
Try also setting c.fill = GridBadConstraints.BOTH.

JTextArea height is only 1 line when using GridBayLayout

I am doing a program that finds words in a text file. I am using GridBagLayout for the position of the elements. When I run the program the text area shows with just one line. Even though it is set JTextArea results = new JTextArea(30, 30)
This is what it shows at the moment:
I am trying to do something like this:
Java code:
public class WordFinder extends JFrame {
private WordList words = new WordList();
private static final int WINDOW_WIDTH = 380;
private static final int WINDOW_HEIGHT = 380;
private static final int TEXT_WIDTH = 30;
private JLabel findLabel = new JLabel("Find:");
private JLabel wordsContaining = new JLabel("words containing");
private JTextField findWord = new JTextField(TEXT_WIDTH);
private JButton clear = new JButton("Clear");
private JTextArea results = new JTextArea(30, 30);
private JScrollPane scroll = new JScrollPane(results);
private JFileChooser chooseFile = new JFileChooser();
private JPanel pane = new JPanel(new GridBagLayout());
public WordFinder() {
super("Word Finder");
// Initialize the menu bar
//initMenu();
results.setEditable(false);
pane.setLayout(new GridBagLayout());
pane.setBorder(new EmptyBorder(15, 20, 0, 10));
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
results.setLineWrap(true);
results.setWrapStyleWord(true);
scroll.setViewportView(results);
// Add label "Find"
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(findLabel, c);
// Add text field
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 1;
c.gridy = 0;
pane.add(findWord, c);
// Add clear button
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = .1;
c.gridx = 2;
c.gridy = 0;
pane.add(clear, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(5, 5, 0, 0);
pane.add(wordsContaining, c);
// Add text area
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1;
c.gridx = 1;
c.gridy = 3;
c.insets = new Insets(0, 3, 0, 5);
pane.add(scroll, c);
add(pane);
setVisible(true);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run () {
new WordFinder().show();
}
});
}
}
Any ideas in what am I missing? or perhaps I am doing something wrong?
Change c.fill = GridBagConstraints.HORIZONTAL; to c.fill = GridBagConstraints.BOTH;
Use pack instead of setSize
For example
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class WordFinder extends JFrame {
private static final int WINDOW_WIDTH = 380;
private static final int WINDOW_HEIGHT = 380;
private static final int TEXT_WIDTH = 30;
private JLabel findLabel = new JLabel("Find:");
private JLabel wordsContaining = new JLabel("words containing");
private JTextField findWord = new JTextField(TEXT_WIDTH);
private JButton clear = new JButton("Clear");
private JTextArea results = new JTextArea(30, 30);
private JScrollPane scroll = new JScrollPane(results);
private JFileChooser chooseFile = new JFileChooser();
private JPanel pane = new JPanel(new GridBagLayout());
public WordFinder() {
super("Word Finder");
// Initialize the menu bar
//initMenu();
results.setEditable(false);
pane.setLayout(new GridBagLayout());
pane.setBorder(new EmptyBorder(15, 20, 0, 10));
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
results.setLineWrap(true);
results.setWrapStyleWord(true);
scroll.setViewportView(results);
// Add label "Find"
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(findLabel, c);
// Add text field
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 1;
c.gridy = 0;
pane.add(findWord, c);
// Add clear button
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = .1;
c.gridx = 2;
c.gridy = 0;
pane.add(clear, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(5, 5, 0, 0);
pane.add(wordsContaining, c);
// Add text area
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.gridx = 1;
c.gridy = 3;
c.insets = new Insets(0, 3, 0, 5);
pane.add(scroll, c);
add(pane);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new WordFinder().setVisible(true);
}
});
}
}
The problem you're having is caused by a combination of using the fill property GridBagConstraints.HORIZONTAL and setSize. When you use setSize, the size of the container is smaller then the JScrollPane's preferredSize and the layout manager is resorting to it's minimumSize instead.
By using GridBagConstraints.BOTH, you are allowing the layout manager to expand the component to fill the entire available space of the cell, regardless

How do I create spacing in between JButtons?

I am trying to create little main menu for a simple scheduling program right now, but I am having a bit of difficulty having some space in between the buttons. They just stick right next to each other, I would like there to be a gap between each button.
I have tried to use the weightx, weighty commands but nothing seems to change.
I would like to have some blank space between the edges of the GUI and the sides of the buttons, and also between each button.
Here's a screenshot
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Scheduler {
JButton VDay, VWeek, Task, Exit;
JFrame wframe, dframe, tframe;
JLabel head;
public void CreateFrame() {
JFrame frame = new JFrame("Main Menu");
ButtonListener btnlst = new ButtonListener();
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
head = new JLabel("The Plain Scheduler");
panel.add(head, c);
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 2;
c.gridwidth = 3;
VDay = new JButton("View Day");
panel.add(VDay, c);
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 3;
c.gridheight = 2;
c.gridwidth = 3;
VWeek = new JButton("View Week");
panel.add(VWeek,c);
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 5;
c.gridheight = 2;
c.gridwidth = 3;
Task = new JButton("Assign/Edit Tasks");
panel.add(Task, c);
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 7;
c.gridheight = 1;
c.gridwidth = 2;
Exit = new JButton("Exit");
panel.add(Exit, c);
VDay.addActionListener(btnlst);
VWeek.addActionListener(btnlst);
Task.addActionListener(btnlst);
Exit.addActionListener(btnlst);
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Scheduler scheduler = new Scheduler();
scheduler.CreateFrame();
}
}
You can use GridBagConstraints#insets to define the amount of spacing/padding which is added to a cell, for example...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Scheduler {
JButton VDay, VWeek, Task, Exit;
JFrame wframe, dframe, tframe;
JLabel head;
public void CreateFrame() {
JFrame frame = new JFrame("Main Menu");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = .25;
c.insets = new Insets(5, 0, 5, 0);
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
head = new JLabel("The Plain Scheduler");
panel.add(head, c);
VDay = new JButton("View Day");
panel.add(VDay, c);
VWeek = new JButton("View Week");
panel.add(VWeek, c);
Task = new JButton("Assign/Edit Tasks");
panel.add(Task, c);
Exit = new JButton("Exit");
panel.add(Exit, c);
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Scheduler scheduler = new Scheduler();
scheduler.CreateFrame();
}
});
}
}

I can't seem to get this inheritance to work

I have a tabbed application. One of the tabs you can enter the name of the company and it should change it to whatever you typed into the other tab. Here are both classes.
On this code its telling me to change About.setCompanyName(str); to static
The error that I am seeing is "Cannot make a static reference to the non-static method SetCompanyName(String) from the type About"
package CourseProject;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class Options extends JPanel{
private JLabel changeLabel;
private JTextField changeName;
private JButton setName;
private JButton exitButton;
public Options(){
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NORTH;
setBackground(Color.WHITE);
super.setLayout(gridbag);
c.insets = new Insets(10, 10, 10, 10);
changeLabel = new JLabel("Change Company Name:");
changeName = new JTextField("", 10);
setName = new JButton("Set New Name");
exitButton = new JButton("Exit");
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
add(changeLabel, c);
c.gridx = 0;
c.gridy = 1;
add(changeName, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
add(setName, c);
setName.addActionListener(new setNameAction());
c.gridx = 1;
c.gridy = 2;
add(exitButton, c);
exitButton.addActionListener(new exitApp());
exitButton.setSize(40,40);
}
class setNameAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str;
str = changeName.getText();
About.SetCompanyName(str);
changeName.setText("");
}
}
class exitApp implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
And here is "About" which contains my setter. It asks me to make the method and variable static but I know this wont work because I am wanting to change it
package CourseProject;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class About extends JPanel{
private JLabel programInfoLabel;
private JLabel programInfo;
private JLabel programmerLabel;
private JLabel programmer;
private JLabel companyLabel;
JLabel company;
public String companyName = "enter a company name in options";
public About() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NORTH;
setBackground(Color.WHITE);
super.setLayout(gridbag);
c.insets = new Insets(10, 10, 10, 10);
programInfoLabel = new JLabel("Program Information:");
programInfo = new JLabel("This is the CIS355A course project application");
programmerLabel = new JLabel("Programmer:");
programmer = new JLabel("Kevin Rankin");
companyLabel = new JLabel("Company Name:");
company = new JLabel(companyName);
c.gridx = 0;
c.gridy = 0;
add(programInfoLabel, c);
c.gridx = 1;
c.gridy = 0;
add(programInfo, c);
c.gridx = 0;
c.gridy = 1;
add(programmerLabel, c);
c.gridx = 1;
c.gridy = 1;
add(programmer, c);
c.gridx = 0;
c.gridy = 2;
add(companyLabel, c);
c.gridx = 1;
c.gridy = 2;
add(company, c);
}
public void SetCompanyName(String str){
company.setText(str);
}
}
On this line
About.SetCompanyName(str);
You're calling SetCompanyName statically (by using the class name "About"). You should either make the method static (which is not the same as "final"; you seem to be confused about this) or create an instance of the About class first, like so:
About myAboutObject = new About();
myAboutObject.SetCompanyName(str);

Proper JFrame components for viewing data entered, and viewing layout correctly on load

As of right now I have all my labels and input fields stacked up the center of the JFrame.
When I run the program the program, the label Business Name appears top center and nothing else appears till I resize the window...
Can anyone tell me why it does this and how to fix it?
Also I would like to add a area to the left side of the JFrame that will display the contact when you click save, what type of component would I use to do this?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import javax.swing.*;
public class Driver extends JFrame {
private JFrame f;
private JPanel p;
private JTextField fieldBN;
private JTextField fieldFN;
private JTextField fieldLN;
private JTextField fieldP;
private JTextField fieldE;
private JTextField fieldA;
private JTextField aLine2;
private JTextField fieldW;
private JLabel labelBN;
private JLabel labelFN;
private JLabel labelLN;
private JLabel labelP;
private JLabel labelE;
private JLabel labelA;
private JLabel labelW;
private JButton buttonS;
// Constructor:
public Driver() {
gui();
}
public void gui() {
f = new JFrame("Contact Book");
f.setVisible(true);
f.setSize(900,800); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10, 10, 10, 10);
p = new JPanel(new GridBagLayout());
f.getContentPane().add(p, BorderLayout.NORTH);
c.gridx = 100;
c.gridy = 0;
labelBN = new JLabel ("Business Name");
p.add(labelBN, c);
c.gridx = 110;
c.gridy = 0;
fieldBN = new JTextField(10);
p.add(fieldBN, c);
c.gridx = 100;
c.gridy = 10;
labelFN= new JLabel ("First Name");
p.add(labelFN, c);
c.gridx = 110;
c.gridy = 10;
fieldFN = new JTextField (10);
p.add(fieldFN, c);
c.gridx = 100;
c.gridy = 20;
labelLN= new JLabel ("Last Name");
p.add(labelLN, c);
c.gridx = 110;
c.gridy = 20;
fieldLN = new JTextField (10);
p.add(fieldLN, c);
c.gridx = 100;
c.gridy = 30;
labelP = new JLabel ("Phone Number");
p.add(labelP, c);
c.gridx = 110;
c.gridy = 30;
fieldP = new JTextField (10);
p.add(fieldP, c);
c.gridx = 100;
c.gridy = 40;
labelE = new JLabel ("Email");
p.add(labelE, c);
c.gridx = 110;
c.gridy = 40;
fieldE = new JTextField (10);
p.add(fieldE, c);
c.gridx = 100;
c.gridy = 50;
labelA = new JLabel ("Address");
p.add(labelA, c);
c.gridx = 110;
c.gridy = 50;
fieldA = new JTextField (10);
p.add(fieldA, c);
c.gridx = 110;
c.gridy = 60;
aLine2 = new JTextField (10);
p.add(aLine2, c);
c.gridx = 100;
c.gridy = 70;
labelW = new JLabel ("Website");
p.add(labelW, c);
c.gridx = 110;
c.gridy = 70;
fieldW = new JTextField (10);
p.add(fieldW, c);
c.gridx = 110;
c.gridy = 80;
buttonS = new JButton("Save");
p.add(buttonS, c);
// Window Listeners
addWindowListener(new WindowAdapter() {
}
);
} // End Gui
public static void main(String[] args) {
new Driver();
} // End main Method
} // End class Driver
The probable cause is right here...
public void gui() {
f = new JFrame("Contact Book");
f.setVisible(true);
f.setSize(900,800); // default size is 0,0
You're calling setVisible to early.
The simple solution is to call setVisible once you've set the frame up the way you want...
public void gui() {
f = new JFrame("Contact Book");
// Add everything to the frame...
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
}
Also, extending from JFrame and then creating an instance of JFrame is very confusing. Generally, you shouldn't need to extend from JFrame, just keep creating an instance as you need.
You should also take a look at Initial Threads
For example...
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method

Categories

Resources