Found 133 Articles for Swing

How can we show/hide the table header of a JTable in Java?

raja
Updated on 10-Feb-2020 07:44:45

2K+ Views

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. The DefaultTableModel class is a subclass of AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically. The DefaultTableCellRenderer class can extend JLabel class and it can be used to add images, colored text and etc. inside the JTable cell. We can hide the table header of a JTable by unchecking the JCheckBox and show the table header of a JTable by clicking the JCheckBox.Exampleimport java.awt.*; import javax.swing.*; import javax.swing.table.*; public final class JTableHeaderHideTest extends ... Read More

How to display the different font items inside a JComboBox in Java?

raja
Updated on 10-Feb-2020 07:46:53

299 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 display the different font styles inside a JComboBox by implementing the ListCellRenderer interfaceExampleimport java.awt.*; import javax.swing.*; public class JComboBoxFontTest extends JFrame {    private JComboBox fontComboBox;    private String fontName[];    private Integer array[];    public JComboBoxFontTest() {       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();       fontName = ge.getAvailableFontFamilyNames();       ... Read More

How to set a tooltip text for each item of a JList in Java?

raja
Updated on 10-Feb-2020 07:10:41

385 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 JToolTip class is used to display a text or a tip of the component, we can set a tooltip text for each item of a list by implementing the getToolTipText() method of JToolTip class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class JListTooltipTest extends JFrame {    private Vector vector;    public JListTooltipTest() {       setTitle("JListTooltip Test");   ... Read More

What is the importance of a FocusListener interface in Java?

raja
Updated on 01-Jul-2020 10:10:12

481 Views

FocusListenerThe focus events are generated whenever a component gains or loses the keyboard focus.The Objects representing focus events are created from FocusEvent Class.The corresponding listener interface for FocusEvent class is a FocusListener interface. Each listener for FocusEvent can implement the FocusListener interface.The FocusListener interface contains two methods focusGained(): Called by the AWT just after the listened-to component gets the focus and focusLost(): Called by the AWT just after the listened-to component loses the focus.Syntaxpublic interface FocusListener extends EventListener {    public void focusGained(FocusEvent fe);    public void focusLost(FocusEvent fe);Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class FocusListenerTest extends JPanel implements FocusListener {    private JTextField textField; ... Read More

How can we sort the items of a JComboBox in Java?

raja
Updated on 10-Feb-2020 07:15:16

674 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. By default, a JComboBox does not support for sorting the items, we can customize the code by extending the DefaultComboBoxModel class.Exampleimport java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class JComboBoxSorterTest extends JFrame {    private JComboBox comboBox;    private JTextField textField;    public JComboBoxSorterTest() {       setTitle("JComboBoxSorter Test");       setLayout(new FlowLayout());       ... Read More

How to center align the items of a JComboBox in Java?

raja
Updated on 10-Feb-2020 07:16:41

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 an ItemListener when the user actions on a combo box. By default, items in the JCombobox are left-aligned, we can also change to center alignment by using the setHorizontalAlignment(DefaultListCellRenderer.CENTER) method of DefaultListCellRenderer class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JComboBoxAlignmentTest extends JFrame {    private JComboBox comboBox;    private DefaultListCellRenderer listRenderer;    public JComboBoxAlignmentTest() {       setTitle("JComboBoxAlignment Test");       setLayout(new FlowLayout());       ... Read More

How can we display the line numbers inside a JTextArea in Java?

raja
Updated on 10-Feb-2020 07:18:54

635 Views

A JTextArea is a subclass of JTextComponent and it is a multi-line text component to display the text or allow the user to enter a text. A JTextArea can generate a CaretListener interface, which can listen to caret update events. By default, JTextArea does not display the line numbers, we have to customize the code by using a DocumentListener interface.Exampleimport java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.Element; public class LineNumberTextAreaTest extends JFrame {    private static JTextArea textArea;    private static JTextArea lines;    private JScrollPane jsp;    public LineNumberTextAreaTest() {       setTitle("LineNumberTextArea Test");       jsp = new JScrollPane();     ... Read More

How can we align the JRadioButtons horizontally in Java?

raja
Updated on 10-Feb-2020 07:21:11

526 Views

A JRadioButton is a subclass of JToggleButton and it is a two-state button that can either selected or deselected. Unlike checkboxes, the radio buttons are associated with a group and only one radio button in a group can be selected and it can be implemented by using the ButtonGroup class. When the radio button in the group is selected, any other previously selected radio button in the group is deselected. We can align the radio buttons either horizontally or vertically by using BoxLayout.Exampleimport java.awt.*; import javax.swing.*; public class HorizontalRadioButtonsTest extends JPanel {    public HorizontalRadioButtonsTest(){       JRadioButton jrb1 = new JRadioButton(" RB1"); ... Read More

How can we add/insert a JCheckBox inside a JTable cell in Java?

raja
Updated on 10-Feb-2020 07:22:40

3K+ 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. We can add or insert a checkbox inside a JTable cell by implementing the getColumnClass() method of a Class type.Exampleimport java.awt.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; public class JCheckBoxJTableTest extends JFrame {    private JTable table;    private DefaultTableModel model;    public JCheckBoxJTableTest() {       Random rnd = new Random();       model = new DefaultTableModel(new Object[]{"Check Box1", ... Read More

What are the differences between the TableCellRenderer and TableCellEditor in Java?

raja
Updated on 10-Feb-2020 07:26:26

922 Views

TableCellRendererA TableCellRenderer creates a component that displays the value of a JTable cell.The default renderer uses JLabel to display the value of each table cell.The TableCellRenderer interface can be specified in two ways : By class of the object to be rendered using table.setDefaultRenderer() method and by a column using tableColumn.setCellRenderer() method and tableColumn.setHeaderRenderer() method for a specific column headers.The TableCellRenderer interface has only one method getTableCellRendererComponent() and this method can return different rendering components based on the value, a cell has the focus or is selected, row and column that can contain the value.TableCellEditorA TableCellEditor is an interface and by default, the cells can be editable.A TableCellEditor can be ... Read More

Previous 1 ... 4 5 6 7 8 ... 14 Next
Advertisements