Found 133 Articles for Swing

How can we set the background/foreground color for individual column of a JTable in Java?

raja
Updated on 10-Feb-2020 12:52:14

3K+ 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, RowSorterListener interfaces. We can change the background and foreground color for each column of a JTable by customizing the DefaultTableCellRenderer class and it has only one method getTableCellRendererComponent() to implement it.Exampleimport java.awt.*; import javax.swing.*; import javax.swing.table.*; public class JTableColumnColorTest extends JFrame {    private JTable table;    private TableColumn tColumn;    public JTableColumnColorTest() {       setTitle("JTableColumnColor Test");       table = ... Read More

How can we change the background and foreground color of a JTooltip in Java?

raja
Updated on 10-Feb-2020 12:44:49

191 Views

A JToolTip is a subclass of JComponent class and we can create a tooltip for any java component by using setToolTipText() method, it can be used to set up a tooltip for the component. The important methods of a JToolTip class are getAccessibleContext(), getComponent(), paramString() and updateUI(). We can change both the background and foreground color of a JToolTip class by using the put() method of UIManager class and pass the arguments ToolTip.background and ToolTip.foreground.Exampleimport java.awt.*; import javax.swing.*; public class JTooltipColorTest extends JFrame {    private JLabel label;    public JTooltipColorTest() {       setTitle("JTooltipColor Test");       setLayout(new FlowLayout());       ... Read More

How can we detect an event when the mouse moves over any component in Java?

raja
Updated on 10-Feb-2020 10:38:28

3K+ Views

We can implement a MouseListener interface when the mouse is stable while handling the mouse event. A MouseEvent is fired when we can press, release or click (press followed by release) a mouse button (left or right button) at the source object or position the mouse pointer at (enter) and away (exit) from the source object. We can detect a mouse event when the mouse moves over any component such as a label by using the mouseEntered() method and can be exited by using mouseExited() method of MouseAdapter class or MouseListener interface.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseOverTest extends JFrame {    private ... Read More

How can we change the JButton text dynamically in Java?

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

7K+ Views

A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons in a Java Swing application. A JButon can generate an ActionListener interface when the user clicking on a button, it can also generate the MouseListener and KeyListener interfaces. By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.getText()) method of a JButton class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public ... Read More

How can we set the orientation of a JTextArea from right to left in Java?

raja
Updated on 10-Feb-2020 10:43:39

793 Views

A JTextArea is a subclass of JTextComponent class and it is a multi-line text component to display the text or allow a user to enter the text. A JTextArea can generate a CaretListener interface when we are trying to implement the functionality of the JTextArea. By default, a JTextarea allows the orientation from left to right, if the user wants to enter a text from right to left by using the setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT) method of JTextArea class.Exampleimport java.awt.*; import javax.swing.event.*; import javax.swing.*; public class JTextAreaOrientationTest extends JFrame {    private JTextArea textArea;    public JTextAreaOrientationTest() {       setTitle("JTextAreaOrientation Test");       textArea = new ... Read More

How to set a tooltip to each column of a JTableHeader in Java?

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

632 Views

A JTableHeader is a subclass of JComponent class, When we create a JTable object, the constructor creates a new JTableHeader object to manage the table component's header. A JTable supplies a setTableHeader() method that establishes the table header component's JTableHeader object and a getTableHeader() method that returns a reference to the table header component's JTableHeader object. We can set a tooltip text to each column of a JTableHeader by overriding the getToolTipText() method of JTableHeader class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableHeaderToolTipTest extends JPanel {    private DefaultTableModel dmodel;    private JTable table;    private JScrollPane jsp;    public JTableHeaderToolTipTest() ... Read More

How to change the position of a JSlider to horizontal/vertical programmatically in Java?

raja
Updated on 10-Feb-2020 10:48:18

262 Views

A JSlider is a subclass of JComponent class and it is similar to scroll bar which allows the user to select a numeric value from a specified range of integer values. It has a knob which can slide on a range of values and can be used to select a particular value. A JSlider can generate a ChangeListener interface and the important methods of JSlider are getMaximum(), getMinimum(), getOrientation(), getValue() and setValue(). The default position of a JSlider is horizontal and we can also set the position to vertical programmatically by selecting a menu item from a menu bar. It can generate an ActionListener interface ... Read More

How can we show a popup menu when the user right-clicks on a JComboBox in Java?

raja
Updated on 10-Feb-2020 10:50:35

317 Views

A JComboBox is a subclass of JComponent class that displays a drop-down list and gives users options that we can select one and only one item at a time. A JComboBox can be editable or read-only. A getSelectedItem() method can be used to get the selected or entered item from a combo box. We can invoke a popup menu from a JComboxBox when the user right-clicks on it by implementing a MouseListener interface and need to override the mouseReleased() method. The method isPopupTrigger() of MouseEvent class can be used to show a popup menu.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JComboBoxPopupTest extends JFrame {    private ... Read More

How can we set a background color to JSplitPane in Java?

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

289 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 set a background color to a JSplitPane by adding two different background colors to two panels first and pass these arguments to JSplitPane constructor.Exampleimport javax.swing.*; import java.awt.*; public class JSplitPaneColorTest extends JFrame {    private JSplitPane jsp;    private JPanel panel1, panel2;    public JSplitPaneColorTest() ... Read More

How to implement the search functionality of a JTable in Java?

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

2K+ Views

A JTable is a subclass of JComponent 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, RowSorterListener interfaces. We can implement the search functionality of a JTable by input a string in the JTextField, it can search for a string available in a JTable. If the string matches it can only display the corresponding value in a JTable. We can use the DocumentListener interface of a JTextField to implement it.Exampleimport java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public ... Read More

Advertisements