Thursday, 2 March 2017

JTabbed Panel Example

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

public class JTabbedPaneExample extends JFrame {
  public static void main( String[] argv ) {
    JTabbedPaneExample myExample = new JTabbedPaneExample( "JTabbedPane Example" );
  }

  public JTabbedPaneExample( String title ) {
    super( title );
    setSize( 150, 150 );
    addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent we ) {
        dispose();
        System.exit( 0 );
      }
    } );
    init();
    pack();
    setVisible( true );
  }

  private void init() {
    JTabbedPane jtb = new JTabbedPane();
    for( int i = 1; i < 4; i++ ) {
      ImageIcon icon = new ImageIcon( i + ".gif" );
      JTextArea jta = new JTextArea( 20, 40 );
      jta.setText( "This is text within tab number " + i );
      JScrollPane jsp = new JScrollPane( jta );
      jtb.addTab( i + "-tab", icon, jsp );
    }
    getContentPane().add( jtb );
    jtb.setBorder( BorderFactory.createEtchedBorder() );
  }
}






Inner windows

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

public class InnerWindows extends JFrame {
  public static void main( String[] argv ) {
    InnerWindows myExample = new InnerWindows( "Inner Windows Example" );
  }

  public InnerWindows( String title ) {
    super( title );
    setSize( 550, 400 );
    addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent we ) {
        dispose();
        System.exit( 0 );
      }
    } );
    init();
    setVisible( true );
  }

  private void init() {
    JLayeredPane layers = new JDesktopPane();
    setLayeredPane( layers );
    for( int i = 0; i < 8; i++ ) {
      JTextArea jta = new JTextArea( 20, 40 );
      jta.setText( "This is text within tab number " + i );
      JScrollPane jsp = new JScrollPane( jta );
      jsp.setPreferredSize( new Dimension( 120, 140 ) );
      //JInternalFrame(title, resizable, closable,maximizable, iconifiable)
      JInternalFrame jif = new JInternalFrame( i + " frame", true, true, true, true );
      jif.setLocation( ( i % 4 ) * 140, ( i / 4 ) * 180 );
      jif.getContentPane().add( jsp );
      jif.pack();
      layers.add( jif );
      jif.show();
    }
  }
}


Display Window X, Y Position

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

public class ab extends JFrame implements MouseListener
    {
     JPanel jpanel;
     JTextField jtf;
     int x,y;
     String str;
     public ab()
         {
         jtf = new JTextField();
         getContentPane().add(jtf,BorderLayout.SOUTH);
         getContentPane().addMouseListener(this);
     }
     public static void main(String[] args)
         {
         JFrame.setDefaultLookAndFeelDecorated(true);
         ab frame = new ab();
         frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
         frame.setSize(300,300);
         frame.setVisible(true);
     }
     public void paint(Graphics g)
         {
         super.paintComponents(g);
         str = "X:"+x+" "+"Y:"+y;
         jtf.setText(str);
     }
     public void mousePressed(MouseEvent me)
     {}
     public void mouseReleased(MouseEvent me)
     {}
     public void mouseEntered(MouseEvent me)
     {}
     public void mouseExited(MouseEvent me)
     {}
     public void mouseClicked(MouseEvent me)
         {
         x = me.getX();
         y = me.getY();
         repaint();
     }
}


Drag n Drop JLabel from JToolBar

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

public class DragDrop1 extends JFrame
    {
     JPanel tpan = new JPanel();
     JToolBar fpan = new JToolBar();
   
     Cursor dc = new Cursor(Cursor.DEFAULT_CURSOR);
     Cursor yd = DragSource.DefaultMoveDrop;
     Point mp;
   
    public DragDrop1()
        {
         super(" From .......> To");
         addWindowListener(new WindowAdapter()
             {
             public void windowClosing(WindowEvent ev)
                 {
                 dispose();
                 System.exit(0);
             }
         });
         setBounds(10,10,650,450);
         fpan.setPreferredSize(new Dimension(1,26));
         fpan.setBorder(BorderFactory.createRaisedBevelBorder());
         tpan.setLayout(null);
         getContentPane().add("North",fpan);
         getContentPane().add("Center",tpan);
         add_comp(new JLabel(" D1 "),Color.red);
         add_comp(new JLabel(" D2 "),Color.green);
         setVisible(true);
    }
   
    private void add_comp(JLabel l, Color c)
        {
         fpan.addSeparator();
         l.setOpaque(true);
         l.setHorizontalAlignment(SwingConstants.CENTER);
         l.setForeground(Color.black);
         l.setBackground(c);
         fpan.add(l);
         mak_lis(l);
    }
    private void mak_lis(final JLabel l)
        {
         l.addMouseListener(new MouseAdapter()
             {
             public void mousePressed(MouseEvent m)
                 {
                 setCursor(yd);
                 l.setBorder(new MatteBorder(1,1,1,1,Color.black));
             }
             public void mouseReleased(MouseEvent m)
                 {
                 l.setBorder(null);
                 setCursor(dc);
                 int x = m.getX()+l.getX();
                 int y = m.getY()+l.getY()-tpan.getY();
                 if (y > 0 && x > 0 && y < tpan.getHeight() && x < tpan.getWidth())
                     {
                     tpan.add(new_lab(l,x,y));
                     tpan.repaint();
                 }
             }
         });
    }
    private Component new_lab(JLabel co, int x, int y) //function to draw label in new posn
        {
         JLabel label = new JLabel(co.getText());
         label.setOpaque(true);
         label.setHorizontalAlignment(SwingConstants.CENTER);
         label.setForeground(co.getForeground());
         label.setBackground(co.getBackground());
         label.setBounds(x,y,co.getWidth(),co.getHeight());
         return(label);
    }
    public static void main (String[] args)
        {
         new DragDrop1();
    }
   
}