Java - Drop Down Menu

How To Make A Drop And Down Menu In Java Eclipse

Java Drop-Down Menu



In this Java Tutorial we will see How To Make A Drop N Down Menu Using Panel Buttons And Timer In Java Using Eclipse .




Project Source Code:

package p;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

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

public class drop_down_menu extends JFrame {

private JPanel contentPane;
Timer tm1,tm2,tm3;
// the panels default height = 60
    Integer pl1 = 60,pl2 = 60,pl3 = 60;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
drop_down_menu frame = new drop_down_menu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public drop_down_menu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 550, 435);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(376, 11, 148, 60);
contentPane.add(panel);
panel.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBackground(Color.ORANGE);
btnNewButton.setBounds(0, 0, 148, 60);
panel.add(btnNewButton);
JButton button = new JButton("New button");
button.setBackground(Color.ORANGE);
button.setBounds(0, 61, 148, 60);
panel.add(button);
JButton button_1 = new JButton("New button");
button_1.setBackground(Color.ORANGE);
button_1.setBounds(0, 122, 148, 60);
panel.add(button_1);
JButton button_2 = new JButton("New button");
button_2.setBackground(Color.ORANGE);
button_2.setBounds(0, 183, 148, 60);
panel.add(button_2);
JButton button_3 = new JButton("New button");
button_3.setBackground(Color.ORANGE);
button_3.setBounds(0, 243, 148, 60);
panel.add(button_3);
JPanel panel_1 = new JPanel();
panel_1.setLayout(null);
panel_1.setBounds(211, 11, 148, 60);
contentPane.add(panel_1);
JButton button_4 = new JButton("New button");
button_4.setBackground(Color.ORANGE);
button_4.setBounds(0, 0, 148, 60);
panel_1.add(button_4);
JButton button_5 = new JButton("New button");
button_5.setBackground(Color.ORANGE);
button_5.setBounds(0, 61, 148, 60);
panel_1.add(button_5);
JButton button_6 = new JButton("New button");
button_6.setBackground(Color.ORANGE);
button_6.setBounds(0, 122, 148, 60);
panel_1.add(button_6);
JButton button_7 = new JButton("New button");
button_7.setBackground(Color.ORANGE);
button_7.setBounds(0, 183, 148, 60);
panel_1.add(button_7);
JButton button_8 = new JButton("New button");
button_8.setBackground(Color.ORANGE);
button_8.setBounds(0, 243, 148, 60);
panel_1.add(button_8);
JPanel panel_2 = new JPanel();
panel_2.setLayout(null);
panel_2.setBounds(42, 11, 148, 60);
contentPane.add(panel_2);
JButton button_9 = new JButton("New button");
button_9.setBackground(Color.ORANGE);
button_9.setBounds(0, 0, 148, 60);
panel_2.add(button_9);
JButton button_10 = new JButton("New button");
button_10.setBackground(Color.ORANGE);
button_10.setBounds(0, 61, 148, 60);
panel_2.add(button_10);
JButton button_11 = new JButton("New button");
button_11.setBackground(Color.ORANGE);
button_11.setBounds(0, 122, 148, 60);
panel_2.add(button_11);
JButton button_12 = new JButton("New button");
button_12.setBackground(Color.ORANGE);
button_12.setBounds(0, 183, 148, 60);
panel_2.add(button_12);
JButton button_13 = new JButton("New button");
button_13.setBackground(Color.ORANGE);
button_13.setBounds(0, 243, 148, 60);
panel_2.add(button_13);
tm1 = new Timer(20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 300 the maximum height
if(pl1 > 300)
{
tm1.stop();
}
else{
panel.setSize(panel.getWidth(),pl1);
pl1 += 20;
}
}
});
      tm2 = new Timer(20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(pl2 > 300)
{
tm2.stop();
}
else{
panel_1.setSize(panel_1.getWidth(),pl2);
pl2 += 20;
}
}
});
      
      tm3 = new Timer(20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(pl3 > 300)
{
tm3.stop();
}
else{
panel_2.setSize(panel_2.getWidth(),pl3);
pl3 += 20;
}
}
});
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent arg0) {
// set the panels to the dafault height
panel_1.setSize(panel_1.getWidth(), pl2);
panel_2.setSize(panel_2.getWidth(), pl3);
tm1.start();
}
@Override
public void mouseExited(MouseEvent e) {
tm1.stop();
pl1 = 60;
}
});
button_4.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent arg0) {
panel.setSize(panel.getWidth(), pl1);
panel_2.setSize(panel_2.getWidth(), pl3);
tm2.start();
}
@Override
public void mouseExited(MouseEvent e) {
tm2.stop();
pl2 = 60;
}
});
button_9.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
panel_1.setSize(panel_1.getWidth(), pl2);
panel.setSize(panel.getWidth(), pl1);
tm3.start();
}
@Override
public void mouseExited(MouseEvent e) {
tm3.stop();
pl3 = 60;
}
});
}
}

