having a little trouble with my code. I've put in a file selector/reader but I have no idea how to make it display in the leftpane within my GUI. Ive tried a few things to no success and cannot figure it out. If someone could provide a solution and an explanation so that I can understand what I've done wrong/not done it would be highly appreciated.
Also my code is a bit messy and with the way I've done my methods its made doing certain things annoying and inconvenient if anyone has troops for the future in organizing methods and code it would be highly appreciated.
Here is my code.
public class JavaAssignment {
public static JMenuBar setupMenu() { // method modifiers where mising here
JMenuBar menuBar = new JMenuBar(); // menubar
JMenu menu1 = new JMenu("File"); //menu
menuBar.add(menu1); // add menu to gui
JMenu menu2 = new JMenu("Help");
menuBar.add(menu2);
JMenuItem menuItem1 = new JMenuItem("Load File", KeyEvent.VK_1); // create drop down menu
JMenuItem menuItem2 = new JMenuItem("Save File", KeyEvent.VK_1);
JMenuItem menuItem3 = new JMenuItem("Exit", KeyEvent.VK_1);
JMenuItem menuItem4 = new JMenuItem("About", KeyEvent.VK_1);
menu1.add(menuItem1); // adds drop down menus to gui
menu1.add(menuItem2);
menu1.add(menuItem3);
menu2.add(menuItem4);
// execute code when selected
menuItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JFileChooser fc = new JFileChooser();
// you can set the directory with the setCurrentDirectory method.
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
// User has selected to open the file.
File file = fc.getSelectedFile();
try {
// Open the selected file
BufferedReader reader = new BufferedReader(new FileReader(file));
// Output the contents to the console.
String nextLine = reader.readLine();
while ( nextLine != null ) {
final JTextArea input = new JTextArea(nextLine);
nextLine = reader.readLine();
}
reader.close();
} catch (IOException e1) {
System.err.println("Error while reading the file");
}
}
}});
return menuBar;
}
public static void main(String[] args) throws FileNotFoundException {
window window = new window();
}
public static class window extends JFrame {
public window() throws FileNotFoundException {
JScrollPane leftScrollPane = new JScrollPane();
JPanel rightPane = new JPanel();
JSplitPane splitPane;
this.setVisible(true);
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerSize(10);
splitPane.setDividerLocation(100);
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(600);
Dimension minimumSize = new Dimension(100, 50);
leftScrollPane.setSize(400, 400);
this.setJMenuBar(setupMenu());
splitPane.setPreferredSize(new Dimension(400, 200));
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightPane);
this.add(splitPane);
this.setSize(1280, 720);
}
}
}