Anvi Jain

Anvi Jain

427 Articles Published

Articles by Anvi Jain

Page 10 of 43

How to display labels in the form of a 4 column table with GridLayout in Java?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 960 Views

We will displays labels in a label with 5 rows and 4 columns using GridLayout −JPanel panel = new JPanel(new GridLayout(5, 4, 10, 10));Use a for loop and loop through 1 to 20 to display 20 labels −for (int i = 1; i

Read More

How to make MySQL table primary key auto increment?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 1K+ Views

To make MySQL table primary key auto increment, use the below syntaxCREATE TABLE yourTableName    (    yourColumnName INT(6) ZEROFILL NOT NULL AUTO_INCREMENT,    PRIMARY KEY(yourColumnName)    );Let us first create a table and set primary key auto increment −mysql> CREATE TABLE DemoTable    (    UserId INT(6) ZEROFILL NOT NULL AUTO_INCREMENT,    PRIMARY KEY(UserId)    ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> INSERT INTO DemoTable values(); Query OK, 1 row affected (0.12 sec) mysql> INSERT INTO DemoTable values(); Query OK, 1 row affected (0.13 sec) mysql> INSERT ...

Read More

How to left align components vertically using BoxLayout with Java?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 3K+ Views

To align components vertically, use the BoxLayout −JFrame frame = new JFrame(); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));Now, create a Panel and add some buttons to it. After that set left alignment of components which are already arranged vertically using Component.LEFT_ALIGNMENT constant −JPanel panel = new JPanel(); JButton btn1 = new JButton("One"); JButton btn2 = new JButton("Two"); JButton btn3 = new JButton("Three"); JButton btn4 = new JButton("Four"); JButton btn5 = new JButton("Five"); panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); panel.add(btn5); panel.setAlignmentX(Component.LEFT_ALIGNMENT);The following is an example to left align components vertically with BoxLayout −Examplepackage my; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; ...

Read More

C++ Program to Generate Random Partition out of a Given Set of Numbers or Characters

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 330 Views

This is a C++ program to generate Random Partition out of a given set of numbers or characters.AlgorithmBegin    Take the integers or characters as input.    For choice 1:       Take the input of the n integer array.       Assign l = 0 to traverse the array.       Using rand(), generate random integer partition of n.       For each partition i, print next i integer from index value l.    For choice is 2:       Take the input of a string in ch[].       Calculate the length ...

Read More

How to stop C++ console application from exiting immediately?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 1K+ Views

Sometimes we have noticed that the console is being closed immediately after displaying the result. So we cannot see the result properly. Here we will see how we can stop the console from closing.The idea is very simple. We can use the getchar() function at the end. This will wait for one character. If one character is pressed, the console will exit.Example Live Demo#include using namespace std; int main() {    cout

Read More

How to center a Window in Java?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 1K+ Views

To center a Window in Java, use the getCenterPoint() method. Set the width and height and use the below formulae to set bounds −Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); int width = 500; int height = 200; frame.setBounds(center.x - width / 2, center.y - height / 2, width, height);The following is an example to center a window −Examplepackage my; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame.setDefaultLookAndFeelDecorated(true);       JFrame frame = new JFrame("Register!");     ...

Read More

Select two fields and return a sorted array with their distinct values in MongoDB?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 447 Views

To select two fields and return a sorted array with distinct values, use aggregate framework along with $setUnion operator. Let us first create a collection with documents −> db.sortedArrayWithDistinctDemo.insertOne( ...    { value1: 4, value2: 5} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e666") } > db.sortedArrayWithDistinctDemo.insertOne( ...    {value1: 5, value2: 6} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e667") } > db.sortedArrayWithDistinctDemo.insertOne( ...    {value1: 7, value2: 4} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e668") }Following is the query to display all documents from a collection with the ...

Read More

How to disallow resizing component with GridBagLayout in Java

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 892 Views

To disallow resizing component with GridBagLayout, use the GridBagConstraints NONE constant −GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.NONE;The following is an example to disallow resizing component with GridBagLayout −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Demo Frame");       JPanel panel = new JPanel();       GridBagLayout layout = new GridBagLayout();       panel.setLayout(layout);       GridBagConstraints gbc = new GridBagConstraints();       gbc.fill = GridBagConstraints.NONE;   ...

Read More

How to measure time taken by a function in C?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 15K+ Views

Here we will see how to calculate the time taken by the process. For this problem, we will use the clock() function. The clock() is present in the time.h header file.To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.Example#include #include void take_enter() {    printf("Press enter to stop the counter ");    while(1) {       ...

Read More

MongoDB multidimensional array projection?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 690 Views

For MongoDB multidimensional array projection, you need to use aggregate framework. Let us first create a collection with documents. Here, we have multidimensional array for Student marks −> db.multiDimensionalArrayProjection.insertOne( ...    { ...       "StudentFirstName" : "Chris", ...       "StudentMarks" : [ [98, 99], [56, 79] ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b75a9cb58ca2b005e66c") }Following is the query to display all documents from a collection with the help of find() method −> db.multiDimensionalArrayProjection.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc6b75a9cb58ca2b005e66c"),    "StudentFirstName" : "Chris",    "StudentMarks" ...

Read More
Showing 91–100 of 427 articles
« Prev 1 8 9 10 11 12 43 Next »
Advertisements