- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4335 Articles for Java 8
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
542 Views
Yes, we can hide the header from a table. Use the setTableHeader() method and set it to null -table.setTableHeader(null);Above, the table is our JTable -JTable table = new JTable(marks, col)The following is an example to hide the table header -Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.JTableHeader; public class SwingDemo { public static void main(String[] argv) throws Exception { Integer[][] marks = { { 70, 66, 76, 89, 67, 98 }, { 67, 89, 64, 78, 59, 78 }, ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
1K+ Views
To change the table header font, you need to first get the header -JTableHeader tableHeader = table.getTableHeader();Now, use the Font class to set the new font. Here, we have set the font face to be Verdana, style as PLAIN and font size as 14 -Font headerFont = new Font("Verdana", Font.PLAIN, 14);Now, set this font to table header -tableHeader.setFont(headerFont);The following is an example to change the header font -Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.JTableHeader; public class SwingDemo { public static void main(String[] argv) throws Exception { Integer[][] marks = ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
253 Views
Let’s say we have following MenuBar and menu in it -JMenuBar menuBar = new JMenuBar(); UIManager.put("MenuBar.background", Color.ORANGE); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu);Now, we will create a MenuItem and separate it using the JSeparator():JMenuItem menuItem1 = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(menuItem1); // separating MenuItems fileMenu.add(new JSeparator());The following is an example to separate Menu Items in a Menu with Java -Examplepackage my; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JSeparator; import javax.swing.UIManager; public class SwingDemo { public static void main(final String args[]) { JFrame frame = new JFrame("MenuBar Demo"); ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
1K+ Views
Use the UIManager to customize the MenuBar:JMenuBar menuBar = new JMenuBar(); UIManager.put("MenuBar.background", Color.ORANGE);We have used the following above to update the background color of the MenuBar:UIManager.put("MenuBar.background", Color.ORANGE);The following is an example to customize MenuBar and change the background color:package my; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.UIManager; public class SwingDemo { public static void main(final String args[]) { JFrame frame = new JFrame("MenuBar Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); UIManager.put("MenuBar.background", Color.ORANGE); JMenu fileMenu = new JMenu("File"); ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
92 Views
Contigous selection means sharing borders like selecting siblings of a node in a JTree. To allow contiguous selection of nodes, set the selection mode to CONTIGUOUS_TREE_SELECTION −tree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);The following is an example to allow contigous selection of nodes in a JTree −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; 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"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Electronics"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Home Decor"); DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Furniture"); node.add(node1); node.add(node2); node.add(node3); node.add(node4); DefaultMutableTreeNode one = new DefaultMutableTreeNode("Shirt"); DefaultMutableTreeNode two = new DefaultMutableTreeNode("Trousers"); ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
82 Views
Let us first create a table with rows and columns −String data[][] = { {"Australia", "5", "1"}, {"US", "10", "2"}, {"Canada", "9", "3"}, {"India", "7", "4"}, {"Poland", "2", "5"}, {"SriLanka", "5", "6"} }; String col [] = {"Team", "Selected Players", "Rank"}; DefaultTableModel tableModel = new DefaultTableModel(data, col); JTable table = new JTable(tableModel);Now, we will disable resizing of columns by dragging headers −table.getTableHeader().setResizingAllowed(false);The following is an example to disallow resizing a column by dragging between headers in a JTable: −Examplepackage my; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
366 Views
Set the selection mode to SINGLE_TREE_SELECTION, if you want only a single tree node to be selected −tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);The following is an example to allow only a single tree node to be selected in a JTree −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; 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"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Electronics"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Home Decor"); ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
90 Views
Use FlowLayout.RIGHT to arrange components in a FlowLayout to be right-justified. −JFrame frame = new JFrame("Language"); frame.setLayout(new FlowLayout(FlowLayout.RIGHT));The following is an example to arrange components in a flow to be right-justified −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("Language"); frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); JLabel label = new JLabel("Most Spoken Language "); label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.RED); ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
2K+ Views
Here, we are using the LayoutManager GridBagLayout to center the components. We have two components here including a label and we have set the layout as GridBagLayout −JLabel label = new JLabel("Name (Centered Label): "); JTextArea text = new JTextArea(); text.setText("Add name here..."); panel.setLayout(new GridBagLayout());The following is an example to center a JLabel in a JPanel with LayoutManager −Examplepackage my; import java.awt.GridBagLayout; import javax.swing.BorderFactory; 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 = ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
599 Views
Use CardLayout layout and set it to panel −JPanel panel = new JPanel(); CardLayout cardLayout = new CardLayout(); panel.setLayout(cardLayout);In the same way create 5 panels and 5 buttons to display 5 different cards.The following is an example to display 5 different cards in CardLayout −Examplepackage my; import java.awt.BorderLayout; import java.awt.CardLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(550, 300); JPanel panel = new JPanel(); JPanel panel1 = new JPanel(); ... Read More