Found 4338 Articles for Java 8

Set whether the cell in the table model can be selected or deselected in Java?

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

36 Views

We can set or disallow selection of cell in the table using setCellSelectionEnabled(). The following is an example. −If you want to allow selection of cell, then set the method to TRUE −table.setCellSelectionEnabled(true);If you want to disallow selection of cell, then set the method to FALSE −table.setCellSelectionEnabled(false);Here we have disallowed selection of a cell −Examplepackage my; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       tableModel.addColumn("Language/ Technology");     ... Read More

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

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

245 Views

Let’s say we want the first child node of a node, then use the getFirstChild() method −node2.getFirstChild()Display the node’s first child on Console −System.out.println("The first child of node 2 = "+node2.getFirstChild());The following is an example to get this node’s first child 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 {       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 create a date spinner in Java?

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

1K+ Views

To create a date spinner, use the SpinnerDateModel class. Within that set the date format −Date today = new Date(); JSpinner spinner2 = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH)); JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner2, "dd/MM/yy"); spinner2.setEditor(editor);Above, we have set the Date format to be −dd/MM/yyThe following is an example to create a date spinner in Java −Examplepackage my; import java.awt.GridBagLayout; import java.util.Calendar; import java.util.Date; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Spinner Demo");       JPanel panel = new JPanel();       JLabel label = new JLabel("Exam No.");       JLabel label2 = new JLabel(" Appeared On");       panel.setLayout(new GridBagLayout());       int min = 0;       int max = 10;       int step = 1; ... Read More

How to check if two nodes are equal in a JTree with Java?

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

626 Views

To check if two nodes are equal, use the equals() method. Here, we are checking that node1 and node2 are equal or not.node1.equals(node2)The following is an example to check if two nodes are equal 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 {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Website");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Videos");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Tutorials");       DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("QA");     ... Read More

How to set TabLayout Policy to JTabbedPane in Java when all the tabs does not fit in a single run

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

261 Views

To set TabLayout Policy to JTabbedPane in Java when all the tabs does not fit in a single run, use the method setTabLayoutPolicy() −JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);Above we have set the constant to be SCROLL_TAB_LAYOUT, which is a tab layout policy for providing a subset of available tabs when all the tabs will not fit within a single run.The following is an example −package my; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Devices");       JTabbedPane tabbedPane = new JTabbedPane(); ... Read More

How to change the background and foreground colors of tab in Java?

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

612 Views

To change the background and foreground colors of tab, use the following methods −JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setBackground(Color.blue); tabbedPane.setForeground(Color.white);Above, we have used the Color class to set the colors for the background and foregroud colors −The following is an example to change the background and foreground colors of tab −package my; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Devices");       JTabbedPane tabbedPane = new JTabbedPane();       JPanel panel1, panel2, panel3, panel4, panel5;       panel1 = new ... Read More

How to specify tab location to make it visible in the bottom with Java?

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

110 Views

Use the setTabPlacement() method to set the tab location. To make it visible in the bottom, use the BOTTOM constant −JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);The following is an example to specify tab location to make it visible in the bottom −package my; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Technologies");       JTabbedPane tabbedPane = new JTabbedPane();       JPanel panel1, panel2, panel3, panel4, panel5;       panel1 = new JPanel();       panel2 = new JPanel();   ... Read More

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

957 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

139 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

Advertisements