Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select tab by title

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".

like image 472
newbie Avatar asked Sep 15 '16 12:09

newbie


2 Answers

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

like image 81
a_horse_with_no_name Avatar answered Nov 20 '22 16:11

a_horse_with_no_name


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.

like image 3
GhostCat Avatar answered Nov 20 '22 17:11

GhostCat