Tuesday, January 15, 2013

JTable Example In Java.♥


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

class SimpleTableExample
        extends     JFrame
 {
   
    private    JPanel        topPanel;
    private    JTable        table;
    private    JScrollPane scrollPane;

        public SimpleTableExample()
    {
       
        setTitle( "Simple Table Application" );
        setSize( 300, 200 );
        setBackground( Color.gray );

       
        topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

       
        String columnNames[] = { "Column 1", "Column 2", "Column 3" };

                String dataValues[][] =
        {
            { "12", "234", "67" },
            { "-123", "43", "853" },
            { "93", "89.2", "109" },
            { "279", "9033", "3092" }
        };

       
        table = new JTable( dataValues, columnNames );

       
        scrollPane = new JScrollPane( table );
        topPanel.add( scrollPane, BorderLayout.CENTER );
    }

    public static void main( String args[] )
    {
       
        SimpleTableExample mainFrame    = new SimpleTableExample();
        mainFrame.setVisible( true );
    }
}

OUTPUT :


Sunday, January 13, 2013

JOptionPane Example In Java.♥


import javax.swing.JOptionPane;

public class JOptionPaneTest1 {
    public static void main(String[] args) {
        String ans;
        ans = JOptionPane.showInputDialog(null, "Speed in miles per hour?");
        double mph = Double.parseDouble(ans);
        double kph = 1.621 * mph;
        JOptionPane.showMessageDialog(null, "KPH = " + kph);

        System.exit(0);
    }
}

OUTPUT :

JColorChooser Example In Java.♥

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
                
public class JColorChooserTest extends JFrame
                               implements ActionListener {
  public static void main(String[] args) {
    new JColorChooserTest();
  }

  public JColorChooserTest() {
    super("Using JColorChooser");
     Container content = getContentPane();
    content.setBackground(Color.white);
    content.setLayout(new FlowLayout());
    JButton colorButton
      = new JButton("Choose Background Color");
    colorButton.addActionListener(this);
    content.add(colorButton);
    setSize(300, 100);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    // Args are parent component, title, initial color
    Color bgColor
      = JColorChooser.showDialog(this,
                                 "Choose Background Color",
                                 getBackground());
    if (bgColor != null)
      getContentPane().setBackground(bgColor);
  }
}
 
OUTPUT :