Blank screen when invoking in applet viewer - java

This is my first time posting here.
I am using GUI in java for the very first time while working on some homework. I have started incrementally coding a Italian restaurant menu.
The below code is compiling fine with no errors. After compiling I run applet viewer Italian.html and the applet viewer screen only displays a blank window. I am a little confused as I have no errors to work with. Am I missing something simple.
Thanks for any help.
import javax.swing.*;
import java.awt.*;
public class Italian extends JApplet {
//Declare and array for a list of Pastas
private String [] pastas = {"Spaghetti", "Angel Hair Pasta", "Tortellini",
"Ziti"};
private String [] sauces = {"Maranaria", "Alfredo", "Spicy Marania"};
public Italian() {
//Create the base panel for the restaurant page
JPanel i1 = new JPanel();
i1.setLayout(new GridLayout(2, 1));
i1.add(new JComboBox(pastas));
i1.add(new JComboBox(sauces));
HTML
<html>
<head>
<title>Java Applet Demo</title>
</head>
<body>
<applet
code = "Italian.class"
width = 250
height = 250>
</applet>
</body>
</html>

You haven't added anything to the applet for the applet to show on the screen.
Either in your constructor or you init method you need to add the panel you've created to the content pane.
getContentPane().setLayout(new BorderLayout()); // Just to make sure
getContentPane().add(i1);

you shoud add something to applet to make them work.here is a link
http://math.hws.edu/eck/cs124/javanotes4/c6/index.html which will help you to know applets and graphics.
this eg. will help you which adds different buttons with proper lay out:
/*
This applet demonstrates various layout managers.
The applet itself uses a border layout with a JPanel in
the center, a JComboBox menu to the North, and a JLabel
to the south. The center panel uses a CardLayout.
Each card in the card layout contains a number of
buttons and uses a different layout manager. The
JComboBox menu is used to select among these cards.
The JLabel reports events as they occur.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutDemo extends JApplet
implements ActionListener, ItemListener {
CardLayout cards; // the layout manager for the center panel
JPanel cardPanel; // the center panel
JComboBox panelChoice; // menu for selecting which card to show
JLabel message; // a message shown at the bottom of the applet
public void init() {
panelChoice = new JComboBox(); // Set up the menu
panelChoice.setBackground(Color.white);
panelChoice.addItem("FlowLayout"); // Add in the names of the cards.
panelChoice.addItem("FlowLayout with Big Hgap");
panelChoice.addItem("Vertical BoxLayout");
panelChoice.addItem("Horizontal BoxLayout with Struts");
panelChoice.addItem("BorderLayout");
panelChoice.addItem("GridLayout(3,2)");
panelChoice.addItem("GridLayout(1,0)");
panelChoice.addItem("GridLayout(0,1)");
panelChoice.addItemListener(this);
message = new JLabel("Layout Demo", JLabel.CENTER); // Set up the mesage
message.setBackground(Color.white);
message.setOpaque(true); // so background color will show
message.setBorder(BorderFactory.createEmptyBorder(5,0,3,0));
message.setForeground(Color.red);
cardPanel = new JPanel(); // Set up the center panel
cardPanel.setBackground(Color.white);
cards = new CardLayout();
cardPanel.setLayout(cards);
setBackground(Color.blue);
getContentPane().setBackground(Color.blue);
getContentPane().setLayout(new BorderLayout(3,3));
getContentPane().add("Center",cardPanel);
getContentPane().add("North",panelChoice);
getContentPane().add("South",message);
JPanel panel; // Will represent various cards to be added to the center panel.
Box box; // For the cards that use a BoxLayout.
// Set up each "card" in the center panel to have its own layout
// manager and to contain a variety of buttons.
panel = new JPanel();
// use default FlowLayout for panel
panel.setBackground(Color.white);
cardPanel.add(panel, "FlowLayout");
addButton(panel,"First Button"); // ( addButton is a untility method, defined below )
addButton(panel,"Second Button");
addButton(panel,"Third Button");
addButton(panel,"Fourth Button");
addButton(panel,"Fifth Button");
addButton(panel,"Sixth Button");
addButton(panel,"Seventh Button");
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER,30000,5));
panel.setBackground(Color.white);
cardPanel.add(panel,"FlowLayout with Big Hgap");
addButton(panel," A Button");
addButton(panel,"Another Button");
addButton(panel,"A Third Button");
addButton(panel,"A Fourth Button");
addButton(panel,"A Final Button");
box = Box.createVerticalBox();
box.setBackground(Color.white);
cardPanel.add(box,"Vertical BoxLayout");
addButton(box,"Button One");
addButton(box,"Button Two");
addButton(box,"Button Three");
addButton(box,"Button Four");
addButton(box,"Button Five");
addButton(box,"Button Six");
box = Box.createHorizontalBox();
box.setBackground(Color.white);
cardPanel.add(box,"Horizontal BoxLayout with Struts");
addButton(box,"1st");
addButton(box,"2nd");
box.add( Box.createHorizontalStrut(10) );
addButton(box,"3rd");
addButton(box,"4th");
box.add( Box.createHorizontalStrut(10) );
addButton(box,"5th");
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
cardPanel.add(panel,"BorderLayout");
addButton(panel,"Center Button", BorderLayout.CENTER);
addButton(panel,"North Button", BorderLayout.NORTH);
addButton(panel,"South Button", BorderLayout.SOUTH);
addButton(panel,"East Button", BorderLayout.EAST);
addButton(panel,"West Button", BorderLayout.WEST);
panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(3,2)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
addButton(panel,"Button 5");
addButton(panel,"Button 6");
panel = new JPanel();
panel.setLayout(new GridLayout(1,0));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(1,0)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
panel = new JPanel();
panel.setLayout(new GridLayout(0,1));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(0,1)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
addButton(panel,"Button 5");
addButton(panel,"Button 6");
} // end init()
public Insets getInsets() {
// specify borders around the edges of the applet
return new Insets(3,3,3,3);
}
void addButton(Container p, String name) {
// Create a button with the given name and add it
// to the given panel. Set up the button to send
// events to the applet.
JButton b = new JButton(name);
p.add(b);
b.addActionListener(this);
}
void addButton(JPanel p, String name, Object option) {
// Same as above, but use the "option" object
// as an additional parameter in the add method.
JButton b = new JButton(name);
p.add(b, option);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
// A button was pressed. Report the name
// of the button by setting the message text.
String buttonName = evt.getActionCommand();
message.setText("Button \"" + buttonName + "\" was pressed.");
}
public void itemStateChanged(ItemEvent evt) {
// The user has selected an item from the JComboBox.
// Change the displayed card to match.
String panelName = (String)panelChoice.getSelectedItem();
cards.show(cardPanel, panelName);
message.setText("Panel \"" + panelName + "\" was selected.");
}
} // end class LayoutDemo

Related

Simple window in swing JAVA

Task: make the same window as in the screenshot below using JAVA swing:
What did I do:
Created a panel for the top block (BorderLayout), added two more panels to it (GridLayour), one for the left buttons(FR, FG, FB), the other for the right buttons (A, B,C), added it all to my JFrame window
Created a JScrollPane and added it to the JFrame too
Created a panel for the bottom block (BorderLayout), added two more panels to it (GridLayour), one for the left buttons(1,2,3,4...), the other for the JTextFiel text field, added it all to my JFrame window.
The result is below:
I tried using other layouts, but it still doesn't work. I attach the code.
import javax.swing.*;
import java.awt.*;
public class MyJFrame extends JFrame {
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
JPanel pan5 = new JPanel();
JPanel pan6 = new JPanel();
JButton jButton1 = new JButton("FR");
JButton jButton2 = new JButton("FG");
JButton jButton3 = new JButton("FB");
JButton jButton4 = new JButton("A");
JButton jButton5 = new JButton("B");
JButton jButton6 = new JButton("C");
public MyJFrame(){
super("Simple Swing App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(650,300);
setLayout(new GridLayout(3,2));
setResizable(true);
JScrollPane scrollPane = new JScrollPane();
jButton1.setBackground(Color.red);
jButton2.setBackground(Color.green);
jButton3.setBackground(Color.blue);
pan1.setLayout(new GridLayout(1,3,2,2));
pan2.setLayout(new GridLayout(1,3,2,2));
pan3.setLayout(new BorderLayout());
pan4.setLayout(new GridLayout(3,3,2,2));
pan5.setLayout(new GridLayout(3,1,1,1));
pan6.setLayout(new BorderLayout());
pan1.add(jButton1);
pan1.add(jButton2);
pan1.add(jButton3);
pan2.add(jButton4);
pan2.add(jButton5);
pan2.add(jButton6);
pan3.add(pan1, BorderLayout.WEST);
pan3.add(pan2, BorderLayout.EAST);
for (int i=1; i<10; i++) {
JButton jButton = new JButton(i+"");
pan4.add(jButton);
}
for (int i=1; i<4; i++){
JTextField jTextField = new JTextField(" Pole tekstowe " + i + " typu jTextField ");
jTextField.setBackground(Color.WHITE);
jTextField.setBorder(BorderFactory.createLineBorder(Color.CYAN));
pan5.add(jTextField);
}
pan6.add(pan4, BorderLayout.WEST);
pan6.add(pan5, BorderLayout.EAST);
add(pan3);
add(scrollPane);
add(pan6);
setSize(700,450);
setVisible(true);
}
}
If the question is "How to make this GUI?" I would use this approach:
3 x BorderLayout (red) - one for the entire GUI, one each for the PAGE_START and PAGE_END constraints of the main GUI panel.
In the panel used in the PAGE_START, 2 x FlowLayout (green), one in the LINE_START, the other in the LINE_END. (1)
In the panel in the PAGE_END, 2 x GridLayout (blue), the first a 3 x 3, the other a single column.
If the components at the top (the groups of buttons on the left & right) need to be the exact same size, also use grid layouts for them.

Button layout outside of a GridLayout

I'm currently doing a quite simple GUI and was wondering how I could get the button in question out from the GridLayout and put it in its own say BorderLayout, if that's a bit vague I'll attach images to show you what I mean:
With that picture I would like the button to not be with the grid layout and for it to fill all the way across at the bottom of the program as it would in a border layout. My code is as follows:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* Write a description of class HW4GUI here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class HW4GUI extends JFrame implements ActionListener
{
private JButton jbtAction;
private JTextField jtfFName;
private JTextField jtfLName;
private JTextField jtfLibNo;
private int nextLibNo;
private JPanel textPanel;
/**
* The constructor for the GUI, also initalises nextLibNo number
*/
public HW4GUI()
{
super("Adding a borrower");
makeFrame();
showFrame();
nextLibNo = 1001;
}
/**
*
*/
private void makeFrame()
{
setLayout(new GridLayout(4,0));
setResizable(false);
textPanel = new JPanel();
//textPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
textPanel.setLayout(new BorderLayout());
jtfFName = new JTextField(15);
JLabel fNLbl = new JLabel("First Name: ");
add(fNLbl);
add(jtfFName);
// add(textPanel);
fNLbl.setHorizontalAlignment(JLabel.RIGHT);
jtfFName.setEditable(true);
jtfLName = new JTextField(15);
JLabel lNLbl = new JLabel("Last Name: ");
add(lNLbl);
add(jtfLName);
//add(textPanel);
lNLbl.setHorizontalAlignment(JLabel.RIGHT);
jtfLName.setEditable(true);
jtfLibNo = new JTextField(15);
JLabel lNOLbl = new JLabel("Library Number: ");
add(lNOLbl);
add(jtfLibNo);
// add(textPanel);
lNOLbl.setHorizontalAlignment(JLabel.RIGHT);
jtfLibNo.setEditable(false);
jbtAction = new JButton("Add Borrower");
add(jbtAction, BorderLayout.SOUTH);
jbtAction.addActionListener(this);
}
/**
* displays the frame window where you can set the size of it and also other variables
*/
private void showFrame()
{
setSize(400,200);
setResizable(false);
setLocationRelativeTo( null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String fn = jtfFName.getText();
String ln = jtfLName.getText();
boolean valid = true;
if (e.getActionCommand().equals("Add Borrower"))
{
if (fn.equals("") && (ln.equals("")))
{
jtfLibNo.setText("No Names");
valid = false;
}
else if (fn.equals("") )
{
jtfLibNo.setText("No First Name");
valid = false;
}
else if (ln.equals(""))
{
jtfLibNo.setText("No Last Name");
valid = false;
}
else
if (valid == true)
{
String lib = Integer.toString(nextLibNo++);
jtfLibNo.setText(lib);
jbtAction.setText("Confirm");
}
}
if (e.getActionCommand().equals("Confirm"))
{
jtfLibNo.setText("");
jbtAction.setText("Add Borrower");
}
}
}
As you have said that you want the Button outside your GridLayout, you can do:
Declare a new Panel, like mainPanel or something like that.
JPanel mp = new JPanel();
Set its layout to 3x1 using GridLayout.
mp.setlayout(new GridLayout(3,1));
Add you labels and text-fields to that panel.
mp.add(fNLbl);// and the rest.
Add this panel to your frame.
add(mp, BorderLayout.CENTER);
Then add the Button at the end, using, BorderLayout.SOUTH.
add(jbtAction, BorderLayout.SOUTH);
But as far as my knowledge goes, then your button will occupy the width of the whole frame. So, instead, you can add the button to a panel, and then add that panel to it. Like:
add( new JPanel(){{ add(jbtAction);}}, BorderLayout.SOUTH); // this is double-brace initialization.
The following code works fine:
private void makeFrame()
{
JPanel mp = new JPanel();
mp.setLayout(new GridLayout(3,1));
setResizable(false);
textPanel = new JPanel();
//textPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
textPanel.setLayout(new BorderLayout());
jtfFName = new JTextField(15);
JLabel fNLbl = new JLabel("First Name: ");
mp.add(fNLbl);
mp.add(jtfFName);
// add(textPanel);
fNLbl.setHorizontalAlignment(JLabel.RIGHT);
jtfFName.setEditable(true);
jtfLName = new JTextField(15);
JLabel lNLbl = new JLabel("Last Name: ");
mp.add(lNLbl);
mp.add(jtfLName);
//add(textPanel);
lNLbl.setHorizontalAlignment(JLabel.RIGHT);
jtfLName.setEditable(true);
jtfLibNo = new JTextField(15);
JLabel lNOLbl = new JLabel("Library Number: ");
mp.add(lNOLbl);
mp.add(jtfLibNo);
// add(textPanel);
lNOLbl.setHorizontalAlignment(JLabel.RIGHT);
jtfLibNo.setEditable(false);
jbtAction = new JButton("Add Borrower");
add(mp, BorderLayout.CENTER);
add( new JPanel(){{ add(jbtAction);}}, BorderLayout.SOUTH);
jbtAction.addActionListener(this);
}
With that picture I would like the button to not be with the grid layout and for it to fill all the way across at the bottom of the program as it would in a border layout
Then use a BorderLayout. The default layout manager for a JFrame is a BorderLayout. So you would do somethinglike:
Create a panel using a GridLayout. Add the first 5 components to this panel. Then add the panel to the "CENTER" of the frame.
Create your button. Add the button the the "PAGE_END" of the frame.
The idea of layout managers is that you can nest panels with different layouts to achieve your final layout.
I also agree, the main panel with multiple buttons should probably be a GridBagLayout as it will size each column to the width of the widest component in the column instead of making every column width identical, which will make the panel look better. Read the section from the Swing tutorial on How to Use GridBagLayout for more information and working examples.

How do I arrange JPanels from top to bottom?

I'm currently self-studying Java. I'm learning Graphical User Interface(GUI) programming.
I want JPanels to be arranged from top to bottom in a JFrame.First of all,I have a JLabel added to the first JPanel. The second JPanel has 5 JRadioButtions. The third JPanel has a JButton and a JLabel.
When the JButton is pressed,the JLabel in the 3rd JPanel shows some text.
I used BoxLayout(BoxLayout.X_AXIS) for all the JPanels and added all 3 of them into a JFrame which has FlowLayout(). Here is a small piece of code:
class GUI extends JFrame implements ActionListener {
JPanel pan1,pan2,pan3; //3 JPanels
JRadioButton rad1,rad2,rad3,rad4,rad5; //5 RadioButtons
JButton button; //A JButton
JLabel label; //A JLabel
public GUI(String header)
{
super(header);
setLayout(new FlowLayout()); //set FlowLayout to JFrame
setBounds(350,325,600,125);
setResizable(false);
creator();
adder();
commander();
add(pan1);
add(pan2);
add(pan3); //Add all 3 panels to JFrame
}
private void adder()
{
pan1.setLayout(new BoxLayout(pan1,BoxLayout.X_AXIS));
pan2.setLayout(new BoxLayout(pan2,BoxLayout.X_AXIS));
pan3.setLayout(new BoxLayout(pan3,BoxLayout.X_AXIS)); //Layout for all 3 JPanels
pan1.add(new JLabel("Choose a Security Level"));
ButtonGroup group=new ButtonGroup();
group.add(rad1);
group.add(rad2);
group.add(rad3);
group.add(rad4);
group.add(rad5);
pan2.add(rad1);
pan2.add(rad2);
pan2.add(rad3);
pan2.add(rad4);
pan2.add(rad5);
pan3.add(button);
pan3.add(label);
}
private void creator()
{
pan1=new JPanel();
pan2=new JPanel();
pan3=new JPanel();
rad1=new JRadioButton("Security Level 1");
rad2=new JRadioButton("Security Level 2");
rad3=new JRadioButton("Security Level 3");
rad4=new JRadioButton("Security Level 4");
rad5=new JRadioButton("Security Level 5");
button=new JButton("Move On");
label=new JLabel();
}
private void commander()
{
rad1.addActionListener(this);
rad2.addActionListener(this);
rad3.addActionListener(this);
rad4.addActionListener(this);
rad5.addActionListener(this);
rad1.setActionCommand("radio1");
rad2.setActionCommand("radio2");
rad3.setActionCommand("radio3");
rad4.setActionCommand("radio4");
rad5.setActionCommand("radio5");
button.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
//When button is pressed,the text in label changes
if(evt.getActionCommand().equals("radio1"))
label.setText("Very Easy to bypass");
else if(evt.getActionCommand().equals("radio2"))
label.setText("Easy to bypass");
else if(evt.getActionCommand().equals("radio3"))
label.setText("Can bypass Sometimes");
else if(evt.getActionCommand().equals("radio4"))
label.setText("Hard to bypass");
else if(evt.getActionCommand().equals("radio5"))
label.setText("Very Hard to bypass");
else
{ //Code here
}
repaint();
//More code here....
}
}
This is the output I'm getting when I select the first radiobutton(Forget the green colour):
I want the "Very easy to Bypass" text to be placed above the "Move on" button and below all the JRadioButtons. I can increase the size of the JFrame so that there will be enough space. My questions are:
Which Layout should I use to achieve this?
Should this layout be applied just for the JFrame or all 3 JPanels?
you must use GridLayout
Its very easy to use it, just add it like this. Take care of the import commands. :)
JFrame frame = new JFrame(new GridLayout(3,5));
Use GridLayout
GridLayout layout = new GridLayout(0, 1, 0, 5);
setLayout(layout);
What I would do to add 5 JPanels:
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PanelAdd extends JFrame {
JPanel [] panels ;
public PanelAdd() {
GridLayout layout = new GridLayout(0, 1, 0, 5);
setLayout(layout);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setSize(400, 350);
}
public static void main(String [] args) {
PanelAdd add = new PanelAdd();
add.addPanels();
add.setVisible(true);
}
private void addPanels() {
panels = new JPanel[5];
for (int i = 0 ; i < panels.length ; i++) {
panels[i] = new JPanel();
panels[i].add(new JLabel("This Is Panel "+i));
add(panels[i]);
}
}
}
In this example, I made an array of 5 JPanels and add them through a loop.
I used GridLayout for the job.
This is just a hint for your answer
when you call add method from jframe,you can also give specified position to your panel in frame
like this:
JPanel pan1,pan2,pan3; //3 JPanels
JRadioButton rad1,rad2,rad3,rad4,rad5; //5 RadioButtons
JButton button; //A JButton
JLabel label; //A JLabel
public GUI(String header)
{
super(header);
setLayout(new FlowLayout()); //set FlowLayout to JFrame
setBounds(350,325,600,125);
setResizable(false);
creator();
adder();
commander();
add(pan1,BorderLayout.NORTH);
add(pan2,BorderLayout.CENTER);
add(pan3,,BorderLayout.SOUTH); //Add all panels to JFrame
}
good luck

JTabbedPane keeps adding new panels

I am trying to create a series of panels in JTabbedPane that displays a segment of info for the user. Right now, I am trying to get it so that the panel will refresh after the edit button is clicked. This button brings you to a different menu where you can adjust any of the fields and then go back to the tabbed pane. Unfortunately, while the object is changing by using the aforementioned menu, the pane does not reflect those changes correctly. What is does is make the first panel and all the tabs unclickable and seems to add a new panel to the bottom of the previous one with the new information. This occurs during the repaint() as previously it would not change at all.
JPanel leftPanel = new JPanel();
JPanel buttons = new JPanel();
JPanel title = new JPanel();
LayoutManager singleLine = new GridLayout(0,2);
leftPanel.setLayout(singleLine);
buttons.setLayout(new FlowLayout());
JLabel header = new JLabel("Basic Student Information");
title.add(header);
JLabel StreetLabel = new JLabel("First Name: "); //Creates all Labels
JLabel LNLabel = new JLabel("Last Name: ");
JLabel IDLabel = new JLabel("Student ID: ");
JLabel GPALabel =new JLabel("GPA: ");
JLabel StreetInfo = new JLabel(selectedStudent.getFirstName().toString());
JLabel LNInfo = new JLabel(selectedStudent.getLastName().toString());
JLabel IDInfo = new JLabel(selectedStudent.getID().toString());
JLabel GPAInfo = new JLabel(selectedStudent.getGpa().toString());
leftPanel.add(StreetLabel);
leftPanel.add(StreetInfo);
leftPanel.add(LNLabel);
leftPanel.add(LNInfo);
leftPanel.add(IDLabel);
leftPanel.add(IDInfo);
leftPanel.add(GPALabel);
leftPanel.add(GPAInfo);
StreetLabel.setHorizontalAlignment(JLabel.LEFT);
LNLabel.setHorizontalAlignment(JLabel.LEFT);
IDLabel.setHorizontalAlignment(JLabel.LEFT);
GPALabel.setHorizontalAlignment(JLabel.LEFT);
JButton edit = new JButton("Edit");
JButton close = new JButton("Close");
buttons.add(edit);
buttons.add(close);
basicInfoTab.add(title, BorderLayout.NORTH);
basicInfoTab.add(leftPanel, BorderLayout.CENTER);
basicInfoTab.add(buttons, BorderLayout.SOUTH);
edit.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent event){
readMenu.setVisible(false);
generalInfo();
}
});
close.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent event){
if(JOptionPane.showConfirmDialog(null, "Do you wish to exit this student's record?") == JOptionPane.OK_OPTION){
readMenu.setVisible(false);
HomeScreen();
}
}
});
Any help in getting this to refresh successfully would be very much appreciated.

