How to enable row selection in a JTable with Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

To enable row selection, use the setRowSelectionAllowed () method and set it to TRUE −table.setCell setRowSelectionAllowed(true);The following is an example to enable row selection in a JTable −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder(          BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP));       String[][] rec = {          { "1", "Steve", "AUS" },   ... Read More

Get MAX() on column in two MySQL tables?

George John
Updated on 30-Jul-2019 22:30:26

373 Views

Use GREATEST() to find the maximum. Let us first create a table −mysql> create table DemoTable1    (    Number int    ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(80); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1 values(229); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(575); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+--------+ | Number | +--------+ | 80 | | 229 ... Read More

Differentiate between the prefix and postfix forms of the ++ operator in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

Java provides two operators namely ++ and --, to increment and decrement values by 1 respectively.There are two variants of these operators −Pre-increment/decrement − This form, increments/decrements the value first, and then performs the specified operation.ExampleIn the following example, the initial value of the variable i is 5. We are printing the incremented value of it using the pre increment operator.Since we are using the pre increment operator, the value of i is incremented then printed. Live Demopublic class ForLoopExample {    public static void main(String args[]) {       int i = 5;       System.out.println(++i);     ... Read More

C/C++ Program to Count Inversions in an array using Merge Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

607 Views

The inversions of an array indicate; how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions, and in other case, the number of inversions will be maximum, if the array is reversed.To solve this problem, we will follow the Merge sort approach to reduce the time complexity, and make it in Divide and Conquer algorithm.InputA sequence of numbers. (1, 5, 6, 4, 20).OutputThe number of inversions required to arrange the numbers into ascending order.Here the number of inversions are 2. First inversion: (1, 5, 4, 6, 20) ... Read More

How to display row count in Java Swing JList

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

358 Views

Use JList getVisibleRowCount() method to display the row count in a JList:String sports[]= {"Tennis", "Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"}; Jlist list = new JList(sports); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount);The following is an example to display row count in JList:Exampleimport java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       String sports[]= {"Tennis", Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"};       list = ... Read More

What's the best way to trim std::string in C++?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

8K+ Views

Here we will see how to trim the strings in C++. The trimming string means removing whitespaces from left and right part of the string.To trim the C++ string, we will use the boost string library. In that library, there are two different methods called trim_left() and trim_right(). To trim string completely, we can use both of them.Example#include #include using namespace std; main(){    string myStr = " This is a string ";    cout

How to create Android Notification with BroadcastReceiver?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

4K+ Views

This example demonstrate about How to create Android Notification with BroadcastReceiver.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to res/menu/main_menu.xml.             Step 4 − Add the following code to src/MainActivity.package app.tutorialspoint.com.notifyme ; import android.app.AlarmManager ; import android.app.Notification ; import android.app.NotificationManager ; import android.app.PendingIntent ; import android.content.Context ; import android.content.Intent ; import android.os.Bundle ; import android.os.SystemClock ; import android.support.v4.app.NotificationCompat ; import android.support.v7.app.AppCompatActivity ... Read More

How do I add a field to existing record in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

264 Views

You can use update command to add a field to existing record. Let us first create a collection with documents −> db.addAFieldToEveryRecordDemo.insertOne({"ClientName":"Chris", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00e32588d4a6447b2e061") } > db.addAFieldToEveryRecordDemo.insertOne({"ClientName":"Robert", "ClientAge":36}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00e59588d4a6447b2e062") }Following is the query to display all documents from a collection with the help of find() method −> db.addAFieldToEveryRecordDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd00e32588d4a6447b2e061"),    "ClientName" : "Chris",    "ClientAge" : 34 } {    "_id" : ObjectId("5cd00e59588d4a6447b2e062"),    "ClientName" : "Robert",    "ClientAge" : 36 }Here is the query ... Read More

Inserting an Image into a JTextPane Component with Java

Smita Kapse
Updated on 30-Jul-2019 22:30:26

460 Views

Let’s say the following is our JTextPane with orange background color −JTextPane textPane = new JTextPane(); textPane.setBackground(Color.orange);Now, set the style and attributes. Also, set the font −SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); textPane.setCharacterAttributes(attributeSet, true); textPane.setText("Recall this and "); Font font = new Font("Verdana", Font.BOLD, 22); textPane.setFont(font);After the text displayed above, we will insert an image using setIcon() −StyledDocument doc = (StyledDocument) textPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setIcon(style, new ImageIcon("E:\kind.png")); doc.insertString(doc.getLength(), "invisible text", style);The following is an example to insert an image into a component. Here, we have inserted an image into a JTextPane component −Examplepackage my; import ... Read More

Java Program to enable column selection in a JTable

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

158 Views

To enable colum selection, use the setColumnSelectionAllowed() method and set it to TRUE −table.setCell setColumnSelectionAllowed(true);The following is an example to enable column selection in a table −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder(          BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP));       String[][] rec = {          { "1", "Steve", "AUS" },     ... Read More

Advertisements