Archive for the ‘Swing’ Category

JAVA Swing: JLabel, JOptionPane, JTextField, JSlider, JButton, JToolBar, JMenuItem, JMenu, JScrollPane, JProgressBar, JPanel, JTabbedPane

Saturday, September 28th, 2013

Example 1

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class FeedInfo extends JFrame {
private JLabel nameLabel = new JLabel(“Name: “,
SwingConstants.RIGHT);
private JTextField name;
private JLabel urlLabel = new JLabel(“URL: “,
SwingConstants.RIGHT);
private JTextField url;
private JLabel typeLabel = new JLabel(“Type: “,
SwingConstants.RIGHT);
private JTextField type;

public FeedInfo() {
super(“Feed Information”);
setSize(400, 145);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
// Site name
String response1 = JOptionPane.showInputDialog(null,
“Enter the site name:”);
name = new JTextField(response1, 20);

// Site address
String response2 = JOptionPane.showInputDialog(null,
“Enter the site address:”);
url = new JTextField(response2, 20);

// Site type
String[] choices = { “Personal”, “Commercial”, “Unknown” };
int response3 = JOptionPane.showOptionDialog(null,
“What type of site is it?”,
“Site Type”,
0,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
type = new JTextField(choices[response3], 20);

setLayout(new GridLayout(3, 2));
add(nameLabel);
add(name);
add(urlLabel);
add(url);
add(typeLabel);
add(type);
setLookAndFeel();
setVisible(true);
}

private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println(“Couldn’t use the system ”
+ “look and feel: ” + e);
}
}

public static void main(String[] arguments) {
FeedInfo frame = new FeedInfo();
}
}

 

Example 2

import java.awt.event.*;
import javax.swing.*;

public class Slider extends JFrame {
public Slider() {
super(“Slider”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
JSlider pickNum = new JSlider(JSlider.HORIZONTAL, 0, 30, 5);
pickNum.setMajorTickSpacing(10);
pickNum.setMinorTickSpacing(1);
pickNum.setPaintTicks(true);
pickNum.setPaintLabels(true);
add(pickNum);
pack();
setVisible(true);
}

private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println(“Couldn’t use the system ” + “look and feel: ” + e);
}
}

public static void main (String[] args) {
Slider frame = new Slider();
}
}

 

Example 3

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FeedBar extends JFrame {

public FeedBar() {
super(“FeedBar”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
// create icons
ImageIcon loadIcon = new ImageIcon(“load.gif”);
ImageIcon saveIcon = new ImageIcon(“save.gif”);
ImageIcon subscribeIcon = new ImageIcon(“subscribe.gif”);
ImageIcon unsubscribeIcon = new ImageIcon(“unsubscribe.gif”);
// create buttons
JButton load = new JButton(“Load”, loadIcon);
JButton save = new JButton(“Save”, saveIcon);
JButton subscribe = new JButton(“Subscribe”, subscribeIcon);
JButton unsubscribe = new JButton(“Unsubscribe”, unsubscribeIcon);
// add buttons to toolbar
JToolBar bar = new JToolBar();
bar.add(load);
bar.add(save);
bar.add(subscribe);
bar.add(unsubscribe);
// create menu
JMenuItem j1 = new JMenuItem(“Load”);
JMenuItem j2 = new JMenuItem(“Save”);
JMenuItem j3 = new JMenuItem(“Subscribe”);
JMenuItem j4 = new JMenuItem(“Unsubscribe”);
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu(“Feeds”);
menu.add(j1);
menu.add(j2);
menu.addSeparator();
menu.add(j3);
menu.add(j4);
menubar.add(menu);
// prepare user interface
JTextArea edit = new JTextArea(8, 40);
JScrollPane scroll = new JScrollPane(edit);
BorderLayout bord = new BorderLayout();
setLayout(bord);
add(“North”, bar);
add(“Center”, scroll);
setJMenuBar(menubar);
pack();
setVisible(true);
}

public void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println(“Couldn’t use the system look and feel: ” + e);
}
}

public static void main(String[] arguments) {
FeedBar frame = new FeedBar();
}
}

 

Example 4

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ProgressMonitor extends JFrame {
JProgressBar current;
JTextArea find;
Thread runner;
int num = 0;

public ProgressMonitor() {
super(“Progress Monitor”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
setSize(205, 68);
setLayout(new FlowLayout());
current = new JProgressBar(0, 2000);
current.setValue(0);
current.setStringPainted(true);
add(current);
}

public void iterate() {
while (num < 2000) {
current.setValue(num);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
num += 95;
}
}

private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println(“Couldn’t use the system ”
+ “look and feel: ” + e);
}
}

public static void main(String[] arguments) {
ProgressMonitor frame = new ProgressMonitor();
frame.setVisible(true);
frame.iterate();
}
}

 

Example 5

import java.awt.*;
import javax.swing.*;

public class TabPanels extends JFrame {
public TabPanels() {
super(“Tabbed Panes”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
setSize(480, 218);
JPanel mainSettings = new JPanel();
JPanel advancedSettings = new JPanel();
JPanel privacySettings = new JPanel();
JPanel emailSettings = new JPanel();
JPanel securitySettings = new JPanel();
JTabbedPane tabs = new JTabbedPane();
tabs.addTab(“Main”, mainSettings);
tabs.addTab(“Advanced”, advancedSettings);
tabs.addTab(“Privacy”, privacySettings);
tabs.addTab(“E-mail”, emailSettings);
tabs.addTab(“Security”, securitySettings);
add(tabs);
setVisible(true);
}

private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println(“Couldn’t use the system ”
+ “look and feel: ” + e);
}
}

public static void main(String[] arguments) {
TabPanels frame = new TabPanels();
}
}

resource: Sams Teach Yourself JAVA in 21 Days, 6th Editions, Covers Java 7 and Android, Rogers Cadenhead