Found 112 Articles for AWT

How can we hide left/right pane of a JSplitPane programmatically in Java?

raja
Updated on 10-Feb-2020 11:28:46

307 Views

A JSplitPane is a subclass of JComponent class that allows us to arrange two components side by side horizontally or vertically in a single pane. The display areas of both components can also be adjusted at the runtime by the user. The important methods of JSplitPane are remove(), removeAll(), resetToPreferredSizes() and setDividerLocation(). A JSplitPane can generate a PropertyChangeListener interface. We can hide one of the panes (left or right) programmatically by click on the left button or right button and can generate action listeners for those buttons.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JSplitPaneHideTest extends JFrame {    private JButton leftBtn, rightBtn;    private JSplitPane ... Read More

How can we add/insert a JButton to JTable cell in Java?

raja
Updated on 10-Feb-2020 11:10:47

6K+ 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 JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableButtonTest extends JFrame {    private JTable table;    private JScrollPane scrollPane;    public JTableButtonTest() {     ... Read More

How to read an input value from a JTextField and add to a JList in Java?

raja
Updated on 10-Feb-2020 08:06:49

973 Views

A JList is a subclass of JComponent class that allows the user to choose either a single selection or multiple selections. A JList class itself does not support scrollbar. In order to add scrollbar, we have to use JScrollPane class together with the JList class. The JScrollPane then manages a scrollbar automatically. 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 items or elements to a JList by using addElement() method of DefaultListModel class. We can also add items or elements to a JList by reading an ... Read More

How can we set the margin to a JButton in Java?

raja
Updated on 10-Feb-2020 08:08:54

3K+ Views

A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a Java Swing 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 set a margin to a JButton by using the setMargin() method of JButton class and pass Insets(int top, int left, int bottom, int right) as an argument.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JButtonMarginTest extends JFrame {    private JButton button;    public JButtonMarginTest() {       setTitle("JButtonMargin Test");       setLayout(new BorderLayout());       button ... Read More

How to display a value when select a JList item in Java?

raja
Updated on 10-Feb-2020 10:04:57

1K+ Views

A JList is a subclass of JComponent class that allows the user to choose either a single or multiple selections of items. A JList can generate a ListSelectiionListener interface and it includes one abstract method valueChanged(). We can display a value when an item is selected from a JList by implementing MouseListener interface or extending MouseAdapter class and call the getClickCount() method with single-click event (getClickCount() == 1) of MouseEvent class.Exampleimport javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class JListItemSeletionTest extends JFrame {    private JList list;    private JScrollPane jsp;    private Vector data;    public JListItemSeletionTest() {       setTitle("JListItemSeletion Test");       ... Read More

How can we minimize/maximize a JFrame programmatically in Java?

raja
Updated on 10-Feb-2020 08:11:34

3K+ Views

A JFrame class is a subclass of Frame class and the components added to a frame are referred to as its contents, these are managed by the contentPane. A JFrame contains a window with title, border, (optional) menu bar and user-specific components. By default, we can minimize a JFrame by clicking on minimize button and maximize a JFrame by clicking on maximize button at the top-right position of the screen. We can also do programmatically by using setState(JFrame.ICONIFIED) to minimize a JFrame and setState(JFrame.MAXIMIZED_BOTH) to maximize a JFrame.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JFrameIconifiedTest extends JFrame implements ActionListener {   ... Read More

How can we disable the cell editing inside a JTable in Java?

raja
Updated on 10-Feb-2020 08:12:59

3K+ Views

A JTable is a subclass of JComponent for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable can fire TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces. By default, we can edit the text and modify it inside a JTable cell. We can also disable the cell editing inside a table by calling the editCellAt() method of JTable class and it must return false.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public final class DisableJTableMouseClickTest extends JFrame {    private JTable table;    private JScrollPane scrollPane;    public DisableJTableMouseClickTest() { ... Read More

How can we create a JPopupMenu with a sub menu in Java?

raja
Updated on 10-Feb-2020 08:15:41

507 Views

A JPopupMenu is a subclass of JComponent class and it can appear anywhere on the screen when a right mouse button is clicked. In order to create a popup menu, we can use the JPopupMenu class. In general, we can add the menu items to a JPopupMenu and also add a submenu to JPopupMenu by adding the menu items to submenu first then add it to JPopupMenu. A Popup menu is triggered by mouse events, so we need to register a MouseListener interface. We can override the mouseReleased() method to display the popup menu when we get an appropriate event by calling isPopupTrigger() method and display it by ... Read More

How can we add/insert a JRadioButton to a JTable cell in Java?

raja
Updated on 10-Feb-2020 08:59:20

729 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 radio button to a JTable cell by customizing the TableCellRenderer interface and the DefaultCellEditor class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class JTableRadioButtonTest extends JFrame {    private DefaultTableModel dtm;    private ButtonGroup bg;    private JTable table;    private JScrollPane jsp;    public JTableRadioButtonTest() {       setTitle("JTableRadioButton Test");   ... Read More

How can we set the foreground and background color to JComboBox items in Java?

raja
Updated on 10-Feb-2020 10:21:27

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 also set the foreground and background color to JComboBox items by using setForeground() and setBackground() methods of a JComboBox class.Exampleimport java.awt.*; import javax.swing.*; public class JComboBoxItemColorTest extends JFrame{    private JComboBox jcb;    public JComboBoxItemColorTest() {       setTitle("JComboBoxItemColor Test");       String[] countries = {"India", "Australia", "England", "South Africa", "Newzealand"};       jcb ... Read More

Advertisements