GridBagLayout problems
Hi everyone...
i m having some problems with GridBagLayout() manager...
Basically my code concerning GBL manager is:
JPanel mainPanel=new JPanel();
GridBagLayout grid=new GridBagLayout();
mainPanel.setLayout(grid);
Now i want to add 2 more panels to mainPanel and add them some constraints...
JPanel subPanel1=new JPanel();
JPanel subPanel2=new JPanel();
GridBagConstraints gbc1=new GridBagConstraints();
gbc1.weightx=1.0;
gbc1.weighty=1.0;
gbc1.gridx=0;
gbc1.gridy=0;
gbc1.insets=new Insets(1,1,1,1);
GridBagConstraints gbc2=new GridBagConstraints();
gbc2.gridx=1;
gbc2.gridy=1;
mainPanel.setBackground(backgroundColors);
mainPanel.add(subPanel1,gbc1);
mainPanel.add(subPanel2,gbc2);
And the problem that i have is that i cant give subPanel1 and subPanel2 some sort of dimensions of mainPanel...like:
subPanel1.setPreferredSize(new Dimension(mainPanel.getWidth()/4,500));
Amm i was surfing the web a bit...found some stuff...but damn i just cant get it to work properly...so if any one has some idea how i can put this together that would be great...thanx!
Basically i would like to have a simple exaple how to use gridBagLayout() if i m using several JPanels, where one is sort of main JPanel and others are layed on top of it using GridBagLayout to manage them.
null
Hi ... thnx for the link....well i sort of managed to set my Components as i wanted them...uhhhhh theres a lot of properties to set...but its a good layout though...thnx for all ur help.
Similar Messages
-
I have a jframe in which i want to display 2 jpanels (panelA, panelB).
The arrangement for panelA is at cell (1,1) and for panelB at cell(1,3)
cell(0,0) upper-left
Here is my code:
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints constr = new GridBagConstraints();
mainFrame = new JFrame("MAIN-FRAME");
mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
aDesktopPane = new JDesktopPane();
aDesktopPane.setBackground(Color.LIGHT_GRAY);
aDesktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
mainFrame.setContentPane(aDesktopPane);
mainFrame.getContentPane().setLayout(gridBag);
BuildConstraints(constr,0,1,1,1,30,0);
constr.fill=GridBagConstraints.BOTH;
constr.insets = createFeatures.Inset(3,3,3,3);
JPanel justForLayout = new JPanel(gridBag); //i use this panel for helping me at the arrangment
justForLayout.setBackground(Color.LIGHT_GRAY);
mainFrame.getContentPane().add(justForLayout,constr);
BuildConstraints(constr,1,1,1,1,40,70);
constr.fill = GridBagConstraints.HORIZONTAL;
constr.insets = createFeatures.Inset(3,3,3,3);
mainFrame.getContentPane().add(panelA, constr);
BuildConstraints(constr,2,1,1,1,30,0);
constr.fill=GridBagConstraints.BOTH;
constr.insets = createFeatures.Inset(3,3,3,3);
JPanel justForLayout2 = new JPanel(gridBag);//i use this panel for helping me at the arrangment
justForLayout2.setBackground(Color.LIGHT_GRAY);
mainFrame.getContentPane().add(justForLayout2,constr);
BuildConstraints(constr,1,3,1,1,0,30);
constr.fill = GridBagConstraints.HORIZONTAL;
constr.insets = createFeatures.Inset(3,3,3,3);
mainFrame.getContentPane().add(panelB, constr);
public void BuildConstraints (GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}//end method Buildconstrraints()Try this as a demo, you may have to work with it a bit.
JPanel mainPanel = (JPanel) getContentPane();
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JPanel PanelA = new JPanel();
PanelA.setBorder(BorderFactory.createEtchedBorder());
PanelA.add(new JLabel("I'm panel A"));
JPanel PanelB = new JPanel();
PanelB.setBorder(BorderFactory.createEtchedBorder());
PanelB.add(new JLabel("I'm Panel B"));
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(40, 10, 40, 10); //top,left,bottom,right
mainPanel.add(PanelA, gbc);
mainPanel.add(PanelB, gbc); -
More GridBagLayout problems...
Okay, I think I have most of it right, but all of my JLabel's are showing up on top of each other. Below is a quick sketch of how I want it all to look. Then underneath is the code I have so far. Someone please tell me what I'm missing.____________________
| | // this label spans 2 columns
|__________________ |
| | | // these labels span only 1
|_________|_________|Very crude, I know, but I've been struggling with GridBagLayout's for a long time, and I feel I've almost got a hold of them, but not quite. Below is the code that I have:public class SpiffFrame extends JFrame
private Container contentPane;
private final JPanel basePanel;
public SpiffFrame()
super("Spiff Manager");
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane = getContentPane();
basePanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
basePanel.setLayout(gbl);
contentPane.add(basePanel, BorderLayout.NORTH);
JLabel currentUser = new JLabel("Current running Spiffs for current user");
gbc.anchor = GridBagConstraints.NORTHEAST;
add(currentUser, gbc, 0, 0, 2, 1);
JLabel marginListLabel = new JLabel("Margin");
gbc = new GridBagConstraints();
add(marginListLabel, gbc, 0, 1, 0, 1);
JLabel bottleListLabel = new JLabel("Bottle");
gbc = new GridBagConstraints();
add(bottleListLabel, gbc, 1, 1, 1, 1);
// helper method to add components with GridBagConstraints
public void add(Component c, GridBagConstraints constraints, int x, int y, int w, int h)
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
basePanel.add(c, constraints);
}Now, essentially what happens is that all of my labels are appearing on top of each other, when I swear I've got the x and y coords right! Someone point me out what I'm doing wrong? Thanks a lot. I'd offer dukes, but I'm plain out. Not much I can do about it.
Jamesimport java.awt.*;
import javax.swing.*;
public class jmccon extends JFrame {
private Container contentPane;
private final JPanel basePanel;
public jmccon() {
super("Spiff Manager");
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane = getContentPane();
basePanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
basePanel.setLayout(gbl);
contentPane.add(basePanel, BorderLayout.NORTH);
JLabel currentUser = new JLabel("Current running Spiffs for current user");
gbc.insets = new Insets(2,5,2,5);
gbc.gridwidth = 4;
gbc.gridx = 0;
gbc.gridy = 0;
basePanel.add(currentUser, gbc);
gbc.gridwidth = 1;
gbc.gridy = 1;
JLabel marginListLabel = new JLabel("Margin");
basePanel.add(marginListLabel, gbc);
gbc.gridx = 1;
JLabel bottleListLabel = new JLabel("Bottle");
basePanel.add(bottleListLabel, gbc);
setSize(300,200);
setLocation(300,200);
setVisible(true);
public static void main(String[] args) {
new jmccon();
} -
Gridbag layout problem on a tabbedpane
I am not sure whether we can do this or not but, I am trying to have a gridbag layout on a TabbedPane object.
I have a JFrame on which I am adding a TabbedPane object called "t" and on this TabbedPane I am adding a tab called "Insert" which is an object of class "Insert Data". Then, I add the TabbedPane t on the Container cp, which is inside the JFrame.
In the InsertData Class (a JPanel), I need to have the gridbag layout. With this gridbag layout object, I am trying to place different objects like buttons, at various places, on this JPanel. But nothing moves on this panel.
In short, please let me know how can I have a gridbag layout on a Tabbedpane.
The Main Class is as follows:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame implements ActionListener, ChangeListener{
Main(){
setPreferredSize(new Dimension(1200,600));
Container cp = getContentPane();
JTabbedPane t = new JTabbedPane();
// insert
InsertData insertOptions = new InsertData();
t.addTab("Insert",insertOptions);
cp.add(t);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
@Override
public void stateChanged(ChangeEvent arg0) {
// TODO Auto-generated method stub
public static void main(String args[]){
new Main();
}The InsertDataClass is:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class InsertData extends JPanel{
InsertData(){
setPreferredSize(new Dimension(1200,600));
setBackground(Color.blue);
//setLayout(new GridBagLayout());
GridBagLayout gb = new GridBagLayout();
setLayout(gb);
GridBagConstraints c = new GridBagConstraints();
//c.insets = new Insets(2, 2, 2, 2);
//JPanel p1= new JPanel();
//p1.setPreferredSize(new Dimension(200,200));
JButton b1 = new JButton("here i am!!!");
//p1.add(b1);
//c.fill = GridBagConstraints.HORIZONTAL;
//c.anchor = GridBagConstraints.WEST;
c.gridx=0;
c.gridy=1;
add(b1,c);
}how can I have a gridbag layout on a Tabbedpane.Huh? You post an example with just one JButton and no weightx / weighty set and you expect others here to be able to see a problem with your layout?
Also, there's needless, unused code that is just clutter -- like the unimplemented ActionListener and ChaneListener. Recommended reading: [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://mindprod.com/jgloss/sscce.html].
Finally, I don't see any need whatsoever to extend JFrame and JPanel as you are not introducing any new behavior of either. Always favor composition over inheritance.
I plugged in some code I had used for someone else's GridBagLayout problems and this will show you that there's no difference in using GridBagLayout in a tabbed pane component or anywhere else.import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class GridBagInTabbedPane {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridBagInTabbedPane().makeUI();
public void makeUI() {
Font fontButton = new Font("Arial", Font.BOLD, 10);
Font fontLabel = new Font("Arial", Font.BOLD, 15);
JLabel labelEnter = new JLabel("Enter sentences in the text area ");
labelEnter.setFont(fontLabel);
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(630, 280));
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setMinimumSize(new Dimension(630, 280));
JLabel labelDuplicates = new JLabel("Duplicates will appear here");
labelDuplicates.setMinimumSize(new Dimension(650, 30));
labelDuplicates.setFont(fontLabel);
JButton buttonDisplay = new JButton("Display Map");
buttonDisplay.setFont(fontButton);
JButton buttonClear = new JButton("Clear");
buttonClear.setFont(fontButton);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 0.5;
panel.add(labelEnter, gbc);
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 0.5;
panel.add(scrollPane, gbc);
gbc.gridy = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.weighty = 0.0;
panel.add(labelDuplicates, gbc);
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
panel.add(buttonDisplay, gbc);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.WEST;
panel.add(buttonClear, gbc);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add(panel);
JFrame frame = new JFrame();
frame.add(tabbedPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}db -
GridBagLayout and Panel Border problem
I have 3 panels like
A
B
C
and the C panel has a Mouse Listener that on a mouseEntered creates a border around the same panel and on a mouseExited clears that border.
When this border is created the A and B panels go up a little bit .. they move alone when the border is created.
Is there any way to fix this problem? Is there any way to get the panels static?
the code is close to the following:
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.event.*;
import java.text.NumberFormat;
public class Game extends JFrame implements MouseListener
JPanel game, options, top, down, middle;
NumberFormat nf;
public Game() {
super("Game");
nf = NumberFormat.getPercentInstance();
nf.setMaximumFractionDigits(1);
JPanel center = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = gbc.BOTH;
gbc.weighty = 1.0;
gbc.weightx = 0.8;
center.add(getGamePanel(), gbc);
gbc.weightx = 0.104;
center.add(getOptionsPanel(), gbc);
Container cp = getContentPane();
// use the JFrame default BorderLayout
cp.add(center); // default center section
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(700,600);
// this.setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
addComponentListener(new ComponentAdapter()
public void componentResized(ComponentEvent e)
showSizeInfo();
showSizeInfo();
private void showSizeInfo()
Dimension d = getContentPane().getSize();
double totalWidth = game.getWidth() + options.getWidth();
double gamePercent = game.getWidth() / totalWidth;
double optionsPercent = options.getWidth() / totalWidth;
double totalHeight = top.getHeight() + middle.getHeight() + down.getHeight();
double topPercent = top.getHeight() / totalHeight;
double middlePercent = middle.getHeight() / totalHeight;
double downPercent = down.getHeight() / totalHeight;
System.out.println("content size = " + d.width + ", " + d.height + "\n" +
"game width = " + nf.format(gamePercent) + "\n" +
"options width = " + nf.format(optionsPercent) + "\n" +
"top height = " + nf.format(topPercent) + "\n" +
"middle height = " + nf.format(middlePercent) + "\n" +
"down height = " + nf.format(downPercent) + "\n");
private JPanel getGamePanel()
// init components
top = new JPanel(new BorderLayout());
top.setBackground(Color.red);
top.add(new JLabel("top panel", JLabel.CENTER));
middle = new JPanel(new BorderLayout());
middle.setBackground(Color.green.darker());
middle.add(new JLabel("middle panel", JLabel.CENTER));
down = new JPanel(new BorderLayout());
down.setBackground(Color.blue);
down.add(new JLabel("down panel", JLabel.CENTER));
// layout game panel
game = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = gbc.BOTH;
gbc.gridwidth = gbc.REMAINDER;
gbc.weighty = 0.2;
game.add(top, gbc);
gbc.weighty = 0.425;
game.add(middle, gbc);
gbc.weighty = 0.2;
game.add(down, gbc);
down.addMouseListener(this);
return game;
private JPanel getOptionsPanel()
options = new JPanel(new BorderLayout());
options.setBackground(Color.pink);
options.add(new JLabel("options panel", JLabel.CENTER));
return options;
// mouse listener events
public void mouseClicked( MouseEvent e ) {
System.out.println("pressed");
public void mousePressed( MouseEvent e ) {
public void mouseReleased( MouseEvent e ) {
public void mouseEntered( MouseEvent e ) {
Border redline = BorderFactory.createLineBorder(Color.red);
JPanel x = (JPanel) e.getSource();
x.setBorder(redline);
public void mouseExited( MouseEvent e ){
JPanel x = (JPanel) e.getSource();
x.setBorder(null);
public static void main(String[] args ) {
Game exe = new Game();
exe.show();
}import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.text.NumberFormat;
public class Game extends JFrame implements MouseListener{
JPanel game, options, top, down, middle;
NumberFormat nf;
public Game() {
super("Game");
nf = NumberFormat.getPercentInstance();
nf.setMaximumFractionDigits(1);
JPanel center = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = gbc.BOTH;
gbc.weighty = 1.0;
gbc.weightx = 0.8;
center.add(getGamePanel(), gbc);
gbc.weightx = 0.104;
center.add(getOptionsPanel(), gbc);
Container cp = getContentPane();
// use the JFrame default BorderLayout
cp.add(center); // default center section
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(700,600);
// this.setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
showSizeInfo();
showSizeInfo();
private void showSizeInfo(){
Dimension d = getContentPane().getSize();
double totalWidth = game.getWidth() + options.getWidth();
double gamePercent = game.getWidth() / totalWidth;
double optionsPercent = options.getWidth() / totalWidth;
double totalHeight = top.getHeight() + middle.getHeight() + down.getHeight();
double topPercent = top.getHeight() / totalHeight;
double middlePercent = middle.getHeight() / totalHeight;
double downPercent = down.getHeight() / totalHeight;
System.out.println("content size = " + d.width + ", " + d.height + "\n" +
"game width = " + nf.format(gamePercent) + "\n" +
"options width = " + nf.format(optionsPercent) + "\n" +
"top height = " + nf.format(topPercent) + "\n" +
"middle height = " + nf.format(middlePercent) + "\n" +
"down height = " + nf.format(downPercent) + "\n");
private JPanel getGamePanel(){
// init components
top = new JPanel(new BorderLayout());
top.setBackground(Color.red);
top.add(new JLabel("top panel", JLabel.CENTER));
middle = new JPanel(new BorderLayout());
middle.setBackground(Color.green.darker());
middle.add(new JLabel("middle panel", JLabel.CENTER));
down = new JPanel(new BorderLayout());
down.setBackground(Color.blue);
down.add(new JLabel("down panel", JLabel.CENTER));
// layout game panel
game = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = gbc.BOTH;
gbc.gridwidth = gbc.REMAINDER;
gbc.weighty = 0.2;
game.add(top, gbc);
gbc.weighty = 0.425;
game.add(middle, gbc);
gbc.weighty = 0.2;
game.add(down, gbc);
down.addMouseListener(this);
return game;
private JPanel getOptionsPanel(){
options = new JPanel(new BorderLayout());
options.setBackground(Color.pink);
options.add(new JLabel("options panel", JLabel.CENTER));
return options;
public void mouseClicked( MouseEvent e ) {
System.out.println("pressed");
public void mousePressed( MouseEvent e ) {
public void mouseReleased( MouseEvent e ) {
public void mouseEntered( MouseEvent e ) {
Border redline = new CalmLineBorder(Color.red);
JPanel x = (JPanel) e.getSource();
x.setBorder(redline);
public void mouseExited( MouseEvent e ){
JPanel x = (JPanel) e.getSource();
x.setBorder(null);
public static void main(String[] args ) {
Game exe = new Game();
exe.setVisible(true);
class CalmLineBorder extends LineBorder{
public CalmLineBorder(Color c){
super(c);
public CalmLineBorder(Color c, int thick){
super(c, thick);
public CalmLineBorder(Color c, int thick, boolean round){
super(c, thick, round);
public Insets getBorderInsets(Component comp){
return new Insets(0, 0, 0, 0);
} -
Problem with the GridBagLayout Manager
Hello i am new to Java Swing and i am facing a problem with the GridBagLayout layout manager . the code in question is attached. First compile and run the code. It will execute w/o probs . Then go to the "Console" tab. There the diff components (6 buttons and 1 text area) are haphazardly arranged where as all measures where taken to prevent it in the code. The GridBagLayout manager for this tab is not working properly please help.
The code in question:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MainForm extends JFrame{
JTabbedPane jtp = new JTabbedPane();
Container generalContainer; // container for the general pane
Container consoleContainer; // container for the console pane
GridBagLayout consoleLayout = new GridBagLayout(); // GridBagLayout for the console
GridBagConstraints consoleConstraints;// GridBagConstraints for the console
public MainForm()
super("Welcome to Grey Griffin -- Network Simulator");
setSize(700,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel generalPane = new JPanel();
generalPane.setLayout(new BoxLayout(generalPane, BoxLayout.Y_AXIS));
JPanel consolePane = new JPanel();
consolePane.setLayout(new BoxLayout(consolePane, BoxLayout.Y_AXIS));
JPanel designPane = new JPanel();
designPane.setLayout(new BoxLayout(designPane, BoxLayout.Y_AXIS));
JPanel outputPane = new JPanel();
outputPane.setLayout(new BoxLayout(outputPane, BoxLayout.Y_AXIS));
//Setting up Layout for all the tabs
//for general tab
FlowLayout layout= new FlowLayout();
generalContainer = generalPane;
layout.setAlignment(FlowLayout.CENTER);
generalContainer.setLayout( layout );
//for console tab
consoleContainer = consolePane;
consoleConstraints = new GridBagConstraints();
// *******Finished********
//********** All buttons text areas are declared here**********
//*******for the general tab**********
JButton generalCreate = new JButton("Create a New Network");
JButton generalOpen = new JButton("Open an Existing Network");
JButton generalSave = new JButton("Save the Network");
JButton generalSaveAs = new JButton("Save As..........");
JButton generalExit = new JButton("Exit");
//******END******
//*******for the console tab
//text area
JTextArea consoleCode = new JTextArea();
consoleCode.setEditable(true);
consoleCode.setMaximumSize(new Dimension(700,400));
consoleCode.setAlignmentX(0.0f);
//text area complete
//*******for the Console tab**********
JButton consoleCompile = new JButton("Compile Code");
JButton consoleSimulate = new JButton("Simulate Code");
JButton consoleReset = new JButton("Reset");
JButton consoleOpen = new JButton("Open script files");
JButton consoleSave = new JButton("Save script files");
JButton consoleConvert = new JButton("Convert Script files to graphical form");
//***************END****************
//Adding buttons and text areas to there respective tabs
// for the general tab
generalContainer.add(generalCreate);
generalContainer.add(generalOpen);
generalContainer.add(generalSave);
generalContainer.add(generalSaveAs);
generalContainer.add(generalExit);
//****END****
// for the console tab
consoleConstraints.fill = GridBagConstraints.BOTH;
addComp(consoleOpen,0,0,1,1);
consoleConstraints.fill = GridBagConstraints.BOTH;
addComp(consoleSave,1,1,1,1);
consoleConstraints.fill = GridBagConstraints.BOTH;
addComp(consoleConvert,1,2,1,1);
consoleConstraints.fill = GridBagConstraints.BOTH;
addComp(consoleCode,1,0,3,1);
consoleConstraints.fill = GridBagConstraints.BOTH;
addComp(consoleCompile,2,0,1,1);
consoleConstraints.fill = GridBagConstraints.BOTH;
addComp(consoleSimulate,2,1,1,1);
consoleConstraints.fill = GridBagConstraints.BOTH;
addComp(consoleReset,2,2,1,1);
//****END****
// adding the tabs
jtp.addTab("General",null,generalPane,"Choose General Options");
jtp.addTab("Design",null,designPane,"Design your own network!!");
jtp.addTab("Console",null,consolePane,"Type commands in console for designing");
jtp.addTab("Output",null,outputPane,"View output");
getContentPane().add(jtp, BorderLayout.CENTER);
//****END****
//This method is used to ad the buttons in the GridBagLayout of the Console tab
private void addComp( Component c,int row,int column,int width,int height)
// set gridx and gridy
consoleConstraints.gridx=column;
consoleConstraints.gridy=row;
//set gridwidth and grid height
consoleConstraints.gridwidth=width;
consoleConstraints.gridheight=height;
//set constraints
consoleLayout.setConstraints(c,consoleConstraints);
consoleContainer.add(c);
class TabManager implements ItemListener
Component tab;
public TabManager(Component tabToManage)
tab = tabToManage;
public void itemStateChanged(ItemEvent ie)
int index=jtp.indexOfComponent(tab);
if (index!=-1)
jtp.setEnabledAt(index,ie.getStateChange()== ItemEvent.SELECTED);
MainForm.this.repaint();
public static void main(String args[])
MainForm form = new MainForm();
form.setVisible(true);
}Thanks for the suggestions. I did away with the GridBagLayout Altogether :-D
and put all the buttons in a seperate JPanel and added that JPanel into the Console tabs container which was using a BorderLayout this time. Take a look
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MainForm extends JFrame{
JTabbedPane jtp = new JTabbedPane();
public MainForm()
super("Welcome to Grey Griffin -- Network Simulator");
setSize(800,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel generalPane = new JPanel();
generalPane.setLayout(new BoxLayout(generalPane, BoxLayout.Y_AXIS));
JPanel consolePane = new JPanel();
consolePane.setLayout(new BoxLayout(consolePane, BoxLayout.Y_AXIS));
JPanel designPane = new JPanel();
designPane.setLayout(new BoxLayout(designPane, BoxLayout.Y_AXIS));
JPanel outputPane = new JPanel();
outputPane.setLayout(new BoxLayout(outputPane, BoxLayout.Y_AXIS));
//Setting up Layout for all the tabs
//**for the general tab
Container generalContainer;
FlowLayout layoutGeneral= new FlowLayout();
generalContainer = generalPane;
layoutGeneral.setAlignment(FlowLayout.CENTER);
generalContainer.setLayout( layoutGeneral );
//**for the console tab
Container consoleContainer;
consoleContainer = consolePane;
consoleContainer.setLayout(new BorderLayout() );
//Creating buttonpanel for adding the buttons
JPanel buttonPanel1 = new JPanel();
buttonPanel1.setLayout(new GridLayout(1,3));
JPanel buttonPanel2 = new JPanel();
buttonPanel2.setLayout(new GridLayout(1,3));
// All buttons / text areas / images are declared here
//**Buttons for the general tab**//
JButton generalCreate = new JButton("Create a New Network");
JButton generalOpen = new JButton("Open an Existing Network");
JButton generalSave = new JButton("Save the Network");
JButton generalSaveAs = new JButton("Save As..........");
JButton generalExit = new JButton("Exit");
//declaring the buttons
JButton consoleCompile = new JButton("Compile");
JButton consoleRun = new JButton("Run");
JButton consoleReset = new JButton("Reset");
JButton consoleOpen = new JButton("Open script files");
JButton consoleSave = new JButton("Save script files");
JButton consoleConvert = new JButton("Convert Script files to graphical form");
//declares the textarea where the code is written
JTextArea consoleCode = new JTextArea();
consoleCode.setEditable(true);
consoleCode.setMaximumSize(new Dimension(500,600));
consoleCode.setAlignmentX(0.0f);
//Adding buttons and text areas to there respective tabs
//**Buttons and text pads for the general tab**
generalContainer.add(generalCreate);
generalContainer.add(generalOpen);
generalContainer.add(generalSave);
generalContainer.add(generalSaveAs);
generalContainer.add(generalExit);
//adding buttons to the button panel 1
buttonPanel1.add(consoleOpen);
buttonPanel1.add(consoleSave);
buttonPanel1.add(consoleConvert);
//adding buttons to the button panel2
buttonPanel2.add(consoleRun);
buttonPanel2.add(consoleReset);
buttonPanel2.add(consoleCompile);
//adding button panels and textarea
consoleContainer.add(buttonPanel1,BorderLayout.NORTH);
consoleContainer.add(consoleCode,BorderLayout.CENTER);
consoleContainer.add(new JScrollPane(consoleCode));
consoleContainer.add(buttonPanel2,BorderLayout.SOUTH);
//adding the tabs
jtp.addTab("General",null,generalPane,"Choose General Options");
jtp.addTab("Design",null,designPane,"Design your own network!!");
jtp.addTab("Console",null,consolePane,"Type commands in console for designing");
jtp.addTab("Output",null,outputPane,"View output");
getContentPane().add(jtp, BorderLayout.CENTER);
class TabManager implements ItemListener
Component tab;
public TabManager(Component tabToManage)
tab = tabToManage;
public void itemStateChanged(ItemEvent ie)
int index=jtp.indexOfComponent(tab);
if (index!=-1)
jtp.setEnabledAt(index,ie.getStateChange()== ItemEvent.SELECTED);
MainForm.this.repaint();
public static void main(String args[])
MainForm form = new MainForm();
form.setVisible(true);
} -
HELP, HELP, HELP, Problems using GridBagLayout with a BorderLayout
I have a BorderLayout for my application. I created a JPanel that has the following items added to it:
1)Button
2)VisualComponent
3)ControllerComponent
The JPanel is then added to the WEST Pane of the BorderLayout. My problem is that I can not get my components in the JPanel to display correctly. The items should be added from top to buttom in my JPanel, but they overlap and display side by side, and some don't appear at all until my entire window application is Maximized. It seems as if all my components are trying to display in one row even though I explicitly add them to different rows as follows:
1)addUsingGBL(openFile,0,1,2,1);
2)addUsingGBL(visualComponent,1,1,1,3);
3)addUsingGBL(controlsComponent,4,1,2,1);
where addUsingGBL is as follows:
void addUsingGBL(Component component,int row, int column, int width, int height)
gbc. gridx = row;
gbc.gridy = column;
gbc.gridwidth = width;
gbc.gridheight = height;
gbLayout.setConstraints(component, gbc);
mediaPanel.add(component);
mediaPanel.doLayout();
If anyone has any suggestions, I would greatly appreciate it. Entire code is available if needed. Thanks!Here is the code, sorry, didn't see that part before.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.media.*;
public class MediaPlayerDemo extends JFrame /*implements ControllerListener*/{
private Player player;
private File file;
public static JPanel mediaPanel;
public static JScrollPane scrollableMediaPanel;
//public static JFrame navigation = new JFrame("Lessons");
public static Component visualComponent;
public static Component controlsComponent;
private static ImageIcon background = new ImageIcon("c:/Tony.jpg");
public static GridBagConstraints gbc;
public static GridBagLayout gbLayout;
//public static panel column;
//public static Container c;
// inner class to handler events from media player
private class EventHandler implements ControllerListener
public void controllerUpdate( ControllerEvent e )
if ( e instanceof EndOfMediaEvent)
//if ( player == null )
//return;
player.close();
//if ( visualComponent != null )
mediaPanel.remove( visualComponent );
//if ( controlsComponent != null )
mediaPanel.remove( controlsComponent );
mediaPanel.doLayout();
// doLayout();
if ( e instanceof RealizeCompleteEvent )
//c = getContentPane();
// load Visual and Control components if they exist
visualComponent =
player.getVisualComponent();
//visualComponent.setSize(5,5);
mediaPanel.doLayout();
//**Sets the layout for mediaPanel as GridLayout**
//**GridLayouts set the rows and col, **
//**& adds contentslt to rt **
//scrollableMediaPanel= new JScrollPane(mediaPanel);
//scrollableMediaPanel= new JScrollPane(mediaPanel);
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
if ( visualComponent != null )
//add comp #2
addUsingGBL(visualComponent,1,1,1,3);
controlsComponent =
player.getControlPanelComponent();
if ( controlsComponent != null )
//add comp #3
//mediaPanel.add(controlsComponent);
addUsingGBL(controlsComponent,1,1,1,3);
controlsComponent =
player.getControlPanelComponent();
/*if ( controlsComponent != null )
//add comp #3
addUsingGBL(controlsComponent,4,1,2,1);
doLayout();//after all components are added, you must redo your layout
//**The following is for adding the a container only **
//**getContentPane().add(mediaPanel,BorderLayout.WEST);**
//**getContenPane().doLayout(); **
//**setSize(200,700);//(width, Height) **
//**show(); **
}//if
}//controllerUpdate
}//eventHandler
public MediaPlayerDemo()
//**Title for a container **
//**super( "Demonstrating the Java Media Player" );**
mediaPanel = new panelBack(background);//instantiate JPanel Object
gbLayout = new GridBagLayout();
mediaPanel.setAlignmentY(TOP_ALIGNMENT);
mediaPanel.setLayout(gbLayout);
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
//gbc.anchor = GridBagConstraints.NONE;
//instatiate JButton object
JButton openFile = new JButton( "Open file to play" );
//openFile.setBounds(0, 0, 25, 25);
//openFile.setVisible(true);
//JBUTTON ActionListener
openFile.addActionListener
new ActionListener()
public void actionPerformed( ActionEvent e )
openFile();
createPlayer();
);//addActionListener
//add comp #1
addUsingGBL(openFile,0,1,2,1);
//set scrollPane
scrollableMediaPanel = new JScrollPane(mediaPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
//Set Border
scrollableMediaPanel.setViewportBorder(new BevelBorder(BevelBorder.RAISED));
}//Constructor
private void createPlayer()
if ( file == null )
return;
removePreviousPlayer();
try
// create a new player and add listener
player = Manager.createPlayer( file.toURL() );
player.addControllerListener( new EventHandler() );
player.start(); // start player
catch ( Exception e )
JOptionPane.showMessageDialog( this,
"Invalid file or location", "Error loading file",
JOptionPane.ERROR_MESSAGE );
//Main only useful if creating a container to hold menuPanel
public static void main(String args[])
MediaPlayerDemo app = new MediaPlayerDemo();
app.getContentPane().setLayout(new BorderLayout(0,0));
app.getContentPane().setBackground(Color.white);
app.getContentPane().add(scrollableMediaPanel, BorderLayout.WEST);
app.getContentPane().validate();//re-layout a container for which the layout has changed
app.setSize(1000,700);//(width, height)
app.show();
app.addWindowListener
new WindowAdapter()
public void windowClosing( WindowEvent e )
System.exit(0);
private void openFile()
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
// user clicked Cancel button on dialog
if ( result == JFileChooser.CANCEL_OPTION )
file = null;
else
file = fileChooser.getSelectedFile();
private void removePreviousPlayer()
if ( player == null )
return;
player.close();
if ( visualComponent != null )
mediaPanel.remove( visualComponent );
if ( controlsComponent != null )
mediaPanel.remove( controlsComponent );
//set background for the JPanel
public class panelBack extends JPanel
ImageIcon image;
panelBack(ImageIcon image)
super();
this.image=image;
public void paintComponent(Graphics g)
image.paintIcon(this,g,0, 0);
}//panelback
void addUsingGBL(Component component,int row, int column, int width, int height)
gbc = new GridBagConstraints();
gbc. gridx = row;
gbc.gridy = column;
gbc.gridwidth = width;
gbc.gridheight = height;
gbLayout.setConstraints(component, gbc);
mediaPanel.add(component);
mediaPanel.doLayout();
}//MediaPlayerDemo -
Facing problem while adding combobox using gridbaglayout
HI, I have used gridbaglayout in my panel. after adding the gridbaglayout, if i select long value combobox does't display all the value of the selected value. Why?
This is my code
cboPrioritySearchValue = new JComboBox();
cboPrioritySearchValue.setBackground(Color.white);
cboPrioritySearchValue.addItem("ALL");
cboPrioritySearchValue.addItem(Priority.NOT.getValue());
cboPrioritySearchValue.addItem(Priority.ONE.getValue());
cboPrioritySearchValue.addItem(Priority.TWO.getValue());
cboPrioritySearchValue.addItem(Priority.THREE.getValue());
cboPrioritySearchValue.addItem(Priority.FOUR.getValue());
setConstraints(gbc,gbc.WEST,gbc.NONE,2,1,2,8,new Insets(2,5,5,5),0,0,0,0);
gbl.setConstraints(cboPrioritySearchValue,gbc);
panel.add(cboPrioritySearchValue);This is my setConstraints method,
public GridBagConstraints setConstraints(GridBagConstraints gbc, int anchor,int fill,
int gridw, int gridh, int gridx, int gridy,
Insets insets, int ipadx, int ipady,
int weightx, int weighty)
gbc.anchor = anchor;
gbc.fill = fill;
gbc.gridwidth = gridw;
gbc.gridheight = gridh;
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.insets = insets;
gbc.ipadx = ipadx;
gbc.ipady = ipady;
gbc.weightx = weightx;
gbc.weighty = weighty;
return gbc;
}Edited by: JavaHeroPrince on May 13, 2010 6:58 AMThanks Pete, hopefully its only a temporary problem.Short answer: It is (well ... hopefully).
Long answer: My server's dying regularly because all the connections fill up in CLOSE_WAIT states. This doesn't seem to have the same root cause as the reported bugs that I can find (issues with older JDKs) so I'm interested in investigating it beyond just upgrading everything to the latest and crossing my fingers. Unfortunately that will take time and I'm a bit pressed for spare time at the moment - so: my apologies; the domain is likely to be down until after the weekend at least.
After the weekend I'll re-target sscce.org (and .com and .net which I acquired a little while back) to a different server that's not suffering from the problem. Or if Andrew prefers I'll target a server under his control. -
I have a serious problem with GridBagLayout please help
I have an working code of an Swing application that has used gridbaglayout it works very well in desktop pc on 2000/xp with 800 x 600 screen resolution but the problem is when i change the screen resolution to higher say at 1152 x 864 resolution then the same code all the components are left align and not properly distributed even though i have GridbagConstraints.BOTH for the fill variable of the Gridbaglayout. Also i am facing similar problem when i run the code on the laptop.
Please help me and can anyone tell me what to do in case i want the layout to show in the same manner for higher resolution as it is showing for lower resolution.you may have to setPreferredSize to a calulation from the screen resolution, if there is 1 thing that is 1 pixel too big for it, i find it stuffs the entire container
Drogo -
Hello all,
I have to implement a JPanel with several input items and use the GridBagLayout manager. My problem is, that the size of some text items won't be set to the preferred size (they are only 1px width as long as I don't set the attribute "fill" to another value than "none").
The Grid looks like this:
c1 c2 c3 c4
r1 label1 textitem1 label2 textitem2
r2 label3 textarea1 textarea1 textarea1 -> span 3
r3 label4 textarea2 textarea2 textarea2 -> span 3
r4 label5 textitem3 -> span 1
All constraints for the used item are the same except gridwidth, gridx and gridy. I already tried to set the widthx property to several values but that didn't work either.
Has anybody any ideas?
Thanks for your efforts and best regards,
Jens[url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use GridBagLayout
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the posted code retains its original formatting. -
[b]Ergent[/b] problem of handling GridBagLayout
Hi everyone. I am doing my assignment using GridBagLayout. I've met a problem. I have developed several levels of JPanel, namely 0,1,2. When I set the lowest level of pane0 and added several labels and textfields by using :
GridBagConstraints c = new GridBagConstraints();
setGrid(c,0,0,1,0,0,0,0);
pane0.add(label1,c);
setGrid(c,1,0,1,0,0,0,0);
pane0.add(label2,c);
that's fine. But when I added a pane1 on the top of pane0 and used the same approach again, the problem was there: I couldn't set the top pane's items (buttons, labels) correctly into the grid i wanted. For example:
setGrid(c,0,2,1,0,0,0,0);
pane1.add(label3,c);
setGrid(c,1,2,1,0,0,0,0);
pane1.add(label4,c);
setGrid(c,0,2,2,0,0,0,0); // occupy two columns for the upper pane1
pane0.add(pane1,c);
They (items) seem to be placed at the (0,0) and occupied two colunms in the grid.(0,0 and 1,0). But what I wanted was put them in (0,2) and (1,2) occupying one column seperately.
Can someone give any solution for this problem?
Thanks in advance.
CheersOne more thing I want to say is I think it is possible to create a upper pane on a pane with GridBagLayout already and set the GridBagLayout again on the upper pane. Are these two layout interact with each other? If, what is the substitue for implementing my thinking.
Thanks a lot. -
Problem with gridbaglayout in JPanel
Hello I am trying to display contact information in three serperate Jpanels on tabbbed panes.I would like to get help in configuring maybe just the pane of void showPane1().I need something like
Searchlb | Combodropname1
firstnamelb | firstnametxt | lastnamelb | lastnametxt
Addresslb addresslb.Horizontal-----------------------*-
citylb | citytxt | statelb | statetxt
postcodelb | postcodetxt | countrylb | countrytxt
emaillb | emailtxt | homephonelb | homephonetxt
faxnumberlb | faxnumbertxt
Other panes have buttons
Here`s the code it operates on login of MSAccess table called persons:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import java.util.Date;
import java.text.NumberFormat;
public class Addressbook extends JFrame{
// Define constant variables
final static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
final static String url = "jdbc:odbc:addBKTAFE";
final static String login = "LOG-IN";
final static String tab_login = "Log-in";
final static String tab1 = "INQUIRE Personnel Details";
final static String tab2 = "UPDATE/DELETE Personnel Details";
final static String tab3 = "INSERT Personnel Details";
final static String insert = "SAVE RECORD";
final static String update = "UPDATE RECORD";
final static String delete = "DELETE RECORD";
final static String inquire = "INQUIRE RECORD";
final static String clear = " CLEAR ";
final static String relogin = "Log-in failed! Please relog-in!";
final static String norecfound = "No Record Found!";
final static String recinserted = "Record Inserted!";
final static String recupdated = "Record Updated!";
final static String recdeleted = "Record Deleted!";
final static String numericerror = "Age should be numeric!";
final static String information = "INFORMATION";
final static String error = "ERROR";
final static String genexception = "GENERAL EXCEPTION";
final static String sqlexception = "SQL EXCEPTION";
final static String confdelete = "CONFIRM DELETE";
final static String slash = "/";
final static String table1 = "persons";
final static String table2 = "Addresses";
// Events events = new Events(this);
// Define variables for general use
String sql = ""; // Used to store sql statements
int pane_number = 0; // Used to indicate what screen needs to be processed
// like resetting input fields and comboboxes
boolean abort = false;// Used to indicate if error found to avoid further
// processing/validations
// Define container, panels and tabbedpane
Container cntr = getContentPane();
JTabbedPane tpane = new JTabbedPane();
JPanel cbpanel1 , cbpanel2 , cbpanel3,
panel1 , panel2 , panel3;
// Setup constraints and type of layout
GridBagConstraints constraints = new GridBagConstraints();
GridBagConstraints constraints1 = new GridBagConstraints();
GridBagConstraints constraints2 = new GridBagConstraints();
GridBagConstraints constraints3 = new GridBagConstraints();
GridBagLayout layout = new GridBagLayout ();
// Define fonts to be used
Font labelFont = new Font("Arial",Font.PLAIN,12);
Font buttonFont = new Font("Arial",Font.BOLD,13);
// Define labels
JLabel lbUser = new JLabel("Enter User ID: " );
JLabel lbPassword = new JLabel("Enter Password: ");
JLabel lbSelectName = new JLabel("Search Name: " );
JLabel lbFirstName = new JLabel("First Name: " );
JLabel lbLastName = new JLabel("Last Name: " );
JLabel lbAddress = new JLabel("Address: " );
JLabel lbCity = new JLabel("City" );
JLabel lbState = new JLabel("State: " );
JLabel lbPostcode = new JLabel("Postcode" );
JLabel lbCountry = new JLabel("Country" );
JLabel lbEmailAddress = new JLabel("Email Address: " );
JLabel lbHomeNumber = new JLabel("Home Phone No.: " );
JLabel lbFaxNumber = new JLabel("Fax No.: " );
// Define combo boxes in third screen (insert pane)
JComboBox cbName1 = new JComboBox();
JComboBox cbPersonId1 = new JComboBox();
// Define combo boxes in second (update/delete pane)
JComboBox cbName2 = new JComboBox();
JComboBox cbPersonId2 = new JComboBox();
// Define buttons, text fields and password field
JButton btLogin = new JButton (login );
JButton btInsert = new JButton (insert );
JButton btUpdate = new JButton (update );
JButton btDelete = new JButton (delete );
JButton btInquire = new JButton (inquire);
JButton btClear = new JButton (clear );
JPasswordField jpPassword = new JPasswordField(10 );
JTextField tfUser = new JTextField("",10 );
// Inquiry fields on first screen (inquiry pane)
JTextField tfFirstName1 = new JTextField("",30);
JTextField tfLastName1 = new JTextField("",30);
JTextField tfAddress1 = new JTextField("",30);
JTextField tfCity1 = new JTextField("",15);
JTextField tfState1 = new JTextField("",15);
JTextField tfPostcode1 = new JTextField("",30);
JTextField tfCountry1 = new JTextField("",15 );
JTextField tfEmailAddress1 = new JTextField("",30);
JTextField tfHomeNumber1 = new JTextField("",15);
JTextField tfFaxNumber1 = new JTextField("",15 );
// Input fields on second screen (update/delete pane)
JTextField tfFirstName2 = new JTextField("",30);
JTextField tfLastName2 = new JTextField("",30);
JTextField tfAddress2 = new JTextField("",30);
JTextField tfCity2 = new JTextField("",15);
JTextField tfState2 = new JTextField("",15);
JTextField tfPostcode2 = new JTextField("",30);
JTextField tfCountry2 = new JTextField("",15 );
JTextField tfEmailAddress2 = new JTextField("",30);
JTextField tfHomeNumber2 = new JTextField("",15);
JTextField tfFaxNumber2 = new JTextField("",15 );
// Input fields on third screen (insert pane)
JTextField tfFirstName3 = new JTextField("",30);
JTextField tfLastName3 = new JTextField("",30);
JTextField tfAddress3 = new JTextField("",30);
JTextField tfCity3 = new JTextField("",15);
JTextField tfState3 = new JTextField("",15);
JTextField tfPostcode3 = new JTextField("",30);
JTextField tfCountry3 = new JTextField("",15 );
JTextField tfEmailAddress3 = new JTextField("",30);
JTextField tfHomeNumber3 = new JTextField("",15);
JTextField tfFaxNumber3 = new JTextField("",15 );
//------------------------------------------------------------------------------>>>
//-----------------------Start Addressbook()------------------------------------>>>
Addressbook(){
// define listener after adding items to CB to avoid triggering it
// cbName1.addItemListener(new ItemListener());
// public void itemStateChanged(ItemEvent e){
//--------------------------END Addressbook constructor------------------------------------->>>
//------------------------------------------------------------------------------>>>
//------------------------------------------------------------------------------>>>
//--------------------Start setupLoginPanel()----------------------------------->>>
// Setup the login screen
public void setupLoginPanel(){
// set application title
setTitle("Address Book Application");
// center screen
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width
- getWidth())/2,
(Toolkit.getDefaultToolkit().getScreenSize().height
- getHeight())/2);
panel1 = new JPanel();
// set screen border
panel1.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab_login, panel1);
// add panel to container
cntr.add(tpane);
// setup layout as GridBagLayout
constraints.insets = new Insets(2,2,2,2);
panel1.setLayout(layout);
// setup User ID label in display area
lbUser.setFont(labelFont);
constraints.ipadx = 2;
constraints.ipady = 2;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbUser, constraints);
panel1.add(lbUser);
// setup User ID input field in display area
tfUser.setFont(labelFont);
constraints.ipadx = 2;
constraints.ipady = 2;
constraints.gridx = 1;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfUser, constraints);
panel1.add(tfUser);
// setup Password label in display area
lbPassword.setFont(labelFont);
constraints.ipadx = 2;
constraints.ipady = 2;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbPassword, constraints);
panel1.add(lbPassword);
// setup Password input field in display area
jpPassword.setEchoChar('*');
constraints.ipadx = 2;
constraints.ipady = 2;
constraints.gridx = 1;
constraints.gridy = 1;
layout.setConstraints(jpPassword, constraints);
panel1.add(jpPassword);
// setup Login button in display area
btLogin.setFont(buttonFont);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridy = 3;
constraints.gridx = 1;
layout.setConstraints(btLogin, constraints);
panel1.add(btLogin);
// setup login button listener
btLogin.addActionListener(new ButtonHandler());
// allow ALT L to press login button
btLogin.setMnemonic('l');
//--------------------End setupLoginPanel()------------------------------------->>>
//--------------------Start login()--------------------------------------------->>>
// Validate user input from the login screen based on information from login
// table (note: manually create/update your login from table LOGIN).
public void login(){
String user = tfUser.getText();
user = user.trim();
char[] pw = jpPassword.getPassword();
String password = new String(pw).trim();
sql = "SELECT * FROM persons WHERE username='"+
user+"' AND password='"+password+"'";
try{
// load MS Access driver
Class.forName(driver);
}catch(java.lang.ClassNotFoundException ex){
JOptionPane.showMessageDialog(null,ex.getMessage(), error ,
JOptionPane.PLAIN_MESSAGE);
try{
// setup connection to DBMS
Connection conn = DriverManager.getConnection(url);
// create statement
Statement stmt = conn.createStatement();
// execute sql statement
stmt.execute(sql);
ResultSet rs = stmt.getResultSet();
boolean recordfound = rs.next();
if (recordfound){
tpane.removeTabAt(0);
showPane1();
showPane2();
showPane3();
else{
// username/password invalid
JOptionPane.showMessageDialog(null,relogin, error,
JOptionPane.INFORMATION_MESSAGE);
//clear login and password fields
tfUser.setText ("");
jpPassword.setText("");
conn.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage(), genexception,
JOptionPane.INFORMATION_MESSAGE);
//--------------------End login()----------------------------------------------->>>
//--------------------Start showPane1()----------------------------------------->>>
// Setup screen 1(inquiry pane) including labels, input fields, comboboxes.
// Table PERSONS is read to list inquiry parameters.
void showPane1(){
panel1 = new JPanel();
cbpanel1 = new JPanel();
// set screen border
panel1.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab1, panel1);
// setup layout as GridBagLayout
constraints1.insets = new Insets(2,2,2,2);
panel1.setLayout (layout);
cbpanel1.setLayout (layout);
// setup Name combobox label
lbSelectName.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 0;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbSelectName, constraints1);
panel1.add(lbSelectName);
// setup Name combobox as search key
cbName1.setFont(labelFont);
constraints1.ipady = 10;
constraints1.gridx = 1;
constraints1.gridy = 0;
constraints1.gridwidth = 3;
constraints1.anchor = GridBagConstraints.WEST;
constraints1.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(cbName1, constraints1);
panel1.add(cbName1);
// setup search combobox (Name and corresponding key)
cbName1.addItem ("Choose one:");
cbPersonId1.addItem("0");
// setup First Name label in display area
lbFirstName.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 1;
constraints1.anchor = GridBagConstraints.WEST;
constraints1.fill = GridBagConstraints.NONE;
constraints1.gridwidth = 1;
layout.setConstraints(lbFirstName, constraints1);
panel1.add(lbFirstName);
// setup First Name input field in display area
tfFirstName1.setFont(labelFont);
tfFirstName1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 1;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName1, constraints1);
panel1.add(tfFirstName1);
// setup Last Name label in display area
lbLastName.setFont(labelFont);
constraints1.gridx = 2;
constraints1.gridy = 1;
constraints1.anchor = GridBagConstraints.WEST;
constraints1.fill = GridBagConstraints.NONE;
layout.setConstraints(lbLastName, constraints1);
panel1.add(lbLastName);
// setup Last Name input field in display area
tfLastName1.setFont(labelFont);
tfLastName1.setEditable(false);
constraints1.gridx = 3;
constraints1.gridy = 1;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfLastName1, constraints1);
panel1.add(tfLastName1);
// setup Address label in display area
lbAddress.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 2;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbAddress, constraints1);
panel1.add(lbAddress);
// setup Address input field in display area
tfAddress1.setFont(labelFont);
tfAddress1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 2;
constraints1.gridwidth = 3;
constraints1.anchor = GridBagConstraints.WEST;
constraints1.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfAddress1, constraints1);
panel1.add(tfAddress1);
// setup City label in display area
lbCity.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 3;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCity, constraints1);
panel1.add(lbCity);
// setup City input field in display area
tfCity1.setFont(labelFont);
tfCity1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 3;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCity1, constraints1);
panel1.add(tfCity1);
// setup State label in display area
lbState.setFont(labelFont);
constraints1.gridx = 2;
constraints1.gridy = 3;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbState, constraints1);
panel1.add(lbState);
// setup State input field in display area
tfState1.setFont(labelFont);
tfState1.setEnabled(false);
constraints1.gridx = 3;
constraints1.gridy = 3;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfState1, constraints1);
panel1.add(tfState1);
// setup Postcode label in display area
lbPostcode.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 4;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbPostcode, constraints1);
panel1.add(lbPostcode);
// setup Address input field in display area
tfPostcode1.setFont(labelFont);
tfPostcode1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 4;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfPostcode1, constraints1);
panel1.add(tfPostcode1);
// setup Country label in display area
lbCountry.setFont(labelFont);
constraints1.gridx = 2;
constraints1.gridy = 4;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCountry, constraints1);
panel1.add(lbCountry);
// setup Country input field in display area
tfCountry1.setFont(labelFont);
tfCountry1.setEditable(false);
constraints1.gridx = 3;
constraints1.gridy = 4;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCountry1, constraints1);
panel1.add(tfCountry1);
// setup Email Address label in display area
lbEmailAddress = new JLabel ("Email Address:");
lbEmailAddress.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 5;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbEmailAddress, constraints1);
panel1.add(lbEmailAddress);
// setup Email Address input field in display area
tfEmailAddress1.setFont(labelFont);
constraints1.gridx = 1;
constraints1.gridy = 5;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfEmailAddress1, constraints1);
panel1.add(tfEmailAddress1);
// setup Home Phone Number label in display area
lbHomeNumber.setFont(labelFont);
constraints1.gridx = 2;
constraints1.gridy = 5;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbHomeNumber, constraints1);
panel1.add(lbHomeNumber);
// setup Home Phone Number input field in display area
tfHomeNumber1.setFont(labelFont);
tfHomeNumber1.setEditable(false);
constraints1.gridx = 3;
constraints1.gridy = 5;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfHomeNumber1, constraints1);
panel1.add(tfHomeNumber1);
// setup Fax Number label in display area
lbFaxNumber.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 6;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbFaxNumber, constraints1);
panel1.add(lbFaxNumber);
// setup Fax Number input field in display area
tfFaxNumber1.setFont(labelFont);
tfFaxNumber1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 6;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFaxNumber1, constraints1);
panel1.add(tfFaxNumber1);
// indicate inquiry pane
pane_number = 1;
// read table get the list of names in CB search key
accessDBInit();
// define listener after adding items to CB to avoid triggering it
cbName1.addItemListener(new ComboBoxHandler());
//--------------------End showPane1()------------------------------------------->>>
//--------------------Start showPane2()----------------------------------------->>>
// Setup screen 2(update and delete pane) including labels, input fields,
// comboboxes, and buttons. Table PERSONS is read to list inquiry parameters.
void showPane2(){
panel2 = new JPanel();
cbpanel2 = new JPanel();
labelFont = new Font("Arial",Font.PLAIN,12);
buttonFont = new Font("Arial",Font.BOLD,12);
// set screen border
panel2.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab2, panel2);
// setup layout as GridBagLayout
constraints2.insets = new Insets(2,2,2,2);
panel2.setLayout (layout);
cbpanel2.setLayout (layout);
// setup Name combobox label
lbSelectName.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 0;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbSelectName, constraints2);
panel1.add(lbSelectName);
// setup Name combobox as search key
cbName2.setFont(labelFont);
constraints2.ipady = 10;
constraints2.gridx = 1;
constraints2.gridy = 0;
constraints2.gridwidth = 3;
constraints2.anchor = GridBagConstraints.WEST;
constraints2.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(cbName1, constraints2);
panel1.add (cbName1);
// setup search combobox (Name and corresponding key)
cbName1.addItem ("Choose one:");
cbPersonId1.addItem("0");
// setup First Name label in display area
lbFirstName.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 1;
constraints2.anchor = GridBagConstraints.WEST;
constraints2.fill = GridBagConstraints.NONE;
constraints2.gridwidth = 1;
layout.setConstraints(lbFirstName, constraints2);
panel1.add(lbFirstName);
// setup First Name input field in display area
tfFirstName2.setFont(labelFont);
tfFirstName2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 1;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName2, constraints2);
panel1.add(tfFirstName2);
// setup Last Name label in display area
lbLastName.setFont(labelFont);
constraints2.gridx = 2;
constraints2.gridy = 1;
constraints2.anchor = GridBagConstraints.WEST;
constraints2.fill = GridBagConstraints.NONE;
layout.setConstraints(lbLastName, constraints2);
panel1.add(lbLastName);
// setup Last Name input field in display area
tfLastName2.setFont(labelFont);
tfLastName2.setEditable(false);
constraints2.gridx = 3;
constraints2.gridy = 1;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfLastName2, constraints2);
panel1.add(tfLastName2);
// setup Address label in display area
lbAddress.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 2;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbAddress, constraints2);
panel1.add(lbAddress);
// setup Address input field in display area
tfAddress2.setFont(labelFont);
tfAddress2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 2;
constraints2.gridwidth = 3;
constraints2.anchor = GridBagConstraints.WEST;
constraints2.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfAddress2, constraints2);
panel1.add(tfAddress2);
// setup City label in display area
lbCity.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 3;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCity, constraints2);
panel1.add(lbCity);
// setup City input field in display area
tfCity2.setFont(labelFont);
tfCity2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 3;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCity2, constraints2);
panel1.add(tfCity2);
// setup State label in display area
lbState.setFont(labelFont);
constraints2.gridx = 2;
constraints2.gridy = 3;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbState, constraints2);
panel1.add(lbState);
// setup State input field in display area
tfState2.setFont(labelFont);
tfState2.setEnabled(false);
constraints2.gridx = 3;
constraints2.gridy = 3;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfState2, constraints2);
panel1.add(tfState2);
// indicate inquiry pane
pane_number = 1;
// setup Address label in display area
lbPostcode.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 4;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbPostcode, constraints2);
panel1.add(lbPostcode);
// setup Address input field in display area
tfPostcode2.setFont(labelFont);
tfPostcode2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 4;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfPostcode2, constraints2);
panel1.add(tfPostcode2);
// setup Country label in display area
lbCountry.setFont(labelFont);
constraints2.gridx = 2;
constraints2.gridy = 4;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCountry, constraints2);
panel1.add(lbCountry);
// setup Country input field in display area
tfCountry2.setFont(labelFont);
tfCountry2.setEditable(false);
constraints2.gridx = 3;
constraints2.gridy = 4;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCountry2, constraints2);
panel1.add(tfCountry2);
// setup Email Address label in display area
lbEmailAddress = new JLabel ("Email Address:");
lbEmailAddress.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 5;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbEmailAddress, constraints2);
panel1.add(lbEmailAddress);
// setup Email Address input field in display area
tfEmailAddress2.setFont(labelFont);
constraints2.gridx = 1;
constraints2.gridy = 5;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfEmailAddress2, constraints2);
panel1.add(tfEmailAddress2);
// setup Home Phone Number label in display area
lbHomeNumber.setFont(labelFont);
constraints2.gridx = 2;
constraints2.gridy = 5;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbHomeNumber, constraints2);
panel1.add(lbHomeNumber);
// setup Home Phone Number input field in display area
tfHomeNumber2.setFont(labelFont);
tfHomeNumber2.setEditable(false);
constraints2.gridx = 3;
constraints2.gridy = 5;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfHomeNumber2, constraints2);
panel1.add(tfHomeNumber2);
// setup Fax Number label in display area
lbFaxNumber.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 6;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbFaxNumber, constraints2);
panel1.add(lbFaxNumber);
// setup Fax Number input field in display area
tfFaxNumber2.setFont(labelFont);
tfFaxNumber2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 6;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFaxNumber2, constraints2);
panel1.add(tfFaxNumber2);
// setup UPDATE button in display area
btUpdate.setFont(buttonFont);
constraints2.gridx = 3;
constraints2.gridy = 7;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(btUpdate, constraints2);
panel2.add(btUpdate);
// setup DELETE button in display area
btDelete.setFont(buttonFont);
constraints2.gridx = 1;
constraints2.gridy = 7;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(btDelete, constraints2);
panel2.add(btDelete);
btUpdate.addActionListener(new ButtonHandler());
btDelete.addActionListener(new ButtonHandler());
// allow ALT U to press update button
btUpdate.setMnemonic('u');
// allow ALT D to press delete button
btDelete.setMnemonic('d');
// read table get the list of names in combo box search key
accessDBInit();
// define listener after adding items to CB to avoid triggering it
cbName2.addItemListener(new ComboBoxHandler());
//--------------------End showPane2()------------------------------------------->>>
//--------------------Start showPane3()----------------------------------------->>>
// Setup screen 2(insert pane) including labels, input fields, comboboxes,
// and buttons.
void showPane3(){
panel3 = new JPanel();
// set screen border
panel3.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab3, panel3);
// setup layout as GridBagLayout
constraints3.insets = new Insets(2,2,2,2);
panel3.setLayout (layout);
// setup First Name label in display area
JLabel lbFirstName = new JLabel("First Name:");
lbFirstName.setFont(labelFont);
constraints3.gridx = 0;
constraints3.gridy = 0;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbFirstName, constraints3);
panel3.add(lbFirstName);
// setup First Name input field in display area
tfFirstName3.setFont(labelFont);
constraints3.ipady = 8; // adjust heigth of input field
constraints3.gridx = 1;
constraints3.gridy = 0;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName3, constraints3);
panel3.add(tfFirstName3);
// setup Last Name label in display area
lbLastName = new JLabel("Last Name: ");
lbLastName.setFont(labelFont);
constraints3.gridx = 2;
constraints3.gridy = 0;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbLastName, constraints3);
panel3.add(lbLastName);
// setup Last Name input field in display area
tfLastName3.setFont(labelFont);
constraints3.gridx = 3;
constraints3.gridy = 0;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfLastName3, constraints3);
panel3.add(tfLastName3);
// setup Middle Name label in display area
lbAddress = new JLabel("Address: ");
lbAddress.setFont(labelFont);
constraints3.gridx = 0;
constraints3.gridy = 1;
constraints3.anchimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class andy extends JFrame {
// Define constant variables
final static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
final static String url = "jdbc:odbc:addBKTAFE";
final static String login = "LOG-IN";
final static String tab_login = "Log-in";
final static String tab1 = "INQUIRE Personnel Details";
final static String tab2 = "UPDATE/DELETE Personnel Details";
final static String tab3 = "INSERT Personnel Details";
final static String tab4 = "PRINT";
final static String insert = "SAVE RECORD";
final static String update = "UPDATE RECORD";
final static String delete = "DELETE RECORD";
final static String inquire = "INQUIRE RECORD";
final static String clear = " CLEAR ";
final static String relogin = "Log-in failed! Please relog-in!";
final static String norecfound = "No Record Found!";
final static String recinserted = "Record Inserted!";
final static String recupdated = "Record Updated!";
final static String recdeleted = "Record Deleted!";
final static String numericerror = "Age should be numeric!";
final static String information = "INFORMATION";
final static String error = "ERROR";
final static String genexception = "GENERAL EXCEPTION";
final static String sqlexception = "SQL EXCEPTION";
final static String confdelete = "CONFIRM DELETE";
final static String slash = "/";
final static String table1 = "persons";
final static String table2 = "Addresses";
// Define container, panels and tabbedpane
Container cntr = getContentPane();
JTabbedPane tpane = new JTabbedPane();
JPanel cbpanel1 , cbpanel2 , cbpanel3,
panel1 , panel2 , panel3, panel4;
// Define fonts to be used
Font labelFont = new Font("Arial",Font.PLAIN,12);
Font buttonFont = new Font("Arial",Font.BOLD,13);
// Define labels
JLabel lbUser = new JLabel("Enter User ID: " );
JLabel lbPassword = new JLabel("Enter Password: ");
JLabel lbSelectName = new JLabel("Search Name: " );
JLabel lbFirstName = new JLabel("First Name: " );
JLabel lbLastName = new JLabel("Last Name: " );
JLabel lbAddress = new JLabel("Address: " );
JLabel lbCity = new JLabel("City: " );
JLabel lbState = new JLabel("State: " );
JLabel lbPostcode = new JLabel("Postcode: " );
JLabel lbCountry = new JLabel("Country: " );
JLabel lbEmailAddress = new JLabel("Email Address: " );
JLabel lbHomeNumber = new JLabel("Home Phone No.: " );
JLabel lbFaxNumber = new JLabel("Fax No.: " );
// Define combo boxes in third screen (insert pane)
JComboBox cbName1 = new JComboBox();
JComboBox cbName2 = new JComboBox();
JComboBox cbName3 = new JComboBox();
JComboBox cbPersonId1 = new JComboBox();
JComboBox cbPersonId2 = new JComboBox();
JComboBox cbPersonId3 = new JComboBox();
// Inquiry fields on first screen (inquiry pane)
JTextField tfFirstName1 = new JTextField("",30);
JTextField tfLastName1 = new JTextField("",30);
JTextField tfAddress1 = new JTextField("",30);
JTextField tfCity1 = new JTextField("",15);
JTextField tfState1 = new JTextField("",15);
JTextField tfPostcode1 = new JTextField("",30);
JTextField tfCountry1 = new JTextField("",15 );
JTextField tfEmailAddress1 = new JTextField("",30);
JTextField tfHomeNumber1 = new JTextField("",15);
JTextField tfFaxNumber1 = new JTextField("",15 );
// Input fields on second screen (update/delete pane)
JTextField tfFirstName2 = new JTextField("",30);
JTextField tfLastName2 = new JTextField("",30);
JTextField tfAddress2 = new JTextField("",30);
JTextField tfCity2 = new JTextField("",15);
JTextField tfState2 = new JTextField("",15);
JTextField tfPostcode2 = new JTextField("",30);
JTextField tfCountry2 = new JTextField("",15);
JTextField tfEmailAddress2 = new JTextField("",30);
JTextField tfHomeNumber2 = new JTextField("",15);
JTextField tfFaxNumber2 = new JTextField("",15);
// Input fields on third screen (inset details pane)
JTextField tfFirstName3 = new JTextField("",30);
JTextField tfLastName3 = new JTextField("",30);
JTextField tfAddress3 = new JTextField("",30);
JTextField tfCity3 = new JTextField("",15);
JTextField tfState3 = new JTextField("",15);
JTextField tfPostcode3 = new JTextField("",30);
JTextField tfCountry3 = new JTextField("",15);
JTextField tfEmailAddress3 = new JTextField("",30);
JTextField tfHomeNumber3 = new JTextField("",15);
JTextField tfFaxNumber3 = new JTextField("",15);
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
public andy() {
super("Address Book Application");
showPane1();
showPane2();
showPane3();
showPane4();
cntr.add(tpane, "Center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,385);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - getWidth())/2,
(screenSize.height - getHeight())/2);
setVisible(true);
// Setup screen 1(inquiry pane) including labels, input fields, comboboxes.
// Table PERSONS is read to list inquiry parameters.
void showPane1() {
panel1 = new JPanel();
cbpanel1 = new JPanel();
// set screen border
panel1.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab1, panel1);
// setup layout as GridBagLayout
constraints.insets = new Insets(2,2,2,2);
panel1.setLayout (layout);
cbpanel1.setLayout (layout);
// setup Name combobox label
lbSelectName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
layout.setConstraints(lbSelectName, constraints);
panel1.add(lbSelectName);
// setup Name combobox as search key
cbName1.setFont(labelFont);
constraints.ipady = 10;
constraints.gridwidth = constraints.REMAINDER;
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(cbName1, constraints);
panel1.add(cbName1);
// setup search combobox (Name and corresponding key)
cbName1.addItem ("Choose one:");
cbPersonId1.addItem("0");
// setup First Name label in display area
lbFirstName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbFirstName, constraints);
panel1.add(lbFirstName);
// setup First Name input field in display area
tfFirstName1.setFont(labelFont);
tfFirstName1.setEditable(false);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName1, constraints);
panel1.add(tfFirstName1);
// setup Last Name label in display area
lbLastName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbLastName, constraints);
panel1.add(lbLastName);
// setup Last Name input field in display area
tfLastName1.setFont(labelFont);
tfLastName1.setEditable(false);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfLastName1, constraints);
panel1.add(tfLastName1);
// setup Address label in display area
lbAddress.setFont(labelFont);
constraints.gridwidth = 1;
constraints.anchor = GridBagConstraints.EAST;
layout.setConstraints(lbAddress, constraints);
panel1.add(lbAddress);
// setup Address input field in display area
tfAddress1.setFont(labelFont);
tfAddress1.setEditable(false);
constraints.gridwidth = constraints.REMAINDER;
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfAddress1, constraints);
panel1.add(tfAddress1);
// setup City label in display area
lbCity.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
constraints.fill = GridBagConstraints.NONE;
layout.setConstraints(lbCity, constraints);
panel1.add(lbCity);
// setup City input field in display area
tfCity1.setFont(labelFont);
tfCity1.setEditable(false);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCity1, constraints);
panel1.add(tfCity1);
// setup State label in display area
lbState.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbState, constraints);
panel1.add(lbState);
// setup State input field in display area
tfState1.setFont(labelFont);
tfState1.setEnabled(false);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfState1, constraints);
panel1.add(tfState1);
// setup Postcode label in display area
lbPostcode.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbPostcode, constraints);
panel1.add(lbPostcode);
// setup Address input field in display area
tfPostcode1.setFont(labelFont);
tfPostcode1.setEditable(false);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfPostcode1, constraints);
panel1.add(tfPostcode1);
// setup Country label in display area
lbCountry.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbCountry, constraints);
panel1.add(lbCountry);
// setup Country input field in display area
tfCountry1.setFont(labelFont);
tfCountry1.setEditable(false);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfCountry1, constraints);
panel1.add(tfCountry1);
// setup Email Address label in display area
lbEmailAddress.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbEmailAddress, constraints);
panel1.add(lbEmailAddress);
// setup Email Address input field in display area
tfEmailAddress1.setFont(labelFont);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfEmailAddress1, constraints);
panel1.add(tfEmailAddress1);
// setup Home Phone Number label in display area
lbHomeNumber.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbHomeNumber, constraints);
panel1.add(lbHomeNumber);
// setup Home Phone Number input field in display area
tfHomeNumber1.setFont(labelFont);
tfHomeNumber1.setEditable(false);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfHomeNumber1, constraints);
panel1.add(tfHomeNumber1);
// setup Fax Number label in display area
lbFaxNumber.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbFaxNumber, constraints);
panel1.add(lbFaxNumber);
// setup Fax Number input field in display area
tfFaxNumber1.setFont(labelFont);
tfFaxNumber1.setEditable(false);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFaxNumber1, constraints);
panel1.add(tfFaxNumber1);
// indicate inquiry pane
// pane_number = 1;
// read table get the list of names in CB search key
// accessDBInit();
// define listener after adding items to CB to avoid triggering it
// cbName1.addItemListener(new ComboBoxHandler());
//--------------------End showPane1()------------------------------------------->>>
//--------------------Start showPane2()----------------------------------------->>>
void showPane2() {
panel2 = new JPanel();
cbpanel2 = new JPanel();
// set screen border
panel2.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab2, panel2);
// setup layout as GridBagLayout
constraints.ipady = 0;
panel2.setLayout (layout);
cbpanel2.setLayout (layout);
// setup Name combobox label
lbSelectName = new JLabel("Search Name: ");
lbSelectName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
layout.setConstraints(lbSelectName, constraints);
panel2.add(lbSelectName);
// setup Name combobox as search key
cbName2.setFont(labelFont);
constraints.ipady = 10;
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(cbName2, constraints);
panel2.add(cbName2);
// setup search combobox (Name and corresponding key)
cbName2.addItem ("Choose one:");
cbPersonId2.addItem("0");
// setup UPDATE button in display area
JButton btUpdate = new JButton("Update");
btUpdate.setFont(buttonFont);
// btUpdate.addActionListener(new ButtonHandler());
btUpdate.setMnemonic(KeyEvent.VK_U);
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(btUpdate, constraints);
panel2.add(btUpdate);
// setup DELETE button in display area
JButton btDelete = new JButton("Delete");
btDelete.setFont(buttonFont);
// btDelete.addActionListener(new ButtonHandler());
btDelete.setMnemonic(KeyEvent.VK_D);
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(btDelete, constraints);
panel2.add(btDelete);
// setup First Name label in display area
lbFirstName = new JLabel("First Name: ");
lbFirstName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbFirstName, constraints);
panel2.add(lbFirstName);
// setup First Name input field in display area
tfFirstName2.setFont(labelFont);
tfFirstName2.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName2, constraints);
panel2.add(tfFirstName2);
// setup Last Name label in display area
lbLastName = new JLabel("Last Name: ");
lbLastName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbLastName, constraints);
panel2.add(lbLastName);
// setup Last Name input field in display area
tfLastName2.setFont(labelFont);
tfLastName2.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfLastName2, constraints);
panel2.add(tfLastName2);
// setup Address label in display area
lbAddress = new JLabel("Address: ");
lbAddress.setFont(labelFont);
constraints.gridwidth = 1;
constraints.anchor = GridBagConstraints.EAST;
layout.setConstraints(lbAddress, constraints);
panel2.add(lbAddress);
// setup Address input field in display area
tfAddress2.setFont(labelFont);
tfAddress2.setEditable(true);
constraints.gridwidth = constraints.REMAINDER;
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfAddress2, constraints);
panel2.add(tfAddress2);
// setup City label in display area
lbCity = new JLabel("City: ");
lbCity.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
constraints.fill = GridBagConstraints.NONE;
layout.setConstraints(lbCity, constraints);
panel2.add(lbCity);
// setup City input field in display area
tfCity2.setFont(labelFont);
tfCity2.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCity2, constraints);
panel2.add(tfCity2);
// setup State label in display area
lbState = new JLabel("State: ");
lbState.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbState, constraints);
panel2.add(lbState);
// setup State input field in display area
tfState2.setFont(labelFont);
tfState2.setEnabled(true);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfState2, constraints);
panel2.add(tfState2);
// setup Postcode label in display area
lbPostcode = new JLabel("Postcode: ");
lbPostcode.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbPostcode, constraints);
panel2.add(lbPostcode);
// setup Address input field in display area
tfPostcode2.setFont(labelFont);
tfPostcode2.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfPostcode2, constraints);
panel2.add(tfPostcode2);
// setup Country label in display area
lbCountry = new JLabel("Country: ");
lbCountry.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbCountry, constraints);
panel2.add(lbCountry);
// setup Country input field in display area
tfCountry2.setFont(labelFont);
tfCountry2.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfCountry2, constraints);
panel2.add(tfCountry2);
// setup Email Address label in display area
lbEmailAddress = new JLabel ("Email Address: ");
lbEmailAddress.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbEmailAddress, constraints);
panel2.add(lbEmailAddress);
// setup Email Address input field in display area
tfEmailAddress2.setFont(labelFont);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfEmailAddress2, constraints);
panel2.add(tfEmailAddress2);
// setup Home Phone Number label in display area
lbHomeNumber = new JLabel("Home Phone No.: ");
lbHomeNumber.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbHomeNumber, constraints);
panel2.add(lbHomeNumber);
// setup Home Phone Number input field in display area
tfHomeNumber2.setFont(labelFont);
tfHomeNumber2.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfHomeNumber2, constraints);
panel2.add(tfHomeNumber2);
// setup Fax Number label in display area
lbFaxNumber = new JLabel("Fax No.: ");
lbFaxNumber.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbFaxNumber, constraints);
panel2.add(lbFaxNumber);
// setup Fax Number input field in display area
tfFaxNumber2.setFont(labelFont);
tfFaxNumber2.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFaxNumber2, constraints);
panel2.add(tfFaxNumber2);
//-----------------------------End showpane2()---------------------------------->>
//--------------------Start showPane3()----------------------------------------->>>
void showPane3() {
panel3 = new JPanel();
// cbpanel3 = new JPanel();
// set screen border
panel3.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab3, panel3);
// setup layout as GridBagLayout
panel3.setLayout(layout);
constraints.ipady = 0;
panel3.setLayout (layout);
// cbpanel3.setLayout (layout);
// setup Name combobox label
lbSelectName = new JLabel("Search Name: ");
lbSelectName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
layout.setConstraints(lbSelectName, constraints);
panel3.add(lbSelectName);
// setup Name combobox as search key
cbName3 = new JComboBox();
cbName3.setFont(labelFont);
constraints.ipady = 10;
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(cbName3, constraints);
panel3.add(cbName3);
// setup search combobox (Name and corresponding key)
cbName3.addItem ("Choose one:");
cbPersonId3.addItem("0");
// setup INSERT button in display area
JButton btInsert = new JButton("Insert");
btInsert.setFont(buttonFont);
// btInsert.addActionListener(new ButtonListener());
btInsert.setMnemonic(KeyEvent.VK_S);
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(btInsert, constraints);
panel3.add(btInsert);
// setup CLEAR button in display area
JButton btClear = new JButton("Clear");
btClear.setFont(buttonFont);
// btClear.addActionListener(new ButtonListener());
btClear.setMnemonic(KeyEvent.VK_C);
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(btClear, constraints);
panel3.add(btClear);
// setup First Name label in display area
lbFirstName = new JLabel("First Name: ");
lbFirstName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbFirstName, constraints);
panel3.add(lbFirstName);
// setup First Name input field in display area
tfFirstName3.setFont(labelFont);
tfFirstName3.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName3, constraints);
panel3.add(tfFirstName3);
// setup Last Name label in display area
lbLastName = new JLabel("Last Name: ");
lbLastName.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbLastName, constraints);
panel3.add(lbLastName);
// setup Last Name input field in display area
tfLastName3.setFont(labelFont);
tfLastName3.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfLastName3, constraints);
panel3.add(tfLastName3);
// setup Address label in display area
lbAddress = new JLabel("Address: ");
lbAddress.setFont(labelFont);
constraints.gridwidth = 1;
constraints.anchor = GridBagConstraints.EAST;
layout.setConstraints(lbAddress, constraints);
panel3.add(lbAddress);
// setup Address input field in display area
tfAddress3.setFont(labelFont);
tfAddress3.setEditable(true);
constraints.gridwidth = constraints.REMAINDER;
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfAddress3, constraints);
panel3.add(tfAddress3);
// setup City label in display area
lbCity = new JLabel("City: ");
lbCity.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
constraints.fill = GridBagConstraints.NONE;
layout.setConstraints(lbCity, constraints);
panel3.add(lbCity);
// setup City input field in display area
tfCity3.setFont(labelFont);
tfCity3.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCity3, constraints);
panel3.add(tfCity3);
// setup State label in display area
lbState = new JLabel("State: ");
lbState.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbState, constraints);
panel3.add(lbState);
// setup State input field in display area
tfState3.setFont(labelFont);
tfState3.setEnabled(true);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfState3, constraints);
panel3.add(tfState3);
// setup Postcode label in display area
lbPostcode = new JLabel("Postcode: ");
lbPostcode.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbPostcode, constraints);
panel3.add(lbPostcode);
// setup Address input field in display area
tfPostcode3.setFont(labelFont);
tfPostcode3.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfPostcode3, constraints);
panel3.add(tfPostcode3);
// setup Country label in display area
lbCountry = new JLabel("Country: ");
lbCountry.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbCountry, constraints);
panel3.add(lbCountry);
// setup Country input field in display area
tfCountry3.setFont(labelFont);
tfCountry3.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfCountry3, constraints);
panel3.add(tfCountry3);
// setup Email Address label in display area
lbEmailAddress = new JLabel ("Email Address: ");
lbEmailAddress.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbEmailAddress, constraints);
panel3.add(lbEmailAddress);
// setup Email Address input field in display area
tfEmailAddress3.setFont(labelFont);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfEmailAddress3, constraints);
panel3.add(tfEmailAddress3);
// setup Home Phone Number label in display area
lbHomeNumber = new JLabel("Home Phone No.: ");
lbHomeNumber.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = constraints.RELATIVE;
layout.setConstraints(lbHomeNumber, constraints);
panel3.add(lbHomeNumber);
// setup Home Phone Number input field in display area
tfHomeNumber3.setFont(labelFont);
tfHomeNumber3.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(tfHomeNumber3, constraints);
panel3.add(tfHomeNumber3);
// setup Fax Number label in display area
lbFaxNumber = new JLabel("Fax No.: ");
lbFaxNumber.setFont(labelFont);
constraints.anchor = GridBagConstraints.EAST;
constraints.gridwidth = 1;
layout.setConstraints(lbFaxNumber, constraints);
panel3.add(lbFaxNumber);
// setup Fax Number input field in display area
tfFaxNumber3.setFont(labelFont);
tfFaxNumber3.setEditable(true);
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFaxNumber3, constraints);
panel3.add(tfFaxNumber3);
//-----------------------------End showpane3()---------------------------------->>
//-----------------------------Start showPane4()-------------------------------->>
void showPane4() {
// qtm = new QueryTableModel();
// JTable table = new JTable(qtm);
// String query = "SELECT FirstName, LastName, HomePhone, " +
// "FROM Addresses ORDER By LastName";
// qtm.setQuery(query);
JPanel demoPanel = new JPanel();
demoPanel.setPreferredSize(new Dimension(400,500));
demoPanel.setBackground(Color.pink);
demoPanel.add(new JLabel("Demo Panel in place of table",
SwingConstants.CENTER));
JScrollPane scrollPane = new JScrollPane(demoPanel);
panel4 = new JPanel();
// pane_number = 4;
panel4.setLayout(layout);
constraints.ipady = 0;
// labelFont = new Font("Arial", Font.PLAIN, 12); defined above
buttonFont = new Font("Arial", Font.PLAIN, 13);
// set screen border
panel4.setBorder(
BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), ""));
// add panel to tabbed pane
tpane.addTab(tab4, panel4);
JLabel lbPrint = new JLabel("Print");
lbPrint.setFont(labelFont);
constraints.anchor = constraints.EAST;
layout.setConstraints(lbPrint, constraints);
panel4.add(lbPrint);
JButton btPrint = new JButton("Print");
btPrint.setFont(buttonFont);
// btPrint.addActionListener(new ButtonListener());
btPrint.setMnemonic(KeyEvent.VK_P);
constraints.anchor = constraints.WEST;
constraints.gridwidth = constraints.REMAINDER;
layout.setConstraints(btPrint, constraints);
panel4.add(btPrint);
constraints.weighty = 1.0;
constraints.fill = constraints.BOTH;
constraints.anchor = constraints.CENTER;
layout.setConstraints(scrollPane, constraints);
panel4.add(scrollPane);
public static void main(String[] args) {
new andy();
} -
HELP, HELP, HELP, Problems using GridBagLayout with BorderLayout
I have a BorderLayout for my application. I created a JPanel that has the following items added to it:
1)Button
2)VisualComponent
3)ControllerComponent
The JPanel is then added to the WEST Pane of the BorderLayout. My problem is that I can not get my components in the JPanel to display correctly. The items should be added from top to buttom in my JPanel, but they overlap and display side by side, and some don't appear at all until my entire window application is Maximized. It seems as if all my components are trying to display in one row even though I explicitly add them to different rows as follows:
1)addUsingGBL(openFile,0,1,2,1);
2)addUsingGBL(visualComponent,1,1,1,3);
3)addUsingGBL(controlsComponent,4,1,2,1);
where addUsingGBL is as follows:
void addUsingGBL(Component component,int row, int column, int width, int height)
gbc. gridx = row;
gbc.gridy = column;
gbc.gridwidth = width;
gbc.gridheight = height;
gbLayout.setConstraints(component, gbc);
mediaPanel.add(component);
mediaPanel.doLayout();
If anyone has any suggestions, I would greatly appreciate it. Entire code is available if needed. Thanks!void addUsingGBL(Component component,int row, int column, int width, int height)
gbc. gridx = row;
gbc.gridy = column;
gbc.gridwidth = width;
gbc.gridheight = height;
gbLayout.setConstraints(component, gbc);
mediaPanel.add(component);
mediaPanel.doLayout();
}I see that the problem is that you are using the same gridbagconstraints reference to add all your components. In effect, all your components refer to the same gridbagconstraints , you are getting all the components on a single row. The solution to this is simple - Try this out and tell me whether that works...
void addUsingGBL(Component component,int row, int column, int width, int height)
//Add this line in the beginning...
gbc = new GridBagConstraints();
gbc. gridx = row;
gbc.gridy = column;
gbc.gridwidth = width;
gbc.gridheight = height;
gbLayout.setConstraints(component, gbc);
mediaPanel.add(component);
mediaPanel.doLayout(); -
Problem refreshing when using GridBagLayout for a JPanel
Well, i have a JPanel which use the GridBagLayout to layout the components. If i remove a component everything it's ok, i mean it disappear from the panel, but the appears when i want to add a component. It doesn't show me the new component. Any sugestion?
Here is the all code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
private JFrame f;
private final JPanel detalii = new JPanel();
private String[] sir1 = { "1820620160027", "Bradescu", "M", "Marius-Catalin", "Rovine bl. I29", "0745035150" };
private String[] sir2 = { "1820620160031", "Bercea", "C", "Florin Bogdan", "Brazda bl. I29", "0745035150" };
public Test(){
f = new JFrame( "Test" );
detalii.setLayout( new GridBagLayout() );
detalii.setPreferredSize( new Dimension( 450, 400) );
creazaDetaliiElev( sir2 );
f.addMouseListener( new MouseAdapter(){
public void mouseClicked( MouseEvent evt ){
if( evt.getButton() == 1){
creazaDetaliiElev( sir1 );
//detalii.invalidate();
f.repaint();
} else {
creazaDetaliiElev( sir2 );
//detalii.invalidate();
f.repaint();
f.getContentPane().add( detalii );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.pack();
f.setVisible( true );
protected void creazaDetaliiElev( String[] elev ){
if( detalii.getComponentCount() == 0 ){
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
JLabel lblID = new JLabel( "CNP:");
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets( 5, 5, 5, 5 );
c.gridx = 0;
c.gridy = 0;
detalii.add( lblID, c );
JLabel lblNume = new JLabel( "Nume:" );
c.gridx = 0;
c.gridy = 1;
detalii.add( lblNume, c );
JLabel lblInitiala = new JLabel( "Initiala:" );
c.gridx = 0;
c.gridy = 2;
detalii.add( lblInitiala, c );
JLabel lblPrenume = new JLabel( "Prenume:" );
c.gridx = 0;
c.gridy = 3;
detalii.add( lblPrenume, c );
JLabel lblAdresa = new JLabel( "Adresa" );
c.gridx = 0;
c.gridy = 4;
detalii.add( lblAdresa, c );
JLabel lblTelefon = new JLabel( "Telefon:" );
c.gridx = 0;
c.gridy = 5;
detalii.add( lblTelefon, c );
JTextField txtID = new JTextField( String.valueOf( elev[ 0 ] ) );
txtID.setBackground( Color.WHITE );
txtID.setHorizontalAlignment( JTextField.TRAILING );
txtID.setEditable( false );
c.gridx = 1;
c.gridy = 0;
detalii.add( txtID, c );
JTextField txtNume = new JTextField( elev[ 1 ] );
txtNume.setBackground( Color.WHITE );
txtNume.setHorizontalAlignment( JTextField.LEADING );
txtNume.setEditable( false );
c.gridx = 1;
c.gridy = 1;
detalii.add( txtNume, c );
JTextField txtInitiala = new JTextField( elev[ 2 ] );
txtInitiala.setBackground( Color.WHITE );
txtInitiala.setHorizontalAlignment( JTextField.CENTER );
txtInitiala.setEditable( false );
c.gridx = 1;
c.gridy = 2;
detalii.add( txtInitiala, c );
JTextField txtPrenume = new JTextField( elev[ 3 ] );
txtPrenume.setBackground( Color.WHITE );
txtPrenume.setHorizontalAlignment( JTextField.LEADING );
txtPrenume.setEditable( false );
c.gridx = 1;
c.gridy = 3;
detalii.add( txtPrenume, c );
JTextField txtAdresa = new JTextField( elev[ 4 ] );
txtAdresa.setBackground( Color.WHITE );
txtAdresa.setHorizontalAlignment( JTextField.LEADING );
txtAdresa.setEditable( false );
c.gridx = 1;
c.gridy = 4;
detalii.add( txtAdresa, c );
JTextField txtTelefon = new JTextField( elev[ 5 ] );
txtTelefon.setBackground( Color.WHITE );
txtTelefon.setHorizontalAlignment( JTextField.TRAILING );
txtTelefon.setEditable( false );
c.gridx = 1;
c.gridy = 5;
detalii.add( txtTelefon, c );
} else {
detalii.removeAll();
public static void main( String[] args ){
new Test();
, but nothing, as you see i use invalidate() too. -
See the problem with the GridbagLayout
See the out put i want small button but it is going wrong.
there are two program.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
class frame1 extends JFrame
JMenu m1;
JMenuItem m13,m14,m12;
JMenuBar mbr;
CardLayout cardLo;
JPanel panCon,jp;
StaticReservation stp;
KeyStroke k1;
public frame1()
super("Counselling System");
setExtendedState(JFrame.ICONIFIED);
Container c=getContentPane();
cardLo=new CardLayout();
panCon=new JPanel(cardLo);
stp=new StaticReservation();
panCon.add(stp,"StaticReservation");
mbr=new JMenuBar();
setJMenuBar(mbr);
m1=new JMenu("Office");
m1.setMnemonic(KeyEvent.VK_O);
mbr.add(m1);
m12=new JMenuItem("Static Reservation ");
m12.setMnemonic(KeyEvent.VK_S);
k1=KeyStroke.getKeyStroke(KeyEvent.VK_S,8);
m12.setAccelerator(k1);
m1.add(m12);
m14=new JMenuItem("Exit ");
m14.setMnemonic(KeyEvent.VK_X);
k1=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK);
m14.setAccelerator(k1);
m1.add(m14);
m14.addActionListener(new Myhandler(this));
m12.addActionListener(new Myhandler(this));
c.add(panCon);
public class Myhandler implements ActionListener
frame1 f1;
public Myhandler(frame1 t1)
this.f1=t1;
public void actionPerformed(ActionEvent ae)
if(ae.getSource()==m14)
System.exit(0);
if(ae.getSource()==m13)
cardLo.show(panCon,"CollegeDetailsPane");
if(ae.getSource()==m12)
cardLo.show(panCon,"StaticReservation");
public class menu2
public static void main(String ax[])throws Exception
frame1 f1=new frame1();
f1.setExtendedState(JFrame.MAXIMIZED_BOTH);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* double h=f1.getHeight();
double w=f1.getWidth();
System.out.println(" ---------- "+h);
System.out.println(" ---------- "+w); */
import javax.swing.*;
import java.awt.*;
public class StaticReservation extends JPanel
JLabel lbca,lbcb,lbcc,lbcd,lsc,lst,lwr;
JTextField tbca,tbcb,tbcc,tbcd,tsc,tst,twr;
GridBagLayout gbl;
GridBagConstraints gbc;
JPanel jpbc,jpsc,jpwr,jpbutton;
JButton b1,b2,b3;
StaticReservation()
setLayout(new GridLayout(4,1));
gbl=new GridBagLayout();
gbc=new GridBagConstraints();
jpbc=new JPanel(gbl);
jpbc.setBorder(BorderFactory.createTitledBorder("Backword Cast"));
jpbc.setBounds(200,200,500,500);
gbc.insets=new Insets(1,1,1,1);
gbc.weightx=0.01;
gbc.weighty=0.01;
//Lable's & Textfiled'sfor BC
gbc.gridx=0;
gbc.gridy=0;
lbca=new JLabel(" BC - A ");
gbl.setConstraints(lbca,gbc);
jpbc.add(lbca);
gbc.gridx=0;
gbc.gridy=1;
lbcb=new JLabel(" BC - B ");
gbl.setConstraints(lbcb,gbc);
jpbc.add(lbcb);
gbc.gridx=0;
gbc.gridy=2;
lbcc=new JLabel(" BC - C ");
gbl.setConstraints(lbcc,gbc);
jpbc.add(lbcc);
gbc.gridx=0;
gbc.gridy=3;
lbcd=new JLabel(" BC - D ");
gbl.setConstraints(lbcd,gbc);
jpbc.add(lbcd);
gbc.gridx=1;
gbc.gridy=0;
gbc.anchor=GridBagConstraints.WEST;
tbca=new JTextField(15);
gbl.setConstraints(tbca,gbc);
jpbc.add(tbca);
gbc.gridx=1;
gbc.gridy=1;
gbc.anchor=GridBagConstraints.WEST;
tbcb=new JTextField(15);
gbl.setConstraints(tbcb,gbc);
jpbc.add(tbcb);
gbc.gridx=1;
gbc.gridy=2;
gbc.anchor=GridBagConstraints.WEST;
tbcc=new JTextField(15);
gbl.setConstraints(tbcc,gbc);
jpbc.add(tbcc);
gbc.gridx=1;
gbc.gridy=3;
gbc.anchor=GridBagConstraints.WEST;
tbcd=new JTextField(15);
gbl.setConstraints(tbcd,gbc);
jpbc.add(tbcd);
//Lable's & Textfiled'sfor SC
jpsc=new JPanel(gbl);
jpsc.setBorder(BorderFactory.createTitledBorder(" SC/ST "));
gbc.insets=new Insets(1,1,1,1);
gbc.weightx=0.01;
gbc.weighty=0.01;
gbc.gridx=0;
gbc.gridy=0;
lsc=new JLabel(" SC ");
gbl.setConstraints(lsc,gbc);
jpsc.add(lsc);
gbc.gridx=0;
gbc.gridy=1;
lst=new JLabel(" ST ");
gbl.setConstraints(lst,gbc);
jpsc.add(lst);
gbc.gridx=1;
gbc.gridy=0;
gbc.anchor=GridBagConstraints.WEST;
tsc=new JTextField(15);
gbl.setConstraints(tsc,gbc);
jpsc.add(tsc);
gbc.gridx=1;
gbc.gridy=1;
gbc.anchor=GridBagConstraints.WEST;
tst=new JTextField(15);
gbl.setConstraints(tst,gbc);
jpsc.add(tst);
//Lable's & Textfiled'sfor WR Or Ladies REservations
jpwr=new JPanel(gbl);
jpwr.setBorder(BorderFactory.createTitledBorder(" Women "));
gbc.insets=new Insets(1,1,1,1);
gbc.weightx=0.01;
gbc.weighty=0.01;
gbc.gridx=0;
gbc.gridy=0;
lwr=new JLabel(" Women/Ladies ");
gbl.setConstraints(lwr,gbc);
jpwr.add(lwr);
gbc.gridx=1;
gbc.gridy=0;
gbc.anchor=GridBagConstraints.WEST;
twr=new JTextField(15);
gbl.setConstraints(twr,gbc);
jpwr.add(tsc);
jpbutton = new JPanel(new GridLayout(1,3));
b1=new JButton("Submit");
b1.setBounds(10,10,25,25);
b2=new JButton("Change");
b3=new JButton("Cancel");
jpbutton.add(b1);
jpbutton.add(b2);
jpbutton.add(b3);
// add all Panels to Main Panel
add(jpbc);
add(jpsc);
add(jpwr);
add(jpbutton);
}depends on how you want them to appear.
here's one way to change the current 'large' buttons
JPanel p = new JPanel(new BorderLayout());//<---
jpbutton = new JPanel(new GridLayout(1,3));
b1=new JButton("Submit");
b1.setBounds(10,10,25,25);
b2=new JButton("Change");
b3=new JButton("Cancel");
jpbutton.add(b1);
jpbutton.add(b2);
jpbutton.add(b3);
p.add(jpbutton,BorderLayout.SOUTH);//<---
// add all Panels to Main Panel
add(jpbc);
add(jpsc);
add(jpwr);
//add(jpbutton);//<---
add(p);//<---
Maybe you are looking for
-
Google Tasks in Safari 5-can't add date
Is anyone else experiencing this problem? Works fine in other browsers.
-
Hello! We have an 1Darray with values and we want to rotate/shift the values (3 positions). But everytime it shifts we want to read the array. For example : we have [1 2 3 4 5 6 7 8 9 10 ] and we want to shift it 3 positions like [10 9 8 1 2 3 4 5 6
-
Hi there. I have the iPhone NJ 6 (64Gb) I got (unlock). After returning home, I put the phone to the local operator's sim card. After the first attempt, however, was a problem on the phone screen. the screen was invisible. Scratches appeared. Improvi
-
Internet suddenly VERY VERY VERY SLOW!!
I have had my Airport Express for about 2 months now and it has worked fabulously. However, a few nights ago, I think it decided to die out on me or something. It connects to the internet and everything, web pages load, but it just takes about 5 minu
-
Firefox 9.0.1 loopt vast met adobe flash player 11.1.102.55
bij het openen van een filmpje ( zoals youtube ) loopt firefox na enkele seconden vast