Programming Articles - Page 2638 of 3363

How to get Directories from JFileChooser in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

231 Views

To get directories from JFileChoose, use the mode setFileSelectionMode −JFileChooser file = new JFileChooser(); file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);The following is an example to get Directories from JFileChooser −Exampleimport javax.swing.JFileChooser; public class SwingDemo {    public static void main(String[] args) {       JFileChooser file = new JFileChooser(); file.setMultiSelectionEnabled(false);          file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);       int res = file.showOpenDialog(null);       if (res == JFileChooser.APPROVE_OPTION) { java.io.File f = file.getSelectedFile();          System.err.println(f.getPath());       }    } }Output

Java Program to display the contents in JTextArea

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

477 Views

The following is an example to display the contents of a text file in JTextArea −Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo {    private JFrame mainFrame;    private JLabel statusLabel;    private JPanel controlPanel;    public SwingDemo() {       prepareGUI();    }    public static void main(String[] args) {       SwingDemo demo = new SwingDemo();       demo.showTextAreaDemo();    }    private void prepareGUI() {       mainFrame = new JFrame("Java Swing");       mainFrame.setSize(400, 400);       mainFrame.setLayout(new GridLayout(3, 1));       mainFrame.addWindowListener(new WindowAdapter() {     ... Read More

How to create FileFilter for JFileChooser in Java and display File Type accordingly?

Samual Sam
Updated on 30-Jul-2019 22:30:26

1K+ Views

To create FileFilter, use the FileNamExtensionFilter class. The following is an example to display File Type in JFileChooser −Exampleimport javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; public class SwingDemo {    public static void main(String[] args) {       JFileChooser file = new JFileChooser();       file.setAcceptAllFileFilterUsed(false);       FileNameExtensionFilter extFilter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");       file.addChoosableFileFilter(extFilter);       file.showOpenDialog(null);    } }Output

Fesetround() and fegetround() in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

143 Views

Here we will see the fesetround() and fegetround() method in C++. These methods can be found in the cfenv library.The fesetround() method is used to set the specified floating point rounding direction to the current rounding direction. This is used with rint(), nearbyint() and some other rounding functions in C++.The syntax is like below −int fesetround(int round);The round can be among these FE_TONEAREST, FE_DOWNWARD, FE_UPWARD etc. This function returns 0 when rounding direction is successfully applied to the required manner.Example#include #include #include using namespace std; main() {    double x = 4.7, ans;    fesetround(FE_TONEAREST); //round to ... Read More

Hiding of all overloaded methods in base class in C++

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

600 Views

In C++, we can use the function overloading techniques. But if some base class has one method in overloaded form (different function signature with the same name), and the derived class redefines one of the function which is present inside the base, then all of the overloaded version of that function will be hidden from the derived class.Let us see one example to get the clear idea.Example#include using namespace std; class MyBaseClass {    public:       void my_function() {          cout

Inheritance and friendship in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

2K+ Views

In C++, the friendship is not inherited. It means that, if one parent class has some friend functions, then the child class will not get them as friend.In this example it will generate an error because the display() function is friend of MyBaseClass but not the friend of MyDerivedClass. The display() can access the private member of MyBaseClass.Example#include using namespace std; class MyBaseClass {    protected:       int x;    public:       MyBaseClass() {          x = 20;       }       friend void display(); }; class MyDerivedClass : public MyBaseClass {    private:       int y;    public:       MyDerivedClass() {          x = 40;       } }; void display() {    MyDerivedClass derived;    cout

Extending namespace and Unnamed namespace

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

341 Views

Here we will see how we can extend some namespace, and how the unnamed or anonymous name space can be used.Sometimes we can define one namespace. Then we can write the namespace again with the same definition. If the first one has some member, and second one has some other members, then the namespace is extended. We can use all of the members from that namespace.Example#include using namespace std; namespace my_namespace {    int my_var = 10; } namespace my_namespace { //extending namespace    int my_new_var = 40; } main() {    cout

Can main() be overloaded in C++?

Tapas Kumar Ghosh
Updated on 03-Jun-2025 14:17:49

866 Views

In every C/C++ program, execution starts from the main() function. Defining multiple main() functions will result in a compilation error. Can main() be Overloaded in C++? No, we cannot overload the main() function in C++ because main() serves as the entry point of any C++ program and must follow a predefined prototype. While C++ does support function overloading (i.e., multiple functions with the same name but different parameters), this does not apply to the main() function. If you try to create multiple main() functions will result in a compilation error due to invalid overloading. The following are the only two ... Read More

Function overloading and const keyword in C++

Tapas Kumar Ghosh
Updated on 13-Jun-2025 14:02:47

691 Views

In C++, function overloading and const keyword are used for different purposes. Function overloading provides different ways to call a function with different parameter types that make the program more readable. While the const keyword provides the ways of declaration such as variable, member variable, function parameters, member function, and return type. What is Function Overloading? Function overloading is the process of defining multiple functions having the same name but different parameter lists. It is also known as compile-time polymorphism. Here, we have list of three points to describe function overloading in C++: The parameter ... Read More

Private Destructor in C++

Nishtha Thakur
Updated on 19-Nov-2024 18:21:52

1K+ Views

In C++, a private destructor is a destructor that is declared within a private access specifier, which means that the destructor cannot be accessed or called directly outside the class, which is useful for design patterns where the user wants to control how and when an object will destroy. Syntax Here is the following syntax for a private destructor, which is declared in the class like any other destructor, but with the private access specifier. class MyClass {private: // Private destructor ~MyClass() { }public: // Public constructor MyClass() { // Constructor code here }}; Here we will see what will ... Read More

Advertisements