Found 7442 Articles for Java

How to add a Tab in JTabbedPane with Java?

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

1K+ Views

Let us first create a JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Create some panels −JPanel panel1, panel2, panel3, panel4, panel5; panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); panel5 = new JPanel();Now, add tabs using the addTab() method and set the panels as well in which the tabs would be visible −tabbedPane.addTab("PHP", panel1); tabbedPane.addTab("Blockchain ", panel2); tabbedPane.addTab("Matlab", panel3); tabbedPane.addTab("JSP ", panel4); tabbedPane.addTab("Servlet", panel5);The following is an example to add a tab in JTabbedPane −package my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       ... Read More

How to set font face, style, size and color for JTextPane text in Java

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

1K+ Views

For background and foreground color of the JTextPane, use the following −JTextPane textPane = new JTextPane(); textPane.setBackground(Color.blue); textPane.setBackground(Color.green);For font face, style and size, use the Font class and set the font −Font font = new Font("Serif", Font.ITALIC, 18); textPane.setFont(font);The following is an example to set font, style and color for text in JTextPane −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; public class SwingDemo {    public static void main(String args[]) throws BadLocationException {       JFrame frame = new JFrame("Demo"); ... Read More

Get the number of siblings of a node in JTree with Java

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

252 Views

Use the getSiblingCount() method to get the number of siblings of a node in JTree. For example, let’s say we have a node, which isn’t a root node. For that, we will find the sibling count −node1.getSiblingCount()The following is an example to get the number of siblings of a node −package 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("Project");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("QA");       DefaultMutableTreeNode node2 ... Read More

Get the number of levels above a node in JTree with Java

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

325 Views

To get the number of levels above a node, use the getLevel() method. Following is an example for the root node “node” −node.getLevel()Note − The value 0 is returned if the node is a root node since there are zero levels above the root node.For other nodes, get the number of levels above a node as shown below with node3 −node3.getLevel()The following is an example to get the number of levels above a node in a JTree −package 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 {     ... Read More

How to use GridBagConstraints to layout two components in the same line with Java

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

323 Views

To align two components in the same line, you need to set the contrainsts of the GridBagConstraints properly. Let’s say we have two components in panel1. Set the contraints using Insets as well −panel1.add(checkBox1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); panel1.add(checkBox2, new GridBagConstraints(1, 0, 1, 1, 2.0, 0.0,    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));The following is an example to set two components in the same line −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {   ... Read More

How to set Palette Layer for JDesktopPane in Java?

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

216 Views

At first, create a JDesktopPane −JDesktopPane desktopPane = new JDesktopPane();Now, create an Internal Frame −JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true); intFrame.setBounds(50, 90, 200, 250);Set Pallette layer for JDesktopPane and add the Internal Frame to JDesktopPane −intFrame.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); desktopPane.add(intFrame, JDesktopPane.PALETTE_LAYER);The following is an example to set palette layer for JDesktopPane −package my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(final String[] args) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JDesktopPane desktopPane = new JDesktopPane();   ... Read More

Display multiple components in an Internal Frame in Java

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

440 Views

At first, set an Internal Frame in the JDesktopPane −JDesktopPane desktopPane = new JDesktopPane(); JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true); desktopPane.add(intFrame);Now, set the bounds of the Internal Frame −intFrame.setBounds(50, 90, 200, 250);Create two components −JLabel label = new JLabel(intFrame.getTitle(), JLabel.CENTER); JButton button = new JButton("Demo Button");Add the two components to the Internal Frame −intFrame.add(label, BorderLayout.CENTER); intFrame.add(button, BorderLayout.WEST);The following is an example to display multiple components in an internal frame −package my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(final String[] args) {   ... Read More

How to position component at the top of the grid cell with GridBagLayout in Java?

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

664 Views

To position components at the top fo the grid cell, set the GridBagConstraints. Let’s say we have a panel and within that two CheckBox components −JPanel panel1 = new JPanel(new GridBagLayout()); JCheckBox checkBox1 = new JCheckBox("Undergraduate"); JCheckBox checkBox2 = new JCheckBox("Graduate");Position the components now −panel1.add(checkBox1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,    GridBagConstraints.NORTH, GridBagConstraints.NORTH, new Insets(0, 0, 0, 0), 0, 0)); panel1.add(checkBox2, new GridBagConstraints(1, 0, 1, 1, 2.0, 0.0,    GridBagConstraints.NORTH, GridBagConstraints.NORTH, new Insets(0, 0, 0, 0), 0, 0));The following is an example to position component at the top of the grid cell with GridBagLayout −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; ... Read More

How to add Internal Frame to a JDesktopPane in Java?

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

678 Views

At first, create a JDesktoPane −JDesktopPane desktopPane = new JDesktopPane();Now, create an Internal frame and add it to the JDesktopPane −JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true); desktopPane.add(intFrame); The following is an example to add Internal Frame to a JDesktopPane − package my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(final String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JDesktopPane desktopPane = new JDesktopPane();       JInternalFrame intFrame = new JInternalFrame("Our Frame", true, ... Read More

How to center a JLabel in a JPanel with GridBagLayout in Java?

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

934 Views

Center a component in a JPanel with GridBagLayout. Let us first create a JFrame and JPanel inside it -JFrame frame = new JFrame("Demo Frame"); JPanel panel = new JPanel();Now, we will add our components −JLabel label = new JLabel("Demo Label (Centered)"); label.setForeground(Color.white); JCheckBox checkBox = new JCheckBox("Checkbox (Centered)");Set the layout −panel.setLayout(new GridBagLayout());The following is an example to center a label in a panel with GridBagLayout −Examplepackage my; import java.awt.Color; import java.awt.GridBagLayout; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Demo ... Read More

Advertisements