I want to open a text file in a frame using swing components, preferably with highlighting facility. I get the name of the text file in a text filed in the first frame and want to open the text file in the second frame.My code is
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FirstGUI extends JFrame {
private JLabel label;
private JTextField textfield;
private JButton button;
public FirstGUI() {
setLayout(new FlowLayout());
label = new JLabel("Enter the file path:");
add(label);
textfield = new JTextField();
add(textfield);
button = new JButton("Open");
add(button);
AnyClass ob = new AnyClass();
button.addActionListener(ob);
}
public class AnyClass implements ActionListener {
public void actionPerformed(ActionEvent obj) {
//SecondGUI s =new SecondGUI();
//s.setVisible(true);
}
}
public static void main(String[] args) {
FirstGUI obj= new FirstGUI();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setSize(600,600);
obj.setLocation(100,100);
obj.setVisible(true);
}
}
What swing component should I use in my second frame to open a text file in it ? If possible, please provide the outline of the code !!
The simplest choice would be a
JTextArea.Another better choice is a
JEditorPane.You can take a look at this text components tutorial for understanding them better and choosing the best you need.