Found 9150 Articles for Object Oriented Programming

Java Program to add Combo Box to JTable

Chandu yadav
Updated on 19-Aug-2024 18:30:53

2K+ Views

In this article, we will learn how to add a JComboBox to a JTable in Java Swing. The JComboBox allows you to create a drop-down list within a table cell, enabling users to select from predefined options. Steps to add a combo box to JTable Following are the steps to add a combo box to JTable − First import the necessary packages. Initialize a JTable with 5 rows and 5 columns. Create a JComboBox and add items to it. Get the first column of ... Read More

How to prevent resizing columns in a JTable

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

1K+ Views

To prevent resizing columns, use the method setResizingAllowed(). Here, we will set setResizingAllowed() to false for table header to disallow resizing of columns from header −table.getTableHeader().setResizingAllowed(false);Let us first see an example wherein we can easily resize columns in a table by resizing the table column 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.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");       tableModel.addColumn("Text Tutorial");   ... Read More

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

203 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

How can circular references cause memory leakage in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

536 Views

Circular reference A circular reference is formed when two variables reference to each other there by giving each object a reference count of 1.In pure garbage collected system, a circular reference may not be a problem when the variables involved were have no references.In that scenario the declared variables will be garbage collected.In reference counting system neither of the objects will be destroyed, because the reference count cannot be zero.In hybrid system, where reference counting and garbage collection are used, memory leaks will occur because the system fails to identify circular references.ExampleThe following example shows a circular reference between javascript object ... Read More

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

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

740 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

349 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

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

How to allow contigous selection of nodes in a JTree?

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

156 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

Advertisements