Archive

Archive for the ‘Java’ Category

Example Code #2 – JOptionPane JCombobox

December 21st, 2008 No comments

This code shows how to add ComboBox property to JOptionPane.showInputDialog. And the class is able to take an input from this combobox

import javax.swing.JOptionPane;

public class Options {

	public Options() {

		String smallList[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
				"Thrusday", "Friday", "Saturday" };

Read more…

VN:F [1.9.22_1171]
Rating: 7.1/10 (8 votes cast)
VN:F [1.9.22_1171]
Rating: +4 (from 4 votes)

Example Code #1 – JOptionPane and ArrayList

December 20th, 2008 No comments

Here this class takes some values using JOptionPane.showInputDialog and shows them to the user using foreach loop

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Confirm {

ArrayList fruits = new ArrayList();

Confirm(){
String value = "";
while (true){
value = JOptionPane.showInputDialog("Which fruit to Add?");

Read more…

VN:F [1.9.22_1171]
Rating: 6.7/10 (7 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Java ActionListener Tutorial

November 6th, 2008 No comments

This tutorial has two classes. ActionListener and Main class are seperated from each other.

  • ParamListener.java
  • BtnsListener.java

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* ParamListener.java
*/
public class ParamListener extends JFrame{
public ParamListener() {
JButton btn10 = new JButton("10");
JButton btn20 = new JButton("20");
JButton btn30 = new JButton("30");
JButton btn40 = new JButton("40");

btn10.addActionListener( new BtnsListener ("ten"));
btn20.addActionListener( new BtnsListener ("twenty"));
btn30.addActionListener( new BtnsListener ("thirty"));
btn40.addActionListener( new BtnsListener ("fourty"));

JPanel content = new JPanel();
content.add(btn10);
content.add(btn20);
content.add(btn30);
content.add(btn40);

setContentPane(content);
setTitle("A Tutorial");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}

public static void main(String[] args) {
new ParamListener();

}

}

Read more…

VN:F [1.9.22_1171]
Rating: 1.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Categories: Java Tags: , , ,

Java JCombobox Tutorial

November 6th, 2008 No comments

This tiny program lets you to choose background color of your pane.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class tut1 extends JFrame{

Color[] colors = {
Color.RED, Color.BLUE, Color.YELLOW, Color.BLACK, Color.ORANGE,
new Color(0,143,222)
};

String[] renk = {
"red", "blue", "yellow", "black", "orange", "turkuaz"
};

public JComboBox cmb = new JComboBox(renk);
public JPanel main = new JPanel();

public tut1() {

cmb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
int index = cmb.getSelectedIndex();
main.setBackground(colors[index]);
}
});
cmb.setBackground(Color.blue);

JLabel lbl = new JLabel();
main.add(cmb);
main.add(lbl);
setContentPane(main);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setTitle("my title");
pack();
}

Read more…

VN:F [1.9.22_1171]
Rating: 5.5/10 (2 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
Categories: General, Java Tags: , ,