Problem with the layouts in making a JAVA GUI

Here is a screenshot of my JFrame. This will be the main window to my application.
So the problem is that all the buttons are inline with each other, whereas I want them to be one under the other i.e. Add Contact under Show Contacts.
So how can I do that?
Here is my code for the JFrame.
public class CRUDFrame extends JFrame {
public CRUDFrame(){
super("AppCRUD");
setLayout(new FlowLayout());
JButton button1, button2, button3, button4;
button1 = new JButton(" Show Contacts ");
button2 = new JButton(" Add Contact ");
button3 = new JButton(" Update Number in a Contact ");
button4 = new JButton(" Delete a Contact ");
add(button1);
add(button2);
add(button3);
add(button4);
}
}
`
There have been some good answers centered around 'use a layout'. This example espouses the same advice, but also introduces the concept of nesting one layout within another. E.G. the JPanel containing the JButtons has a GridLayout. That panel is placed in the NORTH of a panel that is then added to the WEST of the main 'gui' panel.
The other components are added in order to show how the column of buttons might go together with other components in the main user interface.
Contact.java
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class Contact {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new EmptyBorder(3,3,3,3) );
JPanel controls = new JPanel( new BorderLayout(5,5) );
JPanel buttons = new JPanel(new GridLayout(0,1,4,4));
buttons.add( new JButton("Show") );
buttons.add( new JButton("Add") );
buttons.add( new JButton("Update Number") );
buttons.add( new JButton("Delete") );
buttons.setBorder( new TitledBorder("Contact") );
controls.add( buttons, BorderLayout.NORTH );
controls.add(new JScrollPane(new JTree()), BorderLayout.CENTER);
gui.add(controls, BorderLayout.WEST);
gui.add(new JTextArea("CardLayout for CRUD components.",10,30));
gui.add(new JLabel("Output label.."), BorderLayout.SOUTH);
JToolBar toolbar = new JToolBar();
toolbar.add(new JCheckBox("Auto save", true));
toolbar.add(new JCheckBox("Always On Top"));
gui.add(toolbar, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}
Screenshot
Use box layout and set dimension for each button. Check the below link.
http://download.oracle.com/javase/tutorial/uiswing/layout/box.html
Take a look at http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html which discusses the various layout managers that Swing offers. GridBagLayout is probably what you need.

Categories