- Display files in java windows
- Display files in java windows
- Java swing tutorial [02] — Display window at center of screen
- Java swing tutorial [01]
- Creating another window for just displaying text + swing
- What is the best way to move JFrame to 2nd display in Windows 10
- How to display all the files in a directory using Java
- Result
- How to Display the Contents of a Directory in Java
- Related Articles
- Training Options
- Course Catalog
- Displaying files in java
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
Display files in java windows
Now I am in a need to display text along with running of the code side by side. Maybe your’s. Question: I’m trying to move my jframe to second display automatically in windows 10 but its not working, I’m using JDK8, and video drivers are upto date below is my code:
Display files in java windows
Hi I want to display files name in a windows with JFrame but it don’t work, I don’t know how to add a new element in arrayList for each files I’m short of idea.
import java.io.File; import java.io.IOException; import javax.swing.JFrame; public class InterfaceGraphique extends JFrame < private static final long serialVersionUID = 1L; public InterfaceGraphique() < this.setTitle("My first Window"); this.setSize(800,1000); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // First Graph Object Graph graph = new Graph(); String Path = "./TEST"; try < parcourir2(Path); >catch (IOException e) < e.printStackTrace(); >this.setContentPane(graph); this.setVisible(true); > public static void parcourir2 (String Path) throws IOException < String[] Name = ; Graph graph = new Graph(); File repertoire = new File(Path); File[] liste = repertoire.listFiles(); int y = 150; if (liste != null) < for (int i = 0; i < liste.length; i++) < if (liste[i].isDirectory()) < parcourir2(liste[i].getAbsolutePath()); >if(liste[i].isFile()) < Point Name[i] = new Point(liste[i].getName(),150,y,"normal"); graph.addPoint(Nom[i]); System.out.println(liste[i] + "\n"); Test.TestFile.Afficher(liste[i]); >y+=50 ; > > else < System.err.println( "Nom de repertoire invalide"); >> >
and there is the class graph :
import java.awt.Graphics; import javax.swing.JPanel; import java.awt.Color; import java.awt.Font; import java.util.ArrayList; import java.util.List; public class Graph extends JPanel < private static final long serialVersionUID = 1L; Listpoints = new ArrayList(); @Override public void paintComponent(Graphics g) < System.out.println("Je suis exécutée !"); for (Point point : points) < if(point.getEtat().contentEquals("erreur")) < Font font = new Font("Courier", Font.BOLD, 20); g.setFont(font); g.setColor(Color.red); >else if(point.getEtat().contentEquals("normal")) < Font font = new Font("Courier", Font.BOLD, 20); g.setFont(font); g.setColor(Color.black); >else if(point.getEtat().contentEquals("valide")) < Font font = new Font("Courier", Font.BOLD, 20); g.setFont(font); g.setColor(Color.green); >g.drawString(point.getFichier(), point.getX(), point.getX()); > > public void addPoint(Point point) < points.add(point); >public static void main(String[] args) < @SuppressWarnings("unused") InterfaceGraphique ig = new InterfaceGraphique(); >>
It’s the final step for my project it’s very important, thanks for help I search since a long time.
- None of your methods (other than main() ) should be static.
- It is rarely a good idea to extend JFrame.
- Your class InterfaceGraphique serves no useful purpose.
- rcourir2() should be an instance method of Graph.
- All of this should be done on the EventDispatchThread, not on the main thread. Your main should create a Runnable to run on the EDT, and all of the GUI stuff done in its run() method.
- In that run method, create a JFrame, create a Graph, add the graph to the frame, set its size or pack it, and make it visible.
Can Java display a dialog without changing the active window?, I’d like to display a modal (progress) dialog above a Frame , even if that Frame isn’t the active window. However, displaying a dialog
Java swing tutorial [02] — Display window at center of screen
Find the source code here -https://bushansirgur.in/java-swing-display-jframe-window-at Duration: 3:00
Java swing tutorial [01]
Find the source code here -https://bushansirgur.in/java-swing-display-basic-window-example Duration: 3:31
Creating another window for just displaying text + swing
I currently have a window frame with panels inside and am using it to run my program. Now I am in a need to display text along with running of the code side by side.
The problem with displaying it in the same window is that it minimizes the total screen area which I am using for input handling.
Thus I decided to create a dialog box and dump all the text inside.
This proved to be a bad decision as sometimes the output is really large and dialog boxes do not contain the scroll bar.
Can anyone tell me what’s the best way to proceed?
I just want suggestions to what should I try and do not need any code part for it as I would like to do that on my own 🙂
- Create a new JWindow (or JFrame, one tutorial here: http://download.oracle.com/javase/tutorial/uiswing/components/frame.html)
- Inside of it, create a new JTextArea and dump all your text there
- to get the scrolling, create a new JScrollPane around your JTextArea. Tutorial: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
You should follow tradition and use either:
- One JFrame that will host your application and open any number of JDialog s your for your scrolling text etc.
JFrame is a top-level container and every application should have only one. You do not loose any functionality this way, and you stick with the standards, which will definitely make somebody’s life easier in the future. Maybe your’s.
Getting a JFrame to display floating on tiling window managers, You can then customize and add a JPanel with the layout and components you need in your application. The size of the window should be set
What is the best way to move JFrame to 2nd display in Windows 10
I’m trying to move my jframe to second display automatically in windows 10 but its not working, I’m using JDK8, and video drivers are upto date below is my code:
import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; import javax.swing.JLabel; public class DualMonitor < public static void main(String. args) < GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, "Found : " + gs.length, "screen detected ?", javax.swing.JOptionPane.DEFAULT_OPTION); for (int j = 0; j < gs.length; j++) < GraphicsDevice gd = gs[j]; JFrame frame = new JFrame(gd.getDefaultConfiguration()); frame.setTitle("I'm on monitor #" + j); frame.setSize(400, 200); frame.add(new JLabel("hello world")); frame.setVisible(true); >> >
Issue is resolved by using setLocationRelativeTo , setUndecorated and setExtendedState(JFrame.MAXIMIZED_BOTH) check below:
import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; import javax.swing.JLabel; public class DualMonitor < public static void main(String. args) < GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, "Found : " + gs.length, "screen detected ?", javax.swing.JOptionPane.DEFAULT_OPTION); for (int j = 0; j < gs.length; j++) < GraphicsDevice gd = gs[j]; JFrame dualview = new JFrame(gd.getDefaultConfiguration()); JFrame frame = new JFrame(); frame.setLocationRelativeTo(dualview); dualview.dispose(); frame.setUndecorated(true); frame.setTitle("I'm on monitor #" + j); frame.setSize(400, 200); frame.add(new JLabel("hello world")); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setVisible(true); >> >
Java Swing — Why is my first window not disposing/closing?, I’m wondering how would I change the display of the frame then? Should I use panels for each display i want? – data_pi. Jun 4, 2017 at 5:42.
How to display all the files in a directory using Java
Following example shows how to display all the files contained in a directory using list method of File class.
import java.io.*; public class Main < public static void main(String[] args) < File dir = new File("C:"); String[] children = dir.list(); if (children == null) < System.out.println( "Either dir does not exist or is not a directory"); >else < for (int i = 0; i< children.length; i++) < String filename = children[i]; System.out.println(filename); >> > >
Result
The above code sample will produce the following result.
build build.xml destnfile detnfile filename manifest.mf nbproject outfilename src srcfile test
The following is an another example to display all the files in a directory.
import java.io.File; public class ReadFiles < public static File folder = new File("C:\\Apache24\\htdocs"); static String temp = ""; public static void main(String[] args) < System.out.println("Reading files under the folder "+ folder.getAbsolutePath()); listFilesForFolder(folder); >public static void listFilesForFolder(final File folder) < for (final File fileEntry : folder.listFiles()) < if (fileEntry.isDirectory()) < listFilesForFolder(fileEntry); >else < if (fileEntry.isFile()) < temp = fileEntry.getName(); if ((temp.substring(temp.lastIndexOf('.') + 1, temp.length()).toLowerCase()).equals("txt"))System.out.println( "File = " + folder.getAbsolutePath()+ "\\" + fileEntry.getName()); >> > > >
The above code sample will produce the following result.
Reading files under the folder C:\Apache24\htdocs File= C:\Apache24\htdocs\android\bkp\end.txt File= C:\Apache24\htdocs\android\end.txt File= C:\Apache24\htdocs\cpp_standard_library\images\code.txt File= C:\Apache24\htdocs\java\Java - Data Structures.txt File= C:\Apache24\htdocs\java\Java - Inheritance.txt File= C:\Apache24\htdocs\scripts\easyui\changelog.txt
How to Display the Contents of a Directory in Java
Displaying the contents of a directory in Java can be accomplished using the File class. This class provides the listFiles method that returns a list of File objects for a given directory. The objects may represent files or subdirectories. To understand how to display the contents of a directory in Java, follow these four steps.
- Open your text editor and type in the following Java statements:The program instantiates a File object and passes to the constructor the directory whose contents are to be displayed. Replace the directory shown with a directory on your file system that you know contains files and at least one subdirectory. The listFiles method returns an array of File objects. The program iterates through the array and tests each entry. A message is displayed indicating if the object is a file or a directory. The name of the entry is also displayed.
- Save your file as DisplayTheContentsOfADirectory.java .
- Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter .
- Type in the command to run your program and hit Enter .Verify that the output displays all files and subdirectories in the directory you specified in the File constructor parameter.
Related Articles
- How to Check Object Type in Java
- How to Create a Jar File in Java
- How to Compile Packages in Java
- How to Throw an Exception in Java
- How to Create an Exception Class in Java
- How to Use the super Keyword to Call a Base Class Constructor in Java
- How to Use the Comparator.comparing Method in Java 8
- How to Use System.in in Java
- How to Call an Interface Method in Java
- How to Add a Time Zone in the Java 8 Date/Time API
- How to Rethrow an Exception in Java
- How to Use the instanceof Operator with a Generic Class in Java
- How to Instantiate an Object in Java
- How to Filter Distinct Elements from a Collection in Java 8
- How to Create a Derived Class in Java
- How to Skip Elements with the Skip Method in Java 8
- How to Create a Java Bean
- How to Implement an Interface in Java
- How to Compare Two Objects with the equals Method in Java
- How to Set PATH from JAVA_HOME
- How to Prevent Race Conditions in Java 8
- How to Write a Block of Code in Java
- How to Display the Contents of a Directory in Java (this article)
- How to Group and Partition Collectors in Java 8
- How to Create a Reference to an Object in Java
- How to Reduce the Size of the Stream with the Limit Method in Java 8
- How to Write an Arithmetic Expression in Java
- How to Format Date and Time in the Java 8 Date/Time API
- How to Use Comparable and Comparator in Java
- How to Break a Loop in Java
- How to Use the this Keyword to Call Another Constructor in Java
- How to Write a Unit Test in Java
- How to Declare Variables in Java
- How to Override Base Class Methods with Derived Class Methods in Java
- How to Use Serialized Objects in Java
- How to Write Comments in Java
- How to Implement Functional Interfaces in Java 8
- How to Write Type Parameters with Multiple Bounds in Java
- How to Add Type and Repeating Annotations to Code in Java 8
- How to Use Basic Generics Syntax in Java
- How to Map Elements Using the Map Method in Java 8
- How to Work with Properties in Java
- How to Write while and do while Loops in Java
- How to Use the finally Block in Java
- How to Write for-each Loops in Java
- How to Create a Method in Java
- How to Continue a Loop in Java
- How to Handle Java Files with Streams
- How to Create an Interface Definition in Java
- How Default Base Class Constructors Are Used with Inheritance
Training Options
Course Catalog
Displaying files in java
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter