Java Articles - Page 339 of 745

How to highlight a row in a table with Java Swing?

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

1K+ Views

To highlight a row in a table, you can use the addRowSelectionInterval() method. At first create a table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);Add some columns −tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Views");Now, add rows to the table −tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"});Highlight a single row by adding interval of rows. Set the same index for both the parameters ... Read More

Java program to lay out components in a flow to be centered with FlowLayout?

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

219 Views

Use FlowLayout.CENTER to lay out components in a flow to be centered with FlowLayout. The following is an example to lay out components in a flow to be centered with FlowLayout −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("WorldCup2019");       frame.setLayout(new FlowLayout(FlowLayout.CENTER));       JLabel label = new JLabel("WorldCup Hosting Country ");       label.setPreferredSize(new Dimension(220, 70));       label.setOpaque(true);       label.setBackground(Color.ORANGE);     ... Read More

Can we hide the table header from a JTable in Java?

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

759 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

How to change JTable's header font in Java

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

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

How can I separate Menu Items in a Menu with Java?

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

362 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

Java Program to customize MenuBar and change the background color

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

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

How to allow contigous selection of nodes in a JTree?

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

168 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

Disallow resizing a column by dragging between headers in a JTable Component with Java

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

189 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

How to allow only a single tree node to be selected in a JTree with Java?

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

611 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

What are the differences between printStackTrace() method and getMessage() method in Java?

Shriansh Kumar
Updated on 19-May-2025 19:36:14

5K+ Views

The errors that occur during runtime after compilation are called exceptions. To handle and rectify these errors, it is necessary to understand the cause and the point where the error occurs. There are two ways to find the details of the exception: one is the printStackTrace() method, and another is the getMessage() method. Both are used for exception handling in Java, but they are different from each other. Let's discuss how. The printStackTrace() Method The printStackTrace() method is defined in Throwable class of java.lang package and it is inherited into both java.lang.Error class and java.lang.Exception class. When you use it ... Read More

Advertisements