Found 133 Articles for Swing

How can we implement editable JComboBox in Java?

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

967 Views

JComboBoxA JComboBox can extend JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value.If the text field portion of the control is editable, the user can enter a value in the field or edit a value retrieved from the drop-down list.By default, the user not allowed to edit the data in the text field portion of the JComboBox. If we want to allow the user to edit the text field, call setEditable(true) method.A JComboBox can generate an ActionListener, ChangeListener or ItemListener when the user actions on a combo box.A ... Read More

How can we implement a scrollable JPanel in Java?

raja
Updated on 10-Feb-2020 06:35:13

3K+ Views

JPanelA JPanel is a subclass of JComponent (a subclass of a Container class). Therefore, JPanel is also a Container.A JPanel is an empty area that can be used either to layout other components including other panels.In a JPanel, we can add fields, labels, buttons, checkboxes,  and images also.The Layout Managers such as FlowLayout, GridLayout, BorderLayout and other layout managers helps us to control the sizes, positions, and alignment of the components using JPanel.The important methods of a JPanel class are getAccessibleContext(), getUI(), updateUI() and paramString().We can also implement a JPanel with vertical and horizontal scrolls by adding the panel object to JScrollPane.Exampleimport java.awt.*; ... Read More

How to select one item at a time from JCheckBox in Java?

raja
Updated on 07-Feb-2020 11:41:15

1K+ Views

JCheckBoxA JCheckBox can extend 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 an ItemListener whenever the checkbox is changed.An isSelected() method is used to test if a checkbox is checked or not.By default, we can select all the checkbox items at a time, if we want to select only one item at a time by using ButtonGroup class.Exampleimport javax.swing.*; import java.awt.*; import java.awt.event.*; public class JCheckBoxGroupTest extends JFrame {    private ButtonGroup checkBoxGroup;   ... Read More

How can we add new tabs to JTabbedPane from a JMenu in Java?

raja
Updated on 07-Feb-2020 11:42:24

241 Views

JTabbedPaneA JTabbedPane is a component can extend JComponent class and it can provide easy access to more than one panel.Each tab is associated with a single component that can be displayed when the tab is selected.A JTabbedPane can generate a ChangeListener interface when a tab is selected.The important methods of JTabbedPane are add(), addTab(), fireStateChanged(), createChangeListener(), setSelectedIndex(), getTabCount() and etc.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JTabbedPaneTest extends JFrame implements ActionListener {    JTabbedPane tabbedPane;    int ntabs = 0;    public JTabbedPaneTest() {       getContentPane().setLayout(new BorderLayout());       tabbedPane = new JTabbedPane();   ... Read More

How can we implement right click menu using JPopupMenu in Java?

raja
Updated on 07-Feb-2020 11:44:00

1K+ Views

A JPopupMenu appears anywhere on the screen when a right mouse button is clicked.JPopupMenuA popup menu is a free-floating menu which associates with an underlying component called the invoker.Most of the time, a popup menu is linked to a specific component to display context-sensitive choices.In order to create a popup menu, we can use the JPopupMenu class., we can add the JMenuItem to popup menu like a normal menu.To display the popup menu, we can call the show() method, normally popup menu is called in response to a mouse event.Exampleimport java.awt.event.*; import java.awt.*; import javax.swing.*; public class JPopupMenuTest extends JFrame { ... Read More

What are the differences between a JTextPane and a JEditorPane in Java?

raja
Updated on 07-Feb-2020 11:46:23

1K+ Views

A JTextPane is an extension of JEditorPane which provides word processing features like fonts, text styles, colors and etc. If we need to do heavy-duty text processing we can use this class whereas a JEditorPane supports display/editing of HTML and RTF content and can be extended by creating our own EditorKit.JTextPaneA JTextPane is a subclass of JEditorPane.A JTextPane is used for a styled document with embedded images and components.A JTextPane is a text component that can be marked up with the attributes that are represented graphically and it can use a DefaultStyledDocument as the default model.The important methods of JTextPane are addStyle(), getCharacterAttributes(), getStyledDocument(), setDocument(), setEditorKit(), setStyledDocument() and etc.Exampleimport java.awt.*; import ... Read More

What is the importance of a WindowListener interface in Java?

raja
Updated on 30-Jun-2020 13:32:30

192 Views

The class which process the WindowEvent needs to be implemented this interface and an object of this class can be registered with a component by using addWindowListener() method.Methods of WindowListener InterfaceThe WindowListener interface defines 7 methods for handling window eventsvoid windowActivated(WindowEvent we) − Invoked when a window is activated.void windowDeactivated(WindowEvent we) − Invoked when a window is deactivated.void windowOpened(WindowEvent we) − Invoked when a window is opened.void windowClosed(WindowEvent we) − Invoked when a window is closed.void windowClosing(WindowEvent we) − Invoked when a window is closing.void windowIconified(WindowEvent we) − Invoked when a window minimized.void windowDeiconfied(WindowEvent we) − Invoked when a window is restored.Syntaxpublic ... Read More

What is a LayoutManager and types of LayoutManager in Java?

raja
Updated on 19-Feb-2024 04:20:24

29K+ Views

The Layout managers enable us to control the way in which visual components are arranged in the GUI forms by determining the size and position of components within the containers. Types of LayoutManager There are 6 layout managers in Java FlowLayout: It arranges the components in a container like the words on a page. It fills the top line from left to right and top to bottom. The components are arranged in the order as they are added i.e. first components appears at top left, if the container is not wide enough to display all the components, ... Read More

What is the use of setBounds() method in Java?

raja
Updated on 13-Sep-2023 03:56:20

32K+ Views

The layout managers are used to automatically decide the position and size of the added components. In the absence of a layout manager, the position and size of the components have to be set manually. The setBounds() method is used in such a situation to set the position and size. To specify the position and size of the components manually, the layout manager of the frame can be null.setBounds()The setBounds() method needs four arguments. The first two arguments are x and y coordinates of the top-left corner of the component, the third argument is the width of the component and the fourth argument is the ... Read More

What are the differences between an event listener interface and an event adapter class in Java?

raja
Updated on 07-Feb-2020 12:47:57

4K+ Views

An EventListener interface defines the methods that must be implemented by an event handler for a particular kind of an event whereas an Event Adapter class provides a default implementation of an EventListener interface.Event ListenerThe Event Listeners are the backbone of every component to handle the events.Every method of a particular EventListener will have a single parameter as an instance which is the subclass of EventObject class.An EventListener interface needs to be extended and it will be defined in java.util package.A few EventListener interfaces are ActionListener, KeyListener, MouseListener, FocusListener, ItemListener and etc.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyListenerTest implements KeyListener, ActionListener {    JFrame frame;    JTextField tf;   ... Read More

Previous 1 ... 7 8 9 10 11 ... 14 Next
Advertisements