I'm noob at GUI Swing. I have 2 questions.
1)I'm trying to implement a program that reads a text file, so I can use it to add multiple tabs to the program based on the amount of data in the text file. For example, text file have 3 names. I will use the 3 names to make 3 tabs labeling their name in each of them. Where do I call this fileIO, readContacts() part? When I put readContacts() in the Constructor AddTabs(), the program cannot display the frame. When I take readContacts() out of the Contructor, the the program can display the frame.
2)I have JTabbedPane set at Top as default. I want to change JTabbedPane to set it to BOTTOM when I select it in the Tabs Menu -> Layout Placement -> BOTTOM. Did I setup the ActionListener correctly because it doesn't change the JTabbedPane to BOTTOM instead of TOP.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
public class AddTabs
{
JLabel imageLabel, name, email;
JTabbedPane tpane;
public AddTabs()
{
//try
//{
JFrame frame = new JFrame("AddTabs");
frame.setLayout(new FlowLayout());
frame.setSize(550, 170);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//creates the menubar part
JMenuBar menubar = new JMenuBar();
//----------------creates the file menu
JMenu file = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
//disables open
open.setEnabled(false);
file.add(open);
//creates the exit menu
JMenuItem exit = new JMenuItem("Exit");
file.addSeparator();
file.add(exit);
menubar.add(file);
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
//----------------JMenu for Tabs
JMenu tabs = new JMenu("Tabs");
//creates Submenu for placements
JMenu placement = new JMenu("Placement");
JMenuItem top = new JMenuItem("TOP");
JMenuItem right = new JMenuItem("RIGHT");
JMenuItem bot = new JMenuItem("BOTTOM");
JMenuItem left = new JMenuItem("LEFT");
placement.add(top);
placement.add(right);
placement.add(bot);
placement.add(left);
tabs.add(placement);
tabs.addSeparator();
//creates Submenu for Layout
JMenu layout = new JMenu("Layout Policy");
JMenuItem scroll = new JMenuItem("Scroll");
JMenuItem wrap = new JMenuItem("Wrap");
layout.add(scroll);
layout.add(wrap);
tabs.add(layout);
tabs.addSeparator();
//creates Submenu for defaults
JMenuItem defaults = new JMenuItem("Defaults");
tabs.add(defaults);
menubar.add(tabs);
//-------JMenu for Help
JMenu help = new JMenu("Help");
JMenuItem about = new JMenuItem("About");
help.add(about);
menubar.add(help);
//----------Create Panel
JPanel topPanel = new JPanel(new GridLayout(1,2));
JPanel leftPanel = new JPanel(new GridLayout(1,1));
ImageIcon icon = new ImageIcon("test.jpg");
imageLabel = new JLabel();
imageLabel.setIcon(icon);
imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
leftPanel.add(imageLabel);
readContacts();
JPanel rightPanel = new JPanel();
name = new JLabel("Name:");
rightPanel.add(name);
JTextField t1 = new JTextField(19);
t1.setText("username 1");
rightPanel.add(t1);
email = new JLabel("Email:");
rightPanel.add(email);
JTextField t2 = new JTextField(19);
t2.setText("username1@email.com");
rightPanel.add(t2);
topPanel.add(leftPanel);
topPanel.add(rightPanel);
tpane = new JTabbedPane();
right.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String command = ae.getActionCommand();
if(command.equals("BOTTOM"))
{
tpane = new JTabbedPane(JTabbedPane.BOTTOM);
}
else
tpane = new JTabbedPane();
}
});
tpane.setPreferredSize(new Dimension(525, 100));
for(int i=0; i<9;i++)
tpane.addTab("first person", topPanel);
frame.add(tpane);
frame.setJMenuBar(menubar);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// catch(IOException e)
//{
//}
//}
public void readContacts()
{
try
{
File myFile = new File ("contacts.txt");
Scanner inputFile = new Scanner(myFile);
while (inputFile.hasNext())
{
//String extract = inputFile.split("~");
String[] contacts = new String[5];
int i = 0;
//contacts[i] = extract;
i = i+1; //inserts 1 name into each index
}
inputFile.close();
}
catch(IOException e)
{
}
}
/*
public void actionPerformed(ActionEvent ae)
{
String command = ae.getActionCommand();
if(command.equals("BOTTOM"))
tpane = new JTabbedPane(JTabbedPane.BOTTOM);
}
*/
public static void main(String []args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new AddTabs();
}
});
}
}