Java - Move JTable Row Up And Down

How To Move JTable Selected Rows Up N Down In Java Using NetBeans

move jtable selected row up and down in java



In this Java Tutorial we will see How To Get Jtable Selected Row And Move This Row Up & Down By Changing The position Using DefaultTableModel, moveRow() Function On  Two Buttons Click Event  In  Java Programming language And NetBeans IDE .




Project Source Code:

    // change jtable selection background color
    jTable1.setSelectionBackground(Color.BLACK);


   
    boolean row_Is_Selected = false;
    int index;
    DefaultTableModel model;


    // move selected row up
    private void jButtonUPActionPerformed(java.awt.event.ActionEvent evt) {                               
        if(row_Is_Selected == false)
        {
            model = (DefaultTableModel)jTable1.getModel();
            row_Is_Selected = true;
        }
        
        index = jTable1.getSelectedRow();
        if(index > 0){
            // move rows from the index to index into the position index -1
            model.moveRow(index, index, index - 1);
            // set selection to the new position
            jTable1.setRowSelectionInterval(index - 1, index - 1);
            
        }
    }                                         


    // move selected row down
    private void jButtonDownActionPerformed(java.awt.event.ActionEvent evt) {                           
        if(row_Is_Selected == false)
        {
            model = (DefaultTableModel)jTable1.getModel();
            row_Is_Selected = true;
        }
        
        index = jTable1.getSelectedRow();
        if(index < model.getRowCount() - 1){
            model.moveRow(index, index, index + 1);
            jTable1.setRowSelectionInterval(index + 1, index + 1);
            
        }
    }                                           



////// OUTPUT : 

move jtable selected row up and down



C# - Move DataGridView Row Up And Down

How To Move Selected Datagridview Row Up N Down In C#

Move DataGridView Row Up & Down In C#


In This C# Tutorial  We Will See How To Move Selected Datagridview Row Up Or Down On Button Click In CSharp Programming Language.


Project Source Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class datagridview_row_up_down : Form
    {
        public datagridview_row_up_down()
        {
            InitializeComponent();
        }

        DataTable table = new DataTable();
        int rowIndex;
        private void datagridview_row_up_down_Load(object sender, EventArgs e)
        {
            // populate dgv from datatable

            // add columns
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("First Name", typeof(string));
            table.Columns.Add("Last Name", typeof(string));
            table.Columns.Add("Age", typeof(int));

            // add rows
            table.Rows.Add(1, "First A", "Last A", 10);
            table.Rows.Add(2, "First B", "Last B", 20);
            table.Rows.Add(3, "First C", "Last C", 30);
            table.Rows.Add(4, "First D", "Last D", 40);
            table.Rows.Add(5, "First E", "Last E", 50);
            table.Rows.Add(6, "First F", "Last F", 60);
            table.Rows.Add(7, "First G", "Last G", 70);
            table.Rows.Add(8, "First H", "Last H", 80);

            dataGridView1.DataSource = table;

        }

        private void buttonUP_Click(object sender, EventArgs e)
        {
            // get selected row index
            rowIndex = dataGridView1.SelectedCells[0].OwningRow.Index;

            // create a new row
            DataRow row = table.NewRow();

            // add values to the new row
            row[0] = int.Parse(dataGridView1.Rows[rowIndex].Cells[0].Value.ToString());
            row[1] = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
            row[2] = dataGridView1.Rows[rowIndex].Cells[2].Value.ToString();
            row[3] = int.Parse(dataGridView1.Rows[rowIndex].Cells[3].Value.ToString());

            if (rowIndex > 0) { 
                // delete the selected row
                table.Rows.RemoveAt(rowIndex);
                // add the new row 
                table.Rows.InsertAt(row, rowIndex - 1);
                dataGridView1.ClearSelection();
                // select the new row
                dataGridView1.Rows[rowIndex - 1].Selected = true;
            }

        }

        private void buttonDOWN_Click(object sender, EventArgs e)
        {
            rowIndex = dataGridView1.SelectedCells[0].OwningRow.Index;
            DataRow row = table.NewRow();

            row[0] = int.Parse(dataGridView1.Rows[rowIndex].Cells[0].Value.ToString());
            row[1] = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
            row[2] = dataGridView1.Rows[rowIndex].Cells[2].Value.ToString();
            row[3] = int.Parse(dataGridView1.Rows[rowIndex].Cells[3].Value.ToString());

            if (rowIndex < dataGridView1.Rows.Count-2)
            {
                table.Rows.RemoveAt(rowIndex);
                table.Rows.InsertAt(row, rowIndex + 1);
                dataGridView1.ClearSelection();
                dataGridView1.Rows[rowIndex + 1].Selected = true;
            }
        }
    }
}


// OUTPUT :

C# Move DataGridView Row Up N Down




VB.Net - Make A DropDown Menu

How To Make A Drop And Down Menu Using Visual Basic.Net

dropdown menu in vbnet


In This VB.Net Tutorial  We Will See How To Create A Drop-Down Menu Using Panels, Buttons, And Timers In Visual Basic.Net Programming Language.


Project Source Code:

Public Class drop_down_menu

    ' pl1 = panel1 height
    Dim pl1 As Integer = 60

    ' pl2 = panel2 height
    Dim pl2 As Integer = 60

    ' pl3 = panel3 height
    Dim pl3 As Integer = 60

    Private Sub drop_down_menu_Load(sender As Object, e As EventArgs) Handles MyBase.Load


    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        ' 250 = panel height
        If pl1 > 240 Then

            Timer1.Stop()

        Else
            Me.Panel1.Size = New Size(Me.Panel1.Size.Width, pl1)
            pl1 += 10
        End If

    End Sub

    Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover

        ' on button1 mouse hover set panel2 and panel3 to the default height (60) 
        Me.Panel2.Size = New Size(Me.Panel2.Size.Width, pl2)
        Me.Panel3.Size = New Size(Me.Panel3.Size.Width, pl3)
        Timer1.Start()

    End Sub

    Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave

        Timer1.Stop()
        pl1 = 60

    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick

        If pl2 > 240 Then

            Timer2.Stop()

        Else
            Me.Panel2.Size = New Size(Me.Panel2.Size.Width, pl2)
            pl2 += 10
        End If

    End Sub

    Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick

        If pl3 > 240 Then

            Timer3.Stop()

        Else
            Me.Panel3.Size = New Size(Me.Panel3.Size.Width, pl3)
            pl3 += 10
        End If

    End Sub

    Private Sub Button8_MouseHover(sender As Object, e As EventArgs) Handles Button8.MouseHover

        ' on button8 mouse hover set panel1 and panel3 to the default height (60) 
        Me.Panel1.Size = New Size(Me.Panel1.Size.Width, pl1)
        Me.Panel3.Size = New Size(Me.Panel3.Size.Width, pl3)
        Timer2.Start()

    End Sub

    Private Sub Button8_MouseLeave(sender As Object, e As EventArgs) Handles Button8.MouseLeave

        Timer2.Stop()
        pl2 = 60

    End Sub

    Private Sub Button12_MouseHover(sender As Object, e As EventArgs) Handles Button12.MouseHover

        ' on button12 mouse hover set panel2 and panel1 to the default height (60) 
        Me.Panel2.Size = New Size(Me.Panel2.Size.Width, pl2)
        Me.Panel1.Size = New Size(Me.Panel1.Size.Width, pl1)
        Timer3.Start()

    End Sub

    Private Sub Button12_MouseLeave(sender As Object, e As EventArgs) Handles Button12.MouseLeave

        Timer3.Stop()
        pl3 = 60

    End Sub
End Class


//OutPut:

vb.net drop-down menu




Java - Move JList Item Up And Down

How To Move Selected JList Item Up N Down In Java NetBeans

java jlist item up n down



In this Java Tutorial we will see How To Make Two Buttons To Move Selected List Item Up Or Down On Click In Java Using NetBeans .




Project Source Code:

        // populate jlist using string array and for loop
        DefaultListModel model = new DefaultListModel();
        jList1.setModel(model);
        
        String[] JlistItems = {"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11"};
        for(int i = 0; i < JlistItems.length; i++)
        {
            model.add(i, JlistItems[i]);
        }

// button to move selected jlist item up
private void jButton_UPActionPerformed(java.awt.event.ActionEvent evt) {                                           
        String selectedItem = jList1.getSelectedValue();//get item value
        int itemIndex = jList1.getSelectedIndex();// get item index
        DefaultListModel model = (DefaultListModel)jList1.getModel();// get list model
        
        if(itemIndex > 0){
            model.remove(itemIndex);// remove selected item from the list
            model.add(itemIndex - 1, selectedItem);// add the item to a new position in the list
            jList1.setSelectedIndex(itemIndex - 1);// set selection to the new item
        }   
    }        


// button to move selected jlist item down
private void jButton_DownActionPerformed(java.awt.event.ActionEvent evt) {                                             
        String selectedItem = jList1.getSelectedValue();
        int itemIndex = jList1.getSelectedIndex();
        DefaultListModel model = (DefaultListModel)jList1.getModel();
        
        if( itemIndex < model.getSize() -1 ){
            model.remove(itemIndex);
            model.add(itemIndex + 1, selectedItem);
            jList1.setSelectedIndex(itemIndex + 1);
        }
    } 

// OUTPUT :

java move selected jlist item up and down




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