Found 112 Articles for AWT

How can we set the shortcut key to a JButton in Java?

raja
Updated on 10-Feb-2020 06:43:17

1K+ 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 also set the short cut keys for a JButton using the setMnemonic() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JButtonTest extends JFrame {    private JButton button;    public JButtonTest() {       setTitle("JButtonTest");       button = new JButton("Click or press ALT-C");       button.setMnemonic('C');       add(button, BorderLayout.CENTER);       button.addActionListener(new ActionListener() ... Read More

How to change each column width of a JTable in Java?

raja
Updated on 10-Feb-2020 06:45:38

6K+ Views

JTableA 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.The DefaultTableModel class can extend 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.A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, RowSorterListener interfaces.By default the width of a JTable is fixed, we can also change the width of each column by using table.getColumnModel().getColumn().setPreferredWidth() method of JTable class.Exampleimport java.awt.*; import javax.swing.*; import javax.swing.table.*; public class ... Read More

How to display a bold text inside the JTextArea in Java?

raja
Updated on 10-Feb-2020 06:47:37

1K+ Views

A JTextArea class can extend JTextComponent and allow a user to enter multiple lines of text inside it. A JTextArea can generate a CaretListener interface, which can listen to caret update events. We can set a font to a text inside the JTextArea by using setFont() method.Exampleimport java.awt.*; import javax.swing.*; public class JTextAreaTextBoldTest extends JFrame {    private JTextArea textArea;    public JTextAreaTextBoldTest() {       setTitle("JTextAreaTextBold Test");       setLayout(new BorderLayout());       textArea= new JTextArea();       textArea.setLineWrap(true);       textArea.setWrapStyleWord(true);       Font boldFont=new Font(textArea.getFont().getName(), Font.BOLD, textArea.getFont().getSize());       textArea.setFont(boldFont); // bold ... Read More

What is the importance of JSeparator class in Java?

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

136 Views

JSeparatorA JSeparator is a horizontal or vertical line or an empty space that separates the components.A JSeparator class is used to draw a line to separate the components in a Layout.The easiest way to add a separator to a menu or toolbar is to call the addSeparator() method provided by the classes JMenu, JPopupMenu and JToolBar.The important methods of JSeparator class are setOrientation() and getOrientation().Exampleimport java.awt.*; import javax.swing.*; public class JSeparatorTest extends JFrame {    private JLabel label1, label2;    public JSeparatorTest() {       setTitle("JSeparator Test");       setLayout(new GridLayout(0, 1));       label1 = new JLabel("Above Separator");       add(label1);       JSeparator sep ... Read More

What is the importance of JViewport class in Java?

raja
Updated on 11-Feb-2020 11:11:40

318 Views

JViewportA JViewport class defines the basic scrolling model and it is designed to support both logical scrolling and pixel-based scrolling.The viewport's child called the view is scrolled by calling JViewport.setViewPosition() method.A JViewport class supports logical scrolling, that is a kind of scrolling in which view coordinates are not pixels.To support a logical scrolling, JViewport defines a small set of methods that can be used to define the geometry of a viewport and a view. By default, these methods just report the pixel dimensions of the viewport and view.Exampleimport java.awt.*; import javax.swing.*; public class JViewportTest extends JFrame {    public JViewportTest() {       setTitle("JViewport Test"); ... Read More

What are the differences between paint() method and repaint() method in Java?

raja
Updated on 10-Feb-2020 06:52:01

6K+ Views

Paint() and Repaint()paint(): This method holds instructions to paint this component. In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint().repaint(): This method cannot be overridden. It controls the update() -> paint() cycle. We can call this method to get a component to repaint itself. If we have done anything to change the look of the component but not the size then we can call this method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class PaintRepaintTest extends JPanel implements ... Read More

How can we catch a double click and enter key events for a JList in Java?

raja
Updated on 10-Feb-2020 06:55:05

490 Views

A JList can extend JComponent class that allows the user to choose either a single or multiple selections. A JList can generate a ListSelectiionListener interface and it includes one abstract method valueChanged(). A JList can also generate a MouseListener interface to catch a double click event in the list and generates a KeyListener interface to catch an enter key event.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; public class JListTest extends JFrame {    public JListTest() {       setTitle("JList Test");       setLayout(new FlowLayout());       Vector v = new Vector();       for (int i = 1; i < ... Read More

What are the differences between a Font and a FontMetrics in Java?

raja
Updated on 10-Feb-2020 06:57:14

1K+ Views

A Font class is used to set the screen fonts and it maps the characters of the language to their respective glyphs whereas a FontMetrics class defines a font metrics object, which encapsulates information about the rendering of a particular font on a particular screen.FontA Font class can be used to create an instance of a Font object to set the font for drawing text, label, text fields, buttons, etc and it can be specified by its name, style, and size.The fonts have a family name, a logical name and a face-namefamily name: It is the general name of the font, such as Courier.logical ... Read More

How can we set the background color to a JPanel in Java?

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

12K+ Views

A JPanel is a container and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components like buttons, text fields, labels, table, list, tree and etc. to a JPanel. We can set a background color to JPanel by using the setBackground() method.Exampleimport java.awt.* import javax.swing.*; public class JPanelBackgroundColorTest extends JFrame {    private JPanel panel;    public JPanelBackgroundColorTest() {       setTitle("JPanelBackgroundColor Test");       panel = new JPanel();       panel.add(new JLabel("Welcome to Tutorials Point"));       panel.setBackground(Color.green);       add(panel, BorderLayout.CENTER);       ... Read More

How many types of selection modes for a JList in Java?

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

459 Views

A JList is a component that can extend JComponent class used to display a list of objects that allows the user to select one or more items.There are three types of selection modes for a JList in JavaListSelectionModel.SINGLE_SELECTION: Only one list index can be selected at a time.ListSelectionModel.SINGLE_INTERVAL_SELECTION: Only one contiguous interval can be selected at a time.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: In this mode, there is no restriction on what can be selected. This is a default mode.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JListSelectionModeTest extends JFrame implements ActionListener {    private JList list;    private DefaultListModel listModel;    public JListSelectionModeTest() {     ... Read More

Previous 1 ... 5 6 7 8 9 ... 12 Next
Advertisements