Found 4338 Articles for Java 8

How to set Line Border color and width with Java?

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

4K+ Views

To set Line Border color and width, use the LineBorder. At first, set a panel wherein we need to set the line border −JPanel panel = new JPanel();Now, create a border and set on the panel created above −Border border = new LineBorder(Color.ORANGE, 4, true); panel.setBorder(border);The following is an example to set LineBorder color and width −package my; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.Border; import javax.swing.border.LineBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();   ... Read More

How to add components with a relative Y position in Java?

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

114 Views

To add components with a relative Y position, use the GridBagConstraints.RELATIVE constant. Set this to gridy field −GridBagConstraints constraints = new GridBagConstraints(); constraints.gridy = GridBagConstraints.RELATIVE;The following is an example to add components with a relative Y position in Java −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       GridBagConstraints constraints = new GridBagConstraints();       constraints.gridx = ... Read More

How to create Vertical progress bar occupying the entire frame in Java

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

161 Views

For this, create a vertical progress bar −JProgressBar progressBar = new JProgressBar(JProgressBar.VERTICAL); progressBar.setEnabled(true);Also, set the bounds −progressBar.setBounds(70, 50, 120, 30);The following is an example to create vertical progress bar occupying the entire frame −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JProgressBar; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JProgressBar progressBar = new JProgressBar(JProgressBar.VERTICAL);       progressBar.setEnabled(true);       progressBar.setBounds(70, 50, 120, 30);       progressBar.setBackground(Color.orange);       progressBar.setForeground(Color.white);       progressBar.setStringPainted(true);       ... Read More

How to customize how a JTabbedPane looks in Java?

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

631 Views

To customize how a JTabbedPane looks, change its font style, font face, font size and the background and foreground colors.Let’s say the following is the JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Now, let us customize the above created JTabbedPane −tabbedPane.setBackground(Color.orange); tabbedPane.setForeground(Color.white); Font font = new Font("Verdana", Font.CENTER_BASELINE, 18); tabbedPane.setFont(font);The following is an example to customize JTabbedPane −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Technologies");       JTabbedPane tabbedPane = new JTabbedPane();       JPanel panel1, panel2, panel3, panel4, panel5;       ... Read More

How to get the number of siblings of this node in a JTree?

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

111 Views

To get the count of siblings of this node, use the getSiblingCount() method. Let’s say you have a node with 4 child nodes. Find the sibling of any of this node’s child node. Here, “eight” is the child node −eight.getSiblingCount());Note − Remember, a node is it’s own sibling.The following is an example to get the number of siblings of this 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("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories (Product2 - P66779)");       DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Home Decor (Product3 - ... Read More

Can I get the node at a specified index in a JTree with Java?

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

312 Views

To get the node at a specified index in a JTree, use the getChildAt() method. Here, we are finding the node at index 3 i.e. 4th node −node.getChildAt(3)The following is an example to get the node at a specified index 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 (Product2 ... Read More

How to get this nodes’s parent in a JTree with Java?

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

596 Views

Let’s say we want the parent of a node, then use the getParent() method -node3.getFirstChild()You can also get the parent of child node. Here, “nine” is the child node −nine.getParent()The output is as follows displaying this node’s parent on Console −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 (Product2 - P66779)");   ... Read More

How to set the alignment of the JLabel content along the Y axis in Java?

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

395 Views

To set the alignment of the label content along the Y axis, use the setVerticalAlignment() method. Let us first set a label component. We have set the label background color as well so that we can check the alignment of the label’s content properly −JLabel label = new JLabel("Product Name "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.ORANGE);Now, we will align the label content along the Y axis −label.setVerticalAlignment(JLabel.CENTER);The following is an example to set the alignment of the JLabel content along the Y axis −Examplepackage 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 {    public static void main(String[] args) {       JFrame frame = new JFrame("Our Frame ... Read More

How to set the alignment of the JLabel content along the X axis in Java?

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

580 Views

To set the alignment of the label content along the X axis, use the setHorizontalAlignment() method. Let us first set a label component. We have set the label background color as well so that we can check the alignment of the label’s content properly −JLabel label = new JLabel("Team "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.BLUE); label.setForeground(Color.WHITE);Now, we will align the label content along the X axis −label.setHorizontalAlignment(JLabel.CENTER);The following is an example to set the alignment of the JLabel content along the X axis −Examplepackage 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 ... Read More

How to create a JLabel with an image icon in Java?

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

5K+ Views

Let us create a label with image icon −JLabel label = new JLabel("SUBJECT "); label.setIcon(new ImageIcon("E:ew.png"));Now, create another component −JTextArea text = new JTextArea(); text.setText("Add subject here...");Align the components with GridBagLayout −panel.setLayout(new GridBagLayout());The following is an example to center a label with an image icon −Examplepackage my; import java.awt.GridBagLayout; import javax.swing.BorderFactory; import javax.swing.ImageIcon; 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();       JLabel label = new JLabel("SUBJECT "); ... Read More

Advertisements