Found 9150 Articles for Object Oriented Programming

How to add a tooltip to a component in Java?

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

503 Views

Let us first create a component −JLabel label1; label1 = new JLabel("Id", SwingConstants.CENTER); Now, set the tooltip for the above component − label1.setToolTipText("Enter Id");ExampleThe following is an example to add a tooltip to a component in Java −package 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!");       JLabel label1, label2, label3;       frame.setLayout(new GridLayout(2, 2));       label1 = new JLabel("Id", ... Read More

How to get this node’s last child in a JTree with Java?

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

293 Views

Let’s say we want the last child node of a node, then use the getLastChild() method −node3.getFirstChild()Display the node’s last child on Console −System.out.println("The last child of node 3 = "+node3.getLastChild());The following is an example to get this node’s last child in a JTree −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories ... Read More

How to add separator in a ToolBar with Java?

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

208 Views

To add a separator, use the addSeparator() method. Let us first create a toolbar −JToolBar toolbar = new JToolBar();Now, we will create some components and set a separator to separate them as shown below −toolbar.add(new JTextArea(" Add name here")); toolbar.add(new JButton("Submit Name")); toolbar.addSeparator(); toolbar.add(new JTextArea(" Add age here")); toolbar.add(new JButton("Submit Age"));The following is an example to add separator in a ToolBar with Java −Examplepackage my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JToolBar; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel(new BorderLayout());       ... Read More

Java Program to display JTabbedPane on the right

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

146 Views

To display JTabbedPane on the right, you need to set the location to the right using the constant RIGHT −JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);The following is an example to display JTabbedPane on the right −Examplepackage my; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Technologies");       JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);       JPanel panel1, panel2, panel3, panel4, panel5;       panel1 = new JPanel();       panel2 = new JPanel();       panel3 = new JPanel();       panel4 = ... Read More

How to create a vertical toolbar in Java?

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

524 Views

Let us first create a toolbar −JToolBar toolbar = new JToolBar();Now, set the orientation to create vertical toolbar −toolbar.setOrientation(SwingConstants.VERTICAL);The following is an example to create a vertical toolbar in Java −package my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JToolBar; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JToolBar toolbar = new JToolBar();       toolbar.setOrientation(SwingConstants.VERTICAL);       toolbar.add(new JTextArea());       toolbar.add(new JButton("Submit"));       frame.add(toolbar, BorderLayout.WEST);       frame.setTitle("Vertical ToolBar");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   ... Read More

Can we combine GridLayout and BorderLayout in Java?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

652 Views

Yes, we can do that with Java Swings as shown below. Here, we have a panel set with GridLayout and another panel with BorderLayout −JPanel panelGrid = new JPanel(new GridLayout(10, 5, 10, 10)); panelGrid.add(new JCheckBox("Demo CheckBox1")); panelGrid.add(new JCheckBox("Demo CheckBox2")); panelGrid.add(btnAPanel); panelGrid.add(btnBPanel); panelGrid.add(btnCPanel); panelGrid.add(btnDPanel); JPanel panelBrdLayout = new JPanel(new BorderLayout()); panelBrdLayout.add(panelGrid, BorderLayout.NORTH);The following is an example to combine GridLayout and BorderLayout −package my; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JButton btnA = new JButton("Button1");       JButton btnB ... Read More

Insert a tab after the first tab of a JTabbedPane container

Anvi Jain
Updated on 30-Jul-2019 22:30:26

120 Views

We are inserting a tab just after the first tab by using the index value 1. Here, index 1 would be location 2nd i.e. just after the first tab of the JTabbedPane container.The following is an example to insert a tab after the first tab −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Devices");       JTabbedPane tabbedPane = new JTabbedPane();       JTextArea text = new JTextArea(100, 100);       JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;   ... Read More

How can I create a dialog box in Java with Yes No and cancel buttons?

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

2K+ Views

For a dialog box with Yes No Cancel buttons, you need to use the JOptionPane.showConfirmDialog(), wherein you will get a confirmation dialog box.The following is an example to create a dialog box in Java with Yes No and cancel buttons −Examplepackage my; import java.awt.Dimension; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.UIManager; public class SwingDemo {    public static void main(String[] args) {       ImageIcon icon = new ImageIcon("E −ew.PNG");       JPanel panel = new JPanel();       panel.setSize(new Dimension(250, 100));       panel.setLayout(null);       JLabel label1 = ... Read More

How to create JSplitPane to divide components in Java?

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

244 Views

At first, set two components which we will dividing −JComponent one = new JLabel("Label One"); one.setBorder(BorderFactory.createLineBorder(Color.yellow)); JComponent two = new JLabel("Label Two"); two.setBorder(BorderFactory.createLineBorder(Color.orange));Now, split it using SplitPane −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSplitPane; public class SwingDemo {    public static void main(String[] a) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComponent one = new JLabel("Label One");       one.setBorder(BorderFactory.createLineBorder(Color.yellow));       JComponent two = new JLabel("Label Two");       two.setBorder(BorderFactory.createLineBorder(Color.orange));       JComponent three = new JLabel("Label ... Read More

Can we change the Cursor with Java Swing?

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

2K+ Views

Yes, we can change the default cursor representation in Java. Let us first create a button component −JButton button = new JButton("Button with two borders");Whenever user will keep the mouse cursor on the above button component, the cursor would change to hand cursor −Cursor cursor = button.getCursor(); button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));The following is an example to change the cursor −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Cursor; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.LineBorder; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");   ... Read More

Advertisements