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();
         }
      });
   }
}

Recommended Answers

All 12 Replies

Do you have any questions or problems? Please ask or explain.

Do you have any questions or problems? Please ask or explain.

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.

it doesn't change the JTabbedPane to BOTTOM instead of TOP.

Is the listener method called?

Is the listener method called?

yes... it's called on line #119. When I replace System.out.println("a message") instead of the JTabbedPane part, it displays the message on the cmd prompt. So it does work, but when I replace it with JTabbedPane, nothing happens.

I am wondering if I am suppose to use ActionListener or ChangeStateListener for JTabbedPane

Line 119 does not call it.
It is called when there is an Event like when you click on a menu item.

did you solve this problem? i do not know how to read the file to get the contact info
the file is in this order. Each line contains the next info.
Last, First~email~photo.jpg

@nandosss Please start your own thread. They are free.

i believe this person and i have the same problem and that it can be solve in just one thread

I doubt it, unless you are copying his code.

Please explain in a more lucid tone. And if you have another problem, @nandosss, start a new thread.

just create an array of JPanel another for the text fields and another for the images and another one for the text labs. Then create a foor loop and in there get the first line and use String.split() on ~ then this will return you and array

JLabel [] name = new JLabel [1000];
		JLabel [] mail = new JLabel [1000];
		JLabel [] photo = new JLabel [1000];
		JTextField [] name1 = new JTextField [1000];
		JTextField [] mail1 = new JTextField [1000];
		File file = new File("contacts.txt");
		
		Scanner sc = new Scanner(file);
		int i = 0;
		
		while(sc.hasNext()){
			contact[i] = new JPanel(new FlowLayout());
			contact[i].setPreferredSize(new Dimension(420,100));
			String next = sc.nextLine();
			name[i] = new JLabel();
			mail[i] = new JLabel();
			name1[i] = new JTextField(25);
			mail1[i] = new JTextField(25);
			
			//ImageIcon pic = new ImageIcon((next.split("~")[2]));
			
			//JLabel picLabel = new JLabel(new ImageIcon( myPicture )); 
			//add( picLabel );
			try{
				BufferedImage myPicture = ImageIO.read(new File(next.split("~")[2])); 
				photo[i] = new JLabel(new ImageIcon(myPicture));
			}
			catch(Exception e){
				photo[i] = new JLabel(new ImageIcon("nopic.jpg"));
			}
            contact[i].add(photo[i]);
			contact[i].add(name[i]);
			contact[i].add(name1[i]);
			contact[i].add(mail[i]);
			contact[i].add(mail1[i]);
			name[i].setText("Name: ");
			mail[i].setText("Mail: ");
			name1[i].setText((next.split("~")[0]));
			mail1[i].setText((next.split("~")[1]));
			name[i].setLabelFor(name1[i]);
			mail[i].setLabelFor(mail1[i]);
			tabedPane.addTab(next.split("~")[0], contact[i]);
			i++;
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.