// OUTPUT :


java dropdown menu in eclipse ide

Javascript Move Row Up And Down

How To Move Selected HTML Table Row Up And Down In Javascript  

Move Table Row Up & Down In Javascript


In This Javascript Tutorial we will See How To Change Selected HTML Table Row Position Up Or Down On Button Click using JS And Netbeans Editor .


Project Source Code:


<!DOCTYPE html>
<html>
    <head>
        <title>HTML Table Row Up And Down</title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            
            tr{cursor: pointer}
            
            .selected{background-color: red; color: #fff;font-weight: bold}
            
            button{margin-top:10px;background-color: #eee;border: 2px solid #00F;
                  color: #17bb1c;font-weight: bold;font-size: 25px;cursor: pointer}
            
        </style>
    </head>
    <body>
        <table id="table" border="1">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Age</th>
            </tr>
            <tr>
                <td>A1</td>
                <td>B1</td>
                <td>C1</td>
            </tr>
            <tr>
                <td>A2</td>
                <td>B2</td>
                <td>C2</td>
            </tr>
            <tr>
                <td>A3</td>
                <td>B3</td>
                <td>C3</td>
            </tr>
            <tr>
                <td>A4</td>
                <td>B4</td>
                <td>C4</td>
            </tr>
            <tr>
                <td>A5</td>
                <td>B5</td>
                <td>C5</td>
            </tr>
        </table>
        <button onclick="upNdown('up');">&ShortUpArrow;</button>
        <button onclick="upNdown('down');">&ShortDownArrow;</button>
        
        <script>
            
            var index;  // variable to set the selected row index
            function getSelectedRow()
            {
                var table = document.getElementById("table");
                for(var i = 1; i < table.rows.length; i++)
                {
                    table.rows[i].onclick = function()
                    {
                        // clear the selected from the previous selected row
                        // the first time index is undefined
                        if(typeof index !== "undefined"){
                            table.rows[index].classList.toggle("selected");
                        }
                       
                        index = this.rowIndex;
                        this.classList.toggle("selected");

                    };
                }
                    
            }
  

            getSelectedRow();
            
            
            function upNdown(direction)
            {
                var rows = document.getElementById("table").rows,
                    parent = rows[index].parentNode;
                 if(direction === "up")
                 {
                     if(index > 1){
                        parent.insertBefore(rows[index],rows[index - 1]);
                        // when the row go up the index will be equal to index - 1
                        index--;
                    }
                 }
                 
                 if(direction === "down")
                 {
                     if(index < rows.length -1){
                        parent.insertBefore(rows[index + 1],rows[index]);
                        // when the row go down the index will be equal to index + 1
                        index++;
                    }
                 }
            }
            
        </script>
    </body>
</html>



OUTPUT:


moving selected html table row up & down in javascript

Best Javascript Courses On Udemy

Top Javascript Courses

Best Javascript Courses On Udemy


You Will Learn:
  • Lifetime access to my HD quality videos. No monthly subscription. Learn at your own pace, whenever you want.
  • You will gain a deep and true understanding of how JavaScript works behind the scenes.
  • You will understand and use complex features like the 'this' keyword, function constructors, prototypal inheritance, first-class functions, closures, and more.
  • You will learn how to organize and structure your code using modules and functions. Because coding is not just writing code, it's also thinking about your code!
  • You will get my personal support in the Q&A.
  • Multiple coding challenges to practice your new skills (solutions included).
  • You will go all the way from JavaScript beginner to advanced JavaScript developer.
  • You will be able to debug your code and understand other developer's code.
  • You will code two beautiful real-world apps (starter code included) and not just boring toy apps.
  • You will become familiar with the new features of ES6 / ES2015.
  • Downloadable starter code and final code for each section.
  • A final course exam with 30 questions to truly test your JavaScript knowledge.
Instructor: Jonas Schmedtmann
Social Proof: 18,664 students - rating 4.7

Review Example:



  • Grasp how Javascript works and it's fundamental concepts
  • Understand advanced concepts such as closures, prototypal inheritance, IIFEs, and much more.
  • Avoid common pitfalls and mistakes other Javascript coders make
  • Build your own Javascript framework or library
  • Write solid, good Javascript code
  • Drastically improve your ability to debug problems in Javascript.
  • Understand the source code of popular Javascript frameworks
Instructor: Anthony Alicea
Social Proof: 71,350 students - rating 4.7

Review Example:


Javascript Add Active Class To A DIV

How To Set Active Class In Javascript  

javascript set active class


In This Javascript Tutorial we will See How To Add A Class To an HTML element And Delete the class From Other Elements On Element Click using JS And Netbeans Editor .


Project Source Code:

<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript: Element Siblings</title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <style>
            
            .parent{overflow: hidden;margin-top:50px;}
            
            .child{width:200px;height: 150px;float: left;margin-left:20px;border:1px solid #000;
                   text-align:center;line-height:150px;cursor: pointer;background-color:#ddd;}
            
            .bak{ background-color: red; color: #fff; font-weight:bold; }
            
        </style>
        
    </head>
    <body>
        
        <div class="parent">
            <div class="child">DIV1</div>
            <div class="child">DIV2</div>
            <div class="child">DIV3</div>
            <div class="child">DIV4</div>
            <div class="child">DIV5</div>
        </div>
        
        <script>
            
            var elements = document.getElementsByClassName("child");
            for(var i = 0; i < elements.length; i++)
            {
                elements[i].onclick = function(){
                    
                    // remove class from sibling
                    
                    var el = elements[0];
                    while(el)
                    {
                        if(el.tagName === "DIV"){
                            //remove class
                            el.classList.remove("bak");
                            
                        }
                        // pass to the new sibling
                        el = el.nextSibling;
                    }
                    
                  this.classList.add("bak");  
                };
            }
            
        </script>    
        
    </body>
</html>


OUTPUT:

Javascript Set Active Class To An Element

Javascript - Add, Remove, Toggle Class Name

How To Insert, Delete, Toggle Class In Javascript  

Add Remove Toggle Class In Javascript


In This Javascript Tutorial we will See How To Add, Remove, or Toggle, and Show All  className of an HTML element using JS And Netbeans Editor .


Project Source Code:

<!DOCTYPE html>
<html>
    <head>
        <title>Add - Remove - Toggle - Show Classes</title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <style>
            
             div{background-color: #999;width: 200px;height: 80px}
            .class1{border:2px solid red;}
            .class2{background-color: yellow;}
            .class3{margin-left: 50px;}
            .class4{width:300px;}
            
        </style>
        
    </head>
    <body>
        
        <div class="class2 class3 class4" id="box"></div><br>
        <button onclick="addClass();">Add Class</button>
        <button onclick="removeClass();">Remove Class</button>
        <button onclick="toggleClass();">Toggle Class</button>
       
 <script>
        
          // show element class list
          var box = document.getElementById("box");
          var cList = box.classList.toString();
          console.log(cList);
            
         // add class to the div
          function addClass()
          {
              box.classList.add("class2");
          }
          
          // remove class from the div
          function removeClass()
          {
              box.classList.remove("class2");
          }
           
          // toggle the class from the div
          function toggleClass()
          {
              box.classList.toggle("class1");
          }

        </script>
        
    </body>
</html>

OUTPUT:
default
default state
remove class
remove class
add class
add class
toggle class
toggle class: click 1
toggle class
toggle class: click 2

JAVA - Make Transparent Color

How To Make Transparent Background Color In Java

Transparent Color In Java



In this Java Tutorial we will see How To Set A Transparent Background Color To JPanel In Java NetBeans .




Project Source Code:


package javaapp;

import java.awt.BorderLayout;
import java.awt.Color;

/**
 * @author 1bestcsharp.blogspot.com
 */
public class trcolor extends javax.swing.JFrame {

    /**
     * Creates new form trcolor
     */
    public trcolor() {
        initComponents();
         jPanel1.setBackground(new Color(242,38,19, 150));
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(666, 666));
        setSize(new java.awt.Dimension(600, 500));
        getContentPane().setLayout(null);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 390, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(110, 80, 390, 240);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/slider.jpg"))); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 8, 557, 390);

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(trcolor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(trcolor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(trcolor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(trcolor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new trcolor().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}




////// OUTPUT : 

Transparent JPanel Background Color In Java