Set Different Look and Feels to Swing Components in Java

raja
Updated on 03-Jul-2020 12:21:53

2K+ Views

The Java Swing allows us to customize the GUI by changing the look and feel(L&F). The look defines the general appearance of components and the feel defines their behavior. L&Fs are subclasses of the LookAndFeel class and each L&F is identified by its fully qualified class name. By default, the L&F is set to the Swing L&F ( Metal L&F)To set the L&F programmatically, we can call the method setLookAndFeel () of the UIManager class. The call to setLookAndFeel must be done before instantiating any Java Swing class, otherwise, the default Swing L&F will be loaded.Syntaxpublic static void setLookAndFeel(LookAndFeel newLookAndFeel) throws UnsupportedLookAndFeelExceptionExampleimport java.awt.*; import ... Read More

Add Multiple Sub-Panels to the Main Panel in Java

raja
Updated on 03-Jul-2020 12:16:44

4K+ Views

A JPanel is a subclass of JComponent class and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components like buttons, text fields, labels, tables, lists, trees,  etc. to a JPanel.We can also add multiple sub-panels to the main panel using the add() method of Container class.Syntaxpublic Component add(Component comp)Exampleimport java.awt.*; import javax.swing.*; public class MultiPanelTest extends JFrame {    private JPanel mainPanel, subPanel1, subPanel2;    public MultiPanelTest() {       setTitle("MultiPanel Test");       mainPanel = new JPanel(); // main panel       mainPanel.setLayout(new GridLayout(3, 1));       mainPanel.add(new JLabel("Main ... Read More

Count Records That Begin With Specific Letters in MySQL

AmitDiwan
Updated on 03-Jul-2020 12:02:02

380 Views

Let us first create a table −mysql> create table DemoTable775 ( FirstName varchar(100) ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable775 values('Adam') ; Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable775 values('Carol'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable775 values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable775 values('Sam'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable775 values('Adan'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select ... Read More

ORDER BY Clause in MySQL

Anvi Jain
Updated on 03-Jul-2020 12:01:47

149 Views

To order by word in MySQL, you need to use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20),    StudentFavouriteSubject varchar(100) ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentFirstName, StudentFavouriteSubject) values('Larry', 'Java'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentFirstName, StudentFavouriteSubject) values('Sam', 'C'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(StudentFirstName, StudentFavouriteSubject) values('Bob', 'MongoDB'); Query OK, 1 row affected (0.17 sec) mysql> ... Read More

Set Border to JComboBox Items in Java

raja
Updated on 03-Jul-2020 11:58:06

696 Views

A JComboBox is a subclass of JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener and ItemListener interfaces when the user actions on a combo box. We can set the border to the items of a JComboBox by rendering a JComboBox which extends the DefaultListCellRenderer class and need to override the getListCellRendererComponent() method.Syntaxpublic Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)Exampleimport java.awt.*; import javax.swing.*; public class JComboBoxTest extends JFrame {    public JComboBoxTest() {       setTitle("JComboBox Test");       ... Read More

Rotate JLabel Text in Java

raja
Updated on 03-Jul-2020 11:56:43

3K+ Views

A JLabel is a subclass of JComponent class and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface. By default, JLabel can display a text in the horizontal position and we can rotate a JLabel text by implementing the rotate() method of Graphics2D class inside the paintComponent().Syntaxpublic abstract void rotate(double theta, double x, double y)Exampleimport java.awt.*; import java.awt.geom.*; import javax.swing.*; public class RotateJLabelTest extends JFrame {    public RotateJLabelTest() {       setTitle("Rotate JLabel");       JLabel label ... Read More

Ways to Iterate a LinkedList in Java

raja
Updated on 03-Jul-2020 11:54:51

365 Views

A LinkedList is a data structure that contains a group of nodes connected in a sequential manner with a pointer. A LinkedList can behave as a dynamic array and it allocates space for each element separately in its own block of memory called a Node. Each node contains two fields, a "data" field to store an element type the list holds and a "next" field which is a pointer used to link one node to the next node.We can iterate the elements of a LinkedList in three ways in Java.Using IteratorWe can iterate the elements of a LinkedList through the Iterator class.Exampleimport java.util.*; ... Read More

Make Android Status Bar Notification Persist Across Phone Reboot

Nishtha Thakur
Updated on 03-Jul-2020 11:54:06

539 Views

This example demonstrate about How to make an Android status bar notification persist across phone rebootStep 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 src/MainActivity.package app.tutorialspoint.com.notifyme ; import android.os.Bundle ; import android.support.v7.app.AppCompatActivity ; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate (Bundle savedInstanceState) {       super .onCreate(savedInstanceState) ;       setContentView(R.layout. activity_main ) ;    } }Step 4 − Add ... Read More

Default Value of a Local Variable in Java

raja
Updated on 03-Jul-2020 11:53:32

6K+ Views

The local variables can be declared in methods, code blocks, constructors, etc in Java. When the program control enters the methods, code blocks, constructors, etc. then the local variables are created and when the program control leaves the methods, code blocks, constructors, etc. then the local variables are destroyed. The local variables do not have any default values in Java. This means that they can be declared and assigned a value before the variables are used for the first time, otherwise, the compiler throws an error.Examplepublic class LocalVariableTest {    public void print() {       int num;     ... Read More

Add Padding to a JTextField in Java

raja
Updated on 03-Jul-2020 11:49:59

3K+ Views

A JTextField is a subclass of JTextComponent class and it is one of the most important components that allow the user to input text value in a single-line format. A JTextField class will generate an ActionListener interface when we trying to enter some input inside it. The important methods of a JTextField class are setText(), getText(), setBorder(), setEnabled(),  etc. We can add padding to a JTextField using the setMargin(Insets s) of JTextComponent class.Syntaxpublic void setMargin(Insets m)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextfieldPaddingTest extends JFrame {    private JTextField jtf;    public JTextfieldPaddingTest() {       jtf = new JTextField("Welcome to Tutorials Point"); ... Read More

Advertisements