Found 7442 Articles for Java

How to enable the display of hidden files in a JFileChooser in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

362 Views

Set the following to FALSE to enable the display of hidden files −JFileChooser file = new JFileChooser(); file.setFileHidingEnabled(false);The following is an example to enable the display of hidden files in a JFileChooser −Examplepackage my; import javax.swing.JFileChooser; public class SwingDemo {    public static void main(String[] args) {       JFileChooser file = new JFileChooser();       file.setMultiSelectionEnabled(false);       file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);       file.setFileHidingEnabled(false);       int res = file.showOpenDialog(null);       if (res == JFileChooser.APPROVE_OPTION) {          java.io.File f = file.getSelectedFile();          System.err.println(f.getPath());       }   ... Read More

Customize the tooltip font, color , background and foreground color in Java

George John
Updated on 30-Jul-2019 22:30:26

2K+ Views

To customize the tooltip font, color and background, use UIManager.UIManager.put("ToolTip.background", Color.ORANGE); UIManager.put("ToolTip.foreground", Color.BLACK); UIManager.put("ToolTip.font", new Font("Arial", Font.BOLD, 14));Above, we have set the font with −Tooltip.fontWe have set the foreground and background color with the following above −ToolTip.foreground ToolTip.backgroundThe following is an example to customize tooltip −Examplepackage my; import java.awt.Color; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.UIManager; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame.setDefaultLookAndFeelDecorated(true);       JFrame frame = new JFrame("Register!");       JLabel label1, label2, ... Read More

Make the JSlider vertical and move top-to-bottom in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

268 Views

To set the JSlider to be vertical, use the VERTICAL constant while creating the slider −JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 60);Now, for the inverted slider i.e. moving from top-to-bottom −slider.setInverted(true);The following is an example to set the slider vertical and move top-to-bottom −Examplepackage my; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Frame with Slider");       JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 60);       slider.setInverted(true);       slider.setMinorTickSpacing(5);       slider.setMajorTickSpacing(20);     ... Read More

How to display the vertical and horizontal scrollbars always even if it is not required

Chandu yadav
Updated on 30-Jul-2019 22:30:26

108 Views

Use the following constants for the JScrollBar to display the vertical and horizontal scrollbars always even if it is not required −scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);The following is an example to display the vertical and horizontal scrollbars always even if it is not required −Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("Questions and Answers");       JButton button2 = new JButton("Videos");       JButton ... Read More

How to set horizontal gap between elements in a GridLayout with Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

4K+ Views

Use the setHgap() method to set the horizontal gap between elements in a GridLayout. Let’s say we have a GridLaypout −GridLayout layout = new GridLayout(2, 4);Set the horizontal gap −layout.setHgap(25);The following is an example −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Sections");       JPanel panel = new JPanel();       panel.setBackground(Color.blue);       GridLayout layout = new GridLayout(2, 4);     ... Read More

Why the main () method in Java is always static?

raja
Updated on 30-Jul-2019 22:30:26

17K+ Views

Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class.In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method.If the main() is allowed to be non-static, then while calling the main() method JVM has to instantiate its class.While instantiating it has to call the constructor of that class, There will be ambiguity if the constructor of that class takes an argument.Static method of a class can be ... Read More

Can we define an abstract class with no abstract methods in Java?

raja
Updated on 22-Nov-2023 09:15:56

11K+ Views

Yes, we can declare an abstract class with no abstract methods in Java. An abstract class means that hiding the implementation and showing the function definition to the user. An abstract class having both abstract methods and non-abstract methods. For an abstract class, we are not able to create an object directly. But Indirectly we can create an object using the subclass object. A Java abstract class can have instance methods that implement a default behavior. An abstract class can extend only one class or one abstract class at a time. Declaring a class as abstract with no abstract methods means that we don't allow it ... Read More

Why an interface doesn\\'t have a constructor whereas an abstract class have a constructor in Java?

Shriansh Kumar
Updated on 01-Sep-2025 13:16:22

9K+ Views

Interfaces in Java are used for defining a contract that classes can implement. They can contain method signatures, default methods, static methods, and constants. we must implement the methods defined in the interface in the implementing class. Constructor in an Interface A Constructor is to initialize the non-static members of a particular class with respect to an object. An Interface in Java doesn't have a constructor because all data members in interfaces are public static final by default, they are constants (assign the values at the time of declaration). There are ... Read More

Why an interface cannot implement another interface in Java?

raja
Updated on 22-Nov-2023 09:29:18

12K+ Views

An interface cannot implement another interface in Java. An interface in Java is essentially a special kind of class. Like classes, the interface contains methods and variables. Unlike classes, interfaces are always completely abstract. An interface is defined just like a class except for the keyword interface in place of a class, the variables are declared in an interface are static and final and the methods are defined in an interface are public abstract methods. An interface can extend any number of interfaces but one interface cannot implement another interface, because if any interface is implemented then its methods must be defined and ... Read More

Check whether a node is a root node or not in JTree

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

298 Views

To check whether a node is a root node or not, use the isRoot() method. This returns a boolean value. TRUE if the node is a root node, else FALSE is returned. For example, TRUE is returned since the following node is a root node −node.isRoot()Another example, FALSE is returned since the following node isn’t a root node −node2.isRoot()The following is an example to check whether a node is a root node or not −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame ... Read More

Advertisements