Found 4338 Articles for Java 8

How to create a vertical toolbar in Java?

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

328 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

419 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

68 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

1K+ 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

146 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

931 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

Java Program to count the child of root node in a JTree

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

191 Views

Use the getChildCount() method to count the number of root node in a JTree. Here, our root node is “node”. Therefore to count its child node, use −System.out.println("Number of children of node = " + node.getChildCount());We have displayed the count in the Console as shown above. The following is an example to count the child of root node 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("Website");     ... Read More

Disable only the vertical scrollbar in Java

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

1K+ Views

To disable only the vertical scrollbar, use the VERTICAL_SCROLLBAR_NEVER constant, which displays only the horizontal scrollbar.The following is an example to disable only the vertical scrollbar in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("Online Compiler");       JButton button2 = new JButton("Quiz");       JButton button3 = new JButton("Questions and Answers");       JButton button4 = new JButton("Videos"); ... Read More

Can we prevent the collapse of nodes and child nodes in a JTree with Java?

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

280 Views

Yes, we can prevent the collapse of nodes and child nodes in a JTree using setExpandedState() method. This method sets the expanded state of this JTree.If state istrue, all parents of path and path are marked asexpanded.The following is an example that prevents the collapse of nodes and child nodes in a JTree with Java −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.text.Position; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Project");       DefaultMutableTreeNode ... Read More

How to set the content of the label to be right-justified and top-aligned in Java?

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

72 Views

To set the text of the label component to be right-justified and top-aligned, you need to set the alignment. Set the label to be on the right and top aligned −JLabel label = new JLabel("Fav Sports", JLabel.RIGHT); label.setVerticalAlignment(JLabel.TOP);Here, we have set the size of the label as well as the color that includes foreground and background color −label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.YELLOW); label.setForeground(Color.RED);The following is an example to set the content of the lable to be right-justified and top-aligned −package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo ... Read More

Advertisements