Found 112 Articles for AWT

How to display "No records available" text in a JTable in Java?

raja
Updated on 12-Feb-2020 07:15:04

282 Views

A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface.In the below program, we can display "No records available" text if the rows are not available in a JTable.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class NoRecordTableTest extends JFrame {    private JPanel panel;    private JTable table;    private JScrollPane scrollPane;    public NoRecordTableTest() {       panel = new JPanel();       panel.setLayout(new BorderLayout()); ... Read More

How can we disable the leaf of JTree in Java?

raja
Updated on 03-Jul-2020 05:46:15

233 Views

A JTree is a component that presents a hierarchical view of data. The user has the ability to expand or collapse individual sub-trees. A TreeNode interface defines the methods that must be implemented nodes of a JTree object. The DefaulMutableTreeNode class provides a default implementation of a TreeNode interface. We can disable the leaf of JTree by overriding the getTreeCellRendererComponent() method of DefaultTreeCellRenderer class.Syntaxpublic Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)Exampleimport java.awt.*; import javax.swing.tree.*; import javax.swing.*; public class JTreeLeafNodeDisableTest extends JFrame {    private TreeNode treeNode;    private JTree tree;    public JTreeLeafNodeDisableTest() {       setTitle("JTreeLeafNodeDisable Test");     ... Read More

How to implement the mouse right-click on each node of JTree in Java?

raja
Updated on 12-Feb-2020 07:26:46

525 Views

A JTree is a subclass of JComponent class that can be used to display the data with the hierarchical properties by adding nodes to nodes and keeps the concept of parent and child node. Each element in the tree becomes a node. The nodes are expandable and collapsible. We can implement the mouse right-click on each node of a JTree using the mouseReleased() method of MouseAdapter class and need to call show() method of JPopupMenu class to show the popup menu on the tree node.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; public class JTreeRightClickTest extends JFrame {    public JTreeRightClickTest() {       ... Read More

How can we remove a selected row from a JTable in Java?

raja
Updated on 02-Jul-2020 13:12:48

6K+ Views

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable component can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces. We can remove a selected row from a JTable using the removeRow() method of the DefaultTableModel class.Syntaxpublic void removeRow(int row)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class RemoveSelectedRowTest extends JFrame {    private JTable table;    private DefaultTableModel model;    private Object[][] data;    private String[] columnNames;    private JButton button;    public RemoveSelectedRowTest() {       setTitle("RemoveSelectedRow ... Read More

How can we add different font style items to JList in Java?

raja
Updated on 12-Feb-2020 06:54:41

501 Views

A JList is a subclass of JComponent class and it can be used to display a list of objects that allows the user to select one or more items. A JList can generate a ListSelectiionListener interface and need to implement the abstract method valueChanged(). A DefaultListModel class provides a simple implementation of a list model, which can be used to manage items displayed by a JList control. We can add the items to a JList using the addElement() method of the DefaultListModel class, we can also add items with different fonts to JList using HTML tags like for bold style text,   for italic style ... Read More

How can we implement auto-complete JComboBox in Java?

raja
Updated on 12-Feb-2020 06:34:12

1K+ Views

A JComboBox is a subclass of JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener, and ItemListener interfaces when the user actions on a combo box.We can implement auto-complete JComboBox when the user types an input value from a keyboard by using customization of a combo box (AutoCompleteComboBox) by extending the JComboBox class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.basic.*; public class AutoCompleteComboBoxTest extends JFrame {    private JComboBox comboBox;    public AutoCompleteComboBoxTest() {       setTitle("AutoCompleteComboBox");     ... Read More

How can we implement transparent JDialog in Java?

raja
Updated on 12-Feb-2020 06:36:45

285 Views

A JDialog is a subclass of Dialog class and it does not hold minimize and maximize buttons at the top right corner of the window. There are two types of dialog boxes namely, modal and non-modal. The default layout for a dialog box is BorderLayout.In the below program, we can implement a transparent JDialog by customizing the AlphaContainer class and override the paintComponent() method.Exampleimport java.awt.*; import javax.swing.*; public class TransparentDialog {    public static void main (String[] args) {       JDialog dialog = new JDialog();       dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       dialog.getRootPane().setOpaque(false);       dialog.setUndecorated(true);       dialog.setBackground(new Color (0, 0, ... Read More

How can we call the invokeLater() method in Java?

raja
Updated on 02-Jul-2020 08:12:57

2K+ Views

An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread. The SwingUtilities.invokeLater() method works like SwingUtilities.invokeAndWait() except that it puts the request on the event queue and returns immediately. An invokeLater() method does not wait for the block of code inside the Runnable referred by a target to execute.Syntaxpublic static void invokeLater(Runnable target)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class InvokeLaterTest extends Object {    private static void print(String msg) {       String name = Thread.currentThread().getName();       System.out.println(name + ": " + msg);    }   ... Read More

How to implement a rollover effect for a JButton in Java?

raja
Updated on 02-Jul-2020 07:04:18

831 Views

A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a GUI application. A JButon can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. We can implement the rollover effect when the mouse moves over a JButton by overriding the mouseEntered() method of the MouseListener interface.Syntaxvoid mouseEntered(MouseEvent e)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class RollOverButtonTest extends JFrame {    private JButton button;    public RollOverButtonTest() {       setTitle("RollOverButton Test");       button = new JButton("Rollover Button");       button.addMouseListener(new MouseAdapter() {     ... Read More

How to set the shortcut key to a JCheckBox in Java?

raja
Updated on 10-Feb-2020 12:26:50

162 Views

A JCheckBox is a subclass of JToggleButton and it can be a small box that is either checked or unchecked. When we click on a JCheckBox, it changes from checked to unchecked or vice versa automatically. A JCheckBox can generate an ActionListener or ItemListener whenever the checkbox is changed. We can set the shortcut keys to a JCheckBox by using the setMnemonic() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JCheckBoxShortCutKeyTest extends JFrame {    private JCheckBox checkBox;    public JCheckBoxShortCutKeyTest() {       setTitle("JCheckBoxShortCutKey Test");       checkBox = new JCheckBox("Check or Press ALT-C");       checkBox.setBorder(BorderFactory.createLineBorder(Color.lightGray));       checkBox.setMnemonic('C');     ... Read More

Advertisements