8

I have create a simple code :

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class tab extends JFrame
{
    JTabbedPane tab=new JTabbedPane();
    JTextField input=new JTextField();
    JButton button=new JButton("process");
    tab()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600,600);
        setLocation(100,100);
        setLayout(new BorderLayout());
        add(tab,"Center");

            tab.add("code",new JPanel());
            tab.add("assembly",new JPanel());
            tab.add("compiler",new JPanel());
            tab.add("Execution",new JPanel());
            tab.add("Structure",new JPanel());

        JPanel panel=new JPanel();
        add(panel,"South");
            panel.setLayout(new BorderLayout());
            panel.add(input,"Center");
            panel.add(button,"East");

        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                tab.setSelectedIndex(Integer.parseInt(input.getText()));
            }
        });

        show();
    }
    public static void main(String[]args)
    {
        new tab();
    }
}

this code, it can selected tab by index.

in my question how to selected tab by finding title. so if I input "compiler" it can select tab that title "compiler".

newbie
  • 929
  • 17
  • 35

2 Answers2

7

You can simply iterate over the tabs and find the index of the one with the given name:

public int findTabByName(String title, JTabbedPane tab)  
{
  int tabCount = tab.getTabCount();
  for (int i=0; i < tabCount; i++) 
  {
    String tabTitle = tab.getTitleAt(i);
    if (tabTitle.equals(title)) return i;
  }
  return -1;
}

Then in your code just call findTabByName() passing the user input (and the compoenent) and if the returned index is not -1 you can call tab.setSelectedIndex()

  • what is index ? – newbie Sep 15 '16 at 14:42
  • @newbie: sorry, typo. That should be `i` (the variable from the loop) –  Sep 15 '16 at 14:43
  • should i write `findTabByName("compiler",tab);` inside button listener ? i seem doesn't work. – newbie Sep 15 '16 at 14:46
  • `findByTabName()` on its own will do nothing. It only returns the index of the tab with that name. You need to call `setSelectedIndex()` with the value returned from that method (but only if its `> -1`) –  Sep 15 '16 at 14:47
  • @a_horse_with_no_name I am really not sure who deserves the 15 for accept here; thus decided to compensate you at least with another upvote ;-) – GhostCat Sep 15 '16 at 15:10
3

Simple: do your own housekeeping: you create a Map<String, Component> componentsByName; and whenever you add a component to your TabbedPane, you add that component to that map as well.

Of course, you would want to make that as convenient as possible; so you should do something like:

public class Tab extends JFrame {
  private final JTabbedPane tab=new JTabbedPane();
  private final Map<String, Component> componentsByName = new HashMap<>();

  ...
  private void addComponentToPaneAndMap(String title, Component component) {
    tab.add(title, component);
    componentsByName.put(title, component);
  }

( well, not only for convenience, but also to make sure that all components that go into your pane also go into the map! )

And then, you simply replace

tab.add("code",new JPanel());

with

addComponentToPaneAndMap("code", new JPanel());

And later on, when you need to access one of the components in your tab; you can do a simple lookup like:

 Component someComponent = componentsByName.get("compiler");

Please note:

  1. I "fixed" the name of your class - class names always start UpperCase.
  2. I changed your fields to be private & final - that is like the "default" you should strive for: make sure that the internals of your classes stay internal (using private); and that you dont need to worry about accidentally overwriting their content (using final).

If you think you will need this more often, it would be even reasonable to create your own subclass of JTabbedPane that uses the above mechanisms and provides corresponding getter methods already.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • can u make example from my code ? I feel a little difficult to understand your answer, i'm sorry, i'm beginner. – newbie Sep 15 '16 at 13:55
  • It seems so. Just look carefully: the code I put up in my answer is meant to be simply **added** to your current code base. Well, one minor piece was missing; which I will add in a second. In other words: you already got a solution handcrafted for your code base. Dont get me wrong now: if your skills are really on a level that you dont understand that; then I would suggest to STOP writing UI stuff for now. Because then you are lacking super-basic knowledge about Java. Then you should first learn those **basics**; instead of trying to build UI applications. – GhostCat Sep 15 '16 at 13:59
  • OK; updated my answer. It really contains all the elements now that you need; you just need to pull them into your code! Let me know if it works for you; and if so, dont forget about accepting the answer ;-) – GhostCat Sep 15 '16 at 14:02
  • should i include `Component someComponent = componentsByName.get("compiler");` inside button listener ? it cannot work – newbie Sep 15 '16 at 14:19
  • are u sure `private addComponentToPaneAndMap(String title, Component component)` not add void ? – newbie Sep 15 '16 at 14:20
  • A) you are correct, I forget about the void there B) I can't tell you how to further write your code. You asked for a way to access the components within the pane by name. I showed you one way, probably, **the** one most reasonable way to do that. – GhostCat Sep 15 '16 at 14:30
  • my question how to select tab by title if i input from textfield. – newbie Sep 15 '16 at 14:45
  • Seriously: I gave you a way to **exactly** do that - a way that will allow you to fetch that component by its title. Sorry, this is not programming school where people teach you each and any step. See my second comment - if you are really unable to grasp such easy things, than that is the problem you should focus on now. You try to run, but you are hardly able to crawl so far. Your input field **gives** you a **string**. You ask the map if it has a component for that **string**. If that is too much for you to pull together; then as said: you are not ready for such tasks (yet). – GhostCat Sep 15 '16 at 14:50
  • Giving another indication that my statements are not so far off. It seems that you are **overburdening** yourself. Your "tooling" isn't working for you; and you have little idea about what you are doing. Seriously: try to accomplish **less** for a while. Or you will continue to experience one frustrating moment after the other ... but I am glad that things are working by now! – GhostCat Sep 15 '16 at 15:07