Working With GUI Components
Working With GUI Components
The following are code for working with some of the common javax.swing components.
Enter them into your code editor and compile them. Try to understand what the code is doing
in each case.
package frame.radiobutton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
public RadioButtonExample(){
rbtnMale = new JRadioButton("Male");
rbtnMale.setBounds(100,50,100,30);
rbtnFemale = new JRadioButton("Female");
rbtnFemale.setBounds(100,100,100,30);
//Create a Button Group for the Radio Buttons
ButtonGroup bg = new ButtonGroup();
//Add the Radio Buttons to the Button Group
bg.add(rbtnMale);
bg.add(rbtnFemale);
this.setSize(300,300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(rbtnMale.isSelected()){
JOptionPane.showMessageDialog(this,"Male selected.");
}
if(rbtnFemale.isSelected()){
JOptionPane.showMessageDialog(this,"Female selected.");
}
}
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public JInternalFrameExample() {
// create a new frame to
frame = new JFrame("frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Initialize the JInternalFrame();
internalFrame = new JInternalFrame("Internal Frame", true, true,
true, true);
// set the title of the frame
internalFrame.setTitle("InternalFrame");
// create a Button
button = new JButton("Click me");
// create a label to display text
label = new JLabel("This is a JInternal Frame ");
panel = new JPanel();
// add label and button to panel
panel.add(label);
panel.add(button);
// set visibility internal frame
internalFrame.setVisible(true);
// add panel to internal frame
internalFrame.add(panel);
// add internal frame to frame
frame.add(internalFrame);
// set the size of frame
frame.setSize(300, 300);
frame.setVisible(true);
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public LabelExample(){
textField = new JTextField();
textField.setBounds(100,50, 150,25);
label = new JLabel();
label.setBounds(50,100, 250,20);
urlLabel = new JLabel("Website URL:");
urlLabel.setBounds(25, 50, 80, 25);
button = new JButton("Find IP");
button.setBounds(50,150,95,30);
button.addActionListener(this);
add(button);
add(textField);
add(urlLabel);
add(label);
setSize(400,400);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
System.out.println(ex);
}
}
/* ============= Program with menu bar and short cut keys ================*/
makeMenu();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(640, 480);
setLocationRelativeTo(null);
KeyStroke keyStrokeToOpen =
KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK);
menuItemOpen.setAccelerator(keyStrokeToOpen);
menuItemOpen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Opening File...");
}
});
menuItemSave.setAction(saveAction);
menuFile.add(menuItemOpen);
menuFile.add(menuItemSave);
menuFile.add(menuItemExit);
menuBar.add(menuFile);
setJMenuBar(menuBar);
}
public JTableExample() {
String[] columns = {"Name", "Age", "Gender"};
String[][] data_rows = {
{"Chris", "45", "Male"},
{"Paula", "32", "Female"},
{"Michael", "17", "Male"},
{"Beverly", "32", "Female"}
};
if (dataRows % 2 == 0) {
row.setBackground(Color.WHITE);
}else {
row.setBackground(Color.LIGHT_GRAY);
}
if (isCellSelected(dataRows, columns)) {
row.setBackground(Color.GREEN);
}
return row;
}
};
table.setPreferredScrollableViewportSize(new Dimension(450,
63));
table.setFillsViewportHeight(true);
jsp = new JScrollPane(table);
add(jsp);
}
jf.setTitle("JTable Example");
jf.setSize(500, 500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(jte);
}
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public FlowLayoutExample() {
frame = new JFrame("FlowLayout Example");
usernameLabel = new JLabel("Username: ");
passwordLabel = new JLabel();
usernameTextField = new JTextField(20);
passwordField = new JPasswordField(20);
button = new JButton("Sign In");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
button.setSize(new Dimension(400, 30));
namePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
passwordPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
layoutComponents();
}
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public GridLayoutExample() {
frame = new JFrame("GridLayout Example");
usernameLabel = new JLabel("Username: ");
passwordLabel = new JLabel();
usernameTextField = new JTextField(20);
passwordField = new JPasswordField(20);
button = new JButton("Sign In");
button.setSize(new Dimension(400, 30));
namePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
passwordPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
layoutComponents();
}
buttonPanel.setSize(400, 30);
buttonPanel.add(button);
frame.add(buttonPanel);
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public GridBagLayoutExample() {
//Initialize OR Instantiate the Components
frame = new JFrame("GridBagLayout Example");
usernameLabel = new JLabel("Username: ");
passwordLabel = new JLabel();
usernameTextField = new JTextField(20);
passwordField = new JPasswordField(20);
button = new JButton("Sign In");
gbc = new GridBagConstraints();
passwordLabel.setText("Password: ");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.insets = new Insets(9, 10, 0, 0);
frame.add(passwordLabel, gbc);
import java.io.File;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
/** * The methods in this class allow the JTable component to get
* and display data about the files in a specified directory.
* It represents a table with six columns: filename, size, modification
date,
* plus three columns for flags: directory, readable, writable.
**/
public class FileTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
@Override
public int getColumnCount() {
return 6; // A constant for this model
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
File file = new File(dir, filenames[rowIndex]);
switch(columnIndex) {
case 0: return filenames[rowIndex];
case 1: return new Long(file.length());
case 2: return new Date(file.lastModified());
case 3: return file.isDirectory() ? Boolean.TRUE :
Boolean.FALSE;
case 4: return file.canRead() ? Boolean.TRUE :
Boolean.FALSE;
case 5: return file.canWrite() ? Boolean.TRUE :
Boolean.FALSE;
default: return null;
}
}
package table.tablemodel;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;