Java – Convert Array to List

[code lang=”java”]import java.util.*;
import java.io.*;

public class ArrayToCollection {

public static void main(String args[]) throws IOException {

BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("How many elements you want to add to the array: ");
int n = Integer.parseInt(in.readLine());
String[] name = new String[n];
for(int i = 0; i < n; i++) {
name[i] = in.readLine();
}
List list = Arrays.asList(name);
System.out.println();
for(String li: list) {
String str = li;
System.out.print(str + " ");
}

}

}[/code]
Output:

How many elements you want to add to the array:
red white green

red white green

Java Swing – Create Menu Bar

Java Swing Menubar

[code lang=”java”]import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

public class MenuExp extends JFrame {

public MenuExp() {

setTitle("Menu Example");
setSize(150, 150);

// Creates a menubar for a JFrame
JMenuBar menuBar = new JMenuBar();

// Add the menubar to the frame
setJMenuBar(menuBar);

// Define and add two drop down menu to the menubar
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
menuBar.add(fileMenu);
menuBar.add(editMenu);

// Create and add simple menu item to one of the drop down menu
JMenuItem newAction = new JMenuItem("New");
JMenuItem openAction = new JMenuItem("Open");
JMenuItem exitAction = new JMenuItem("Exit");
JMenuItem cutAction = new JMenuItem("Cut");
JMenuItem copyAction = new JMenuItem("Copy");
JMenuItem pasteAction = new JMenuItem("Paste");

// Create and add CheckButton as a menu item to one of the drop down menu
JCheckBoxMenuItem checkAction = new JCheckBoxMenuItem("Check Action");

// Create and add Radio Buttons as simple menu items to one of the drop down menu
JRadioButtonMenuItem radioAction1 = new JRadioButtonMenuItem("Radio Button1");
JRadioButtonMenuItem radioAction2 = new JRadioButtonMenuItem("Radio Button2");

// Create a ButtonGroup and add both radio Button to it. Only one radio
// button in a ButtonGroup can be selected at a time.
ButtonGroup bg = new ButtonGroup();
bg.add(radioAction1);
bg.add(radioAction2);

fileMenu.add(newAction);
fileMenu.add(openAction);
fileMenu.add(checkAction);
fileMenu.addSeparator();
fileMenu.add(exitAction);

editMenu.add(cutAction);
editMenu.add(copyAction);
editMenu.add(pasteAction);
editMenu.addSeparator();
editMenu.add(radioAction1);
editMenu.add(radioAction2);

// Add a listener to the New menu item. actionPerformed() method will
// invoked, if user triggred this menu item
newAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("You have clicked on the new action");
}
});
}

public static void main(String[] args) {
MenuExp me = new MenuExp();
me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
me.setVisible(true);
}

}[/code]

Java – Change File Date / Time using Commons FileUtils

[code lang=”java”]import java.io.IOException;
import org.apache.commons.io.FileUtils;

import java.io.File;

public class Main {

public static void main(String[] args) {
try {
File file = new File("pic.jpg");
long lastModified1 = file.lastModified();
FileUtils.touch(file);
long lastModified2 = file.lastModified();
System.out.println("File date / time was updated: " + (lastModified2 > lastModified1));
} catch (IOException ex) {
ex.printStackTrace();
}
}

}[/code]

Output:

File date / time was updated: true