List of usage examples for javax.swing JTabbedPane setSelectedComponent
@BeanProperty(bound = false, preferred = true, description = "The tabbedpane's selected component.") public void setSelectedComponent(Component c)
From source file:Main.java
public static void main(String[] args) { JTabbedPane tab = new JTabbedPane(); tab.addTab("A", new JPanel()); tab.addTab("+", new JPanel()); tab.getModel().addChangeListener(new ChangeListener() { private int lastSelected; private boolean ignore = false; @Override//from ww w . j ava2s . c o m public void stateChanged(ChangeEvent e) { if (!ignore) { ignore = true; try { int selected = tab.getSelectedIndex(); String title = tab.getTitleAt(selected); if ("+".equals(title)) { JPanel pane = new JPanel(); tab.insertTab("Tab" + (tab.getTabCount() - 1), null, pane, null, lastSelected + 1); tab.setSelectedComponent(pane); } else { lastSelected = selected; } } finally { ignore = false; } } } }); final JButton btn = new JButton("Add"); btn.addActionListener(e -> System.out.println(tab.getTabCount())); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tab); frame.add(btn, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); }
From source file:com.haulmont.cuba.desktop.gui.components.DesktopComponentsHelper.java
public static void focusProblemComponent(ValidationErrors errors) { Component component = null;//from ww w .j a v a2 s . c o m if (!errors.getAll().isEmpty()) { component = errors.getAll().iterator().next().component; } if (component != null) { try { final JComponent jComponent = DesktopComponentsHelper.unwrap(component); java.awt.Component c = jComponent; java.awt.Component prevC = null; while (c != null) { if (c instanceof JTabbedPane && !((JTabbedPane) c).getSelectedComponent().equals(prevC)) { final JTabbedPane tabbedPane = (JTabbedPane) c; // do not focus tabbed pane on programmaticaly selection change JTabbedPaneExt.setFocusOnSelectionChange(false); tabbedPane.setSelectedComponent(prevC); break; } if (c instanceof CollapsiblePanel && !((CollapsiblePanel) c).isExpanded()) { ((CollapsiblePanel) c).setExpanded(true); break; } prevC = c; c = c.getParent(); } if (!JTabbedPaneExt.isFocusOnSelectionChange()) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JTabbedPaneExt.setFocusOnSelectionChange(true); } }); } if (jComponent instanceof FocusableComponent) { ((FocusableComponent) jComponent).focus(); } else { // focus first up component c = jComponent; while (c != null) { if (c.isFocusable()) { c.requestFocus(); break; } c = c.getParent(); } } } catch (Exception e) { LoggerFactory.getLogger(DesktopComponentsHelper.class) .warn("Error while problem component focusing", e); } } }
From source file:fur.shadowdrake.minecraft.InstallPanel.java
private void addonsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addonsActionPerformed addonsPanel = new AddonsPanel( availablePacks.get(modpackChooser.getSelectedIndex()).addons.toArray(new Addon[0])); JTabbedPane tabPane; tabPane = (JTabbedPane) getParent(); tabPane.remove(this); tabPane.add("Addons", addonsPanel); tabPane.setSelectedComponent(addonsPanel); addonsOpenedOnce = true;// w ww .j a v a2s . c om parentFrame.setAbortListener((ActionEvent e) -> { tabPane.remove(addonsPanel); tabPane.add("Install", this); tabPane.setSelectedComponent(this); }); parentFrame.setProceedListener((ActionEvent e) -> { boolean s[]; tabPane.remove(addonsPanel); tabPane.add("Install", this); tabPane.setSelectedComponent(this); s = addonsPanel.getSelected(); for (int n = 0; n < s.length; n++) { availablePacks.get(modpackChooser.getSelectedIndex()).addons.get(n).install = s[n]; } }); }
From source file:net.sf.firemox.DeckBuilder.java
/** * Creates new form DeckBuilder//from w w w. j av a2s . c o m */ private DeckBuilder() { super("DeckBuilder"); form = this; timerPanel = new TimerGlassPane(); cardLoader = new CardLoader(timerPanel); timer = new Timer(200, cardLoader); setGlassPane(timerPanel); try { setIconImage(Picture.loadImage(IdConst.IMAGES_DIR + "deckbuilder.gif")); } catch (Exception e) { // IGNORING } // Load settings loadSettings(); // Initialize components final JMenuItem newItem = UIHelper.buildMenu("menu_db_new", 'n', this); newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK)); final JMenuItem loadItem = UIHelper.buildMenu("menu_db_load", 'o', this); loadItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); final JMenuItem saveAsItem = UIHelper.buildMenu("menu_db_saveas", 'a', this); saveAsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0)); final JMenuItem saveItem = UIHelper.buildMenu("menu_db_save", 's', this); saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); final JMenuItem quitItem = UIHelper.buildMenu("menu_db_exit", this); quitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK)); final JMenuItem deckConstraintsItem = UIHelper.buildMenu("menu_db_constraints", 'c', this); deckConstraintsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0)); final JMenuItem aboutItem = UIHelper.buildMenu("menu_help_about", 'a', this); aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, InputEvent.SHIFT_MASK)); final JMenuItem convertDCK = UIHelper.buildMenu("menu_convert_DCK_MP", this); final JMenu mainMenu = UIHelper.buildMenu("menu_file"); mainMenu.add(newItem); mainMenu.add(loadItem); mainMenu.add(saveAsItem); mainMenu.add(saveItem); mainMenu.add(new JSeparator()); mainMenu.add(quitItem); super.optionMenu = new JMenu("Options"); final JMenu convertMenu = UIHelper.buildMenu("menu_convert"); convertMenu.add(convertDCK); final JMenuItem helpItem = UIHelper.buildMenu("menu_help_help", 'h', this); helpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0)); final JMenu helpMenu = new JMenu("?"); helpMenu.add(helpItem); helpMenu.add(deckConstraintsItem); helpMenu.add(aboutItem); final JMenuBar menuBar = new JMenuBar(); menuBar.add(mainMenu); initAbstractMenu(); menuBar.add(optionMenu); menuBar.add(convertMenu); menuBar.add(helpMenu); setJMenuBar(menuBar); addWindowListener(this); // Build the panel containing amount of available cards final JLabel amountLeft = new JLabel("<html>0/?", SwingConstants.RIGHT); // Build the left list allListModel = new MListModel<MCardCompare>(amountLeft, false); leftList = new ThreadSafeJList(allListModel); leftList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); leftList.setLayoutOrientation(JList.VERTICAL); leftList.getSelectionModel().addListSelectionListener(this); leftList.addMouseListener(this); leftList.setVisibleRowCount(10); // Initialize the text field containing the amount to add addQtyTxt = new JTextField("1"); // Build the "Add" button addButton = new JButton(LanguageManager.getString("db_add")); addButton.setMnemonic('a'); addButton.setEnabled(false); // Build the panel containing : "Add" amount and "Add" button final Box addPanel = Box.createHorizontalBox(); addPanel.add(addButton); addPanel.add(addQtyTxt); addPanel.setMaximumSize(new Dimension(32010, 26)); // Build the panel containing the selected card name cardNameTxt = new JTextField(); new HireListener(cardNameTxt, addButton, this, leftList); final JLabel searchLabel = new JLabel(LanguageManager.getString("db_search") + " : "); searchLabel.setLabelFor(cardNameTxt); // Build the panel containing search label and card name text field final Box searchPanel = Box.createHorizontalBox(); searchPanel.add(searchLabel); searchPanel.add(cardNameTxt); searchPanel.setMaximumSize(new Dimension(32010, 26)); listScrollerLeft = new JScrollPane(leftList); MToolKit.addOverlay(listScrollerLeft); // Build the left panel containing : list, available amount, "Add" panel final JPanel srcPanel = new JPanel(null); srcPanel.add(searchPanel); srcPanel.add(listScrollerLeft); srcPanel.add(amountLeft); srcPanel.add(addPanel); srcPanel.setMinimumSize(new Dimension(220, 200)); srcPanel.setLayout(new BoxLayout(srcPanel, BoxLayout.Y_AXIS)); // Initialize constraints constraintsChecker = new ConstraintsChecker(); constraintsChecker.setBorder(new EtchedBorder()); final JScrollPane constraintsCheckerScroll = new JScrollPane(constraintsChecker); MToolKit.addOverlay(constraintsCheckerScroll); // create a pane with the oracle text for the present card oracleText = new JLabel(); oracleText.setPreferredSize(new Dimension(180, 200)); oracleText.setVerticalAlignment(SwingConstants.TOP); final JScrollPane oracle = new JScrollPane(oracleText, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); MToolKit.addOverlay(oracle); // build some Pie Charts and a panel to display it initSets(); datasets = new ChartSets(); final JTabbedPane tabbedPane = new JTabbedPane(); for (ChartFilter filter : ChartFilter.values()) { final Dataset dataSet = filter.createDataSet(this); final JFreeChart chart = new JFreeChart(null, null, filter.createPlot(dataSet, painterMapper.get(filter)), false); datasets.addDataSet(filter, dataSet); ChartPanel pieChartPanel = new ChartPanel(chart, true); tabbedPane.add(pieChartPanel, filter.getTitle()); } // add the Constraints scroll panel and Oracle text Pane to the tabbedPane tabbedPane.add(constraintsCheckerScroll, LanguageManager.getString("db_constraints")); tabbedPane.add(oracle, LanguageManager.getString("db_text")); tabbedPane.setSelectedComponent(oracle); // The toollBar for color filtering toolBar = new JToolBar(); toolBar.setFloatable(false); final JButton clearButton = UIHelper.buildButton("clear"); clearButton.addActionListener(this); toolBar.add(clearButton); final JToggleButton toggleColorlessButton = new JToggleButton( UIHelper.getTbsIcon("mana/colorless/small/" + MdbLoader.unknownSmlMana), true); toggleColorlessButton.setActionCommand("0"); toggleColorlessButton.addActionListener(this); toolBar.add(toggleColorlessButton); for (int index = 1; index < IdCardColors.CARD_COLOR_NAMES.length; index++) { final JToggleButton toggleButton = new JToggleButton( UIHelper.getTbsIcon("mana/colored/small/" + MdbLoader.coloredSmlManas[index]), true); toggleButton.setActionCommand(String.valueOf(index)); toggleButton.addActionListener(this); toolBar.add(toggleButton); } // sorted card type combobox creation final List<String> idCards = new ArrayList<String>(Arrays.asList(CardFactory.exportedIdCardNames)); Collections.sort(idCards); final Object[] cardTypes = ArrayUtils.addAll(new String[] { LanguageManager.getString("db_types.any") }, idCards.toArray()); idCardComboBox = new JComboBox(cardTypes); idCardComboBox.setSelectedIndex(0); idCardComboBox.addActionListener(this); idCardComboBox.setActionCommand("cardTypeFilter"); // sorted card properties combobox creation final List<String> properties = new ArrayList<String>( CardFactory.getPropertiesName(DeckConstraints.getMinProperty(), DeckConstraints.getMaxProperty())); Collections.sort(properties); final Object[] cardProperties = ArrayUtils .addAll(new String[] { LanguageManager.getString("db_properties.any") }, properties.toArray()); propertiesComboBox = new JComboBox(cardProperties); propertiesComboBox.setSelectedIndex(0); propertiesComboBox.addActionListener(this); propertiesComboBox.setActionCommand("propertyFilter"); final JLabel colors = new JLabel(" " + LanguageManager.getString("colors") + " : "); final JLabel types = new JLabel(" " + LanguageManager.getString("types") + " : "); final JLabel property = new JLabel(" " + LanguageManager.getString("properties") + " : "); // filter Panel with colors toolBar and card type combobox final Box filterPanel = Box.createHorizontalBox(); filterPanel.add(colors); filterPanel.add(toolBar); filterPanel.add(types); filterPanel.add(idCardComboBox); filterPanel.add(property); filterPanel.add(propertiesComboBox); getContentPane().add(filterPanel, BorderLayout.NORTH); // Destination section : // Build the panel containing amount of available cards final JLabel rightAmount = new JLabel("0/?", SwingConstants.RIGHT); rightAmount.setMaximumSize(new Dimension(220, 26)); // Build the right list rightListModel = new MCardTableModel(new MListModel<MCardCompare>(rightAmount, true)); rightListModel.addTableModelListener(this); rightList = new JTable(rightListModel); rightList.setShowGrid(false); rightList.setTableHeader(null); rightList.getSelectionModel().addListSelectionListener(this); rightList.getColumnModel().getColumn(0).setMaxWidth(25); rightList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); // Build the panel containing the selected deck deckNameTxt = new JTextField("loading..."); deckNameTxt.setEditable(false); deckNameTxt.setBorder(null); final JLabel deckLabel = new JLabel(LanguageManager.getString("db_deck") + " : "); deckLabel.setLabelFor(deckNameTxt); final Box deckNamePanel = Box.createHorizontalBox(); deckNamePanel.add(deckLabel); deckNamePanel.add(deckNameTxt); deckNamePanel.setMaximumSize(new Dimension(220, 26)); // Initialize the text field containing the amount to remove removeQtyTxt = new JTextField("1"); // Build the "Remove" button removeButton = new JButton(LanguageManager.getString("db_remove")); removeButton.setMnemonic('r'); removeButton.addMouseListener(this); removeButton.setEnabled(false); // Build the panel containing : "Remove" amount and "Remove" button final Box removePanel = Box.createHorizontalBox(); removePanel.add(removeButton); removePanel.add(removeQtyTxt); removePanel.setMaximumSize(new Dimension(220, 26)); // Build the right panel containing : list, available amount, constraints final JScrollPane deskListScroller = new JScrollPane(rightList); MToolKit.addOverlay(deskListScroller); deskListScroller.setBorder(BorderFactory.createLineBorder(Color.GRAY)); deskListScroller.setMinimumSize(new Dimension(220, 200)); deskListScroller.setMaximumSize(new Dimension(220, 32000)); final Box destPanel = Box.createVerticalBox(); destPanel.add(deckNamePanel); destPanel.add(deskListScroller); destPanel.add(rightAmount); destPanel.add(removePanel); destPanel.setMinimumSize(new Dimension(220, 200)); destPanel.setMaximumSize(new Dimension(220, 32000)); // Build the panel containing the name of card in picture cardPictureNameTxt = new JLabel("<html><i>no selected card</i>"); final Box cardPictureNamePanel = Box.createHorizontalBox(); cardPictureNamePanel.add(cardPictureNameTxt); cardPictureNamePanel.setMaximumSize(new Dimension(32010, 26)); // Group the detail panels final JPanel viewCard = new JPanel(null); viewCard.add(cardPictureNamePanel); viewCard.add(CardView.getInstance()); viewCard.add(tabbedPane); viewCard.setLayout(new BoxLayout(viewCard, BoxLayout.Y_AXIS)); final Box mainPanel = Box.createHorizontalBox(); mainPanel.add(destPanel); mainPanel.add(viewCard); // Add the main panel getContentPane().add(srcPanel, BorderLayout.WEST); getContentPane().add(mainPanel, BorderLayout.CENTER); // Size this frame getRootPane().setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); getRootPane().setMinimumSize(getRootPane().getPreferredSize()); pack(); }
From source file:it.iit.genomics.cru.igb.bundles.mi.business.MIWorker.java
/** * Adds a component to a JTabbedPane with a little close tab" button on the * right side of the tab./*from ww w .j a v a2 s. c o m*/ * * @param tabbedPane * the JTabbedPane * @param c * any JComponent * @param title * the title for the tab */ public static void addClosableTab(final JTabbedPane tabbedPane, final JComponent c, final String title) { // Add the tab to the pane without any label tabbedPane.addTab(null, c); int pos = tabbedPane.indexOfComponent(c); // Create a FlowLayout that will space things 5px apart FlowLayout f = new FlowLayout(FlowLayout.CENTER, 5, 0); // Make a small JPanel with the layout and make it non-opaque JPanel pnlTab = new JPanel(f); pnlTab.setOpaque(false); // Add a JLabel with title and the left-side tab icon JLabel lblTitle = new JLabel(title); // Create a JButton for the close tab button JButton btnClose = new JButton("x"); // btnClose.setOpaque(false); int size = 17; btnClose.setPreferredSize(new Dimension(size, size)); btnClose.setToolTipText("close this tab"); // Make the button looks the same for all Laf's btnClose.setUI(new BasicButtonUI()); // Make it transparent btnClose.setContentAreaFilled(false); // No need to be focusable btnClose.setFocusable(false); btnClose.setBorder(BorderFactory.createEtchedBorder()); btnClose.setBorderPainted(false); // Making nice rollover effect // we use the same listener for all buttons btnClose.setRolloverEnabled(true); // Close the proper tab by clicking the button // Configure icon and rollover icon for button btnClose.setRolloverEnabled(true); // Set border null so the button doesnt make the tab too big btnClose.setBorder(null); // Make sure the button cant get focus, otherwise it looks funny btnClose.setFocusable(false); // Put the panel together pnlTab.add(lblTitle); pnlTab.add(btnClose); // Add a thin border to keep the image below the top edge of the tab // when the tab is selected pnlTab.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0)); // Now assign the component for the tab tabbedPane.setTabComponentAt(pos, pnlTab); // Add the listener that removes the tab ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog(new JFrame(), "Are you sure you want to remove this tab? All results will be lost!", "Remove tab", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { tabbedPane.remove(c); } } }; btnClose.addActionListener(listener); // Optionally bring the new tab to the front tabbedPane.setSelectedComponent(c); }
From source file:fur.shadowdrake.minecraft.InstallPanel.java
public void begin(boolean first) { workingDir = dirBox.getText();/*from w w w . j a v a 2 s. c o m*/ workingPack = (String) modpackChooser.getSelectedItem(); parentFrame.dropAllProceedListeners(); new Thread(() -> { try { JTabbedPane tabPane; tabPane = (JTabbedPane) getParent(); if (force.isSelected()) { log.println("Cleaning up."); cleanUp(new File(workingDir)); Manifest mf = manifest.get(workingPack); if (mf != null) { mf.clear(); } } Pack detected = detectPack(new File(workingDir)); if ((detected == null) && checkDir(new File(workingDir), 2)) { try { EventQueue.invokeAndWait(() -> { JOptionPane.showMessageDialog(parentFrame, "This directory contains an unknown modpack.\nChoose another directory or force reinstall."); }); } catch (InterruptedException | InvocationTargetException ex) { } completedListener.actionPerformed(new ActionEvent(this, 2, "Install")); } else if (detected == null) { try { EventQueue.invokeAndWait(() -> { tabPane.remove(this); }); } catch (InterruptedException | InvocationTargetException ex) { } if (first && !addonsOpenedOnce) { addonsPanel = new AddonsPanel( availablePacks.get(modpackChooser.getSelectedIndex()).addons.toArray(new Addon[0])); tabPane.add("Addons", addonsPanel); tabPane.setSelectedComponent(addonsPanel); addonsOpenedOnce = true; parentFrame.setAbortListener((ActionEvent e) -> { tabPane.remove(addonsPanel); tabPane.add("Install", this); tabPane.setSelectedComponent(this); parentFrame.setProceedListener((ActionEvent ev) -> { begin(false); }); parentFrame.setAbortListener((ActionEvent ev) -> { tabPane.remove(this); parentFrame.state = 0; parentFrame.dropAllAbortListeners(); parentFrame.dropAllProceedListeners(); }); }); parentFrame.setProceedListener((ActionEvent e) -> { tabPane.remove(addonsPanel); boolean s[]; s = addonsPanel.getSelected(); for (int n = 0; n < s.length; n++) { availablePacks.get(modpackChooser.getSelectedIndex()).addons.get(n).install = s[n]; } begin(false); }); return; } if (downloadPack() && downloadAddons() && doPostDownload()) { log.println("Installation completed successful."); log.setStatusText("Ok"); completedListener.actionPerformed(new ActionEvent(this, 1, "Install")); } else { log.println("Installation completed with errors."); log.setStatusText("Error"); EventQueue.invokeLater(() -> { JOptionPane.showMessageDialog(parentFrame, "Installation failed"); }); completedListener.actionPerformed(new ActionEvent(this, 0, "Install")); } } else if (detected.name.equals(modpackChooser.getSelectedItem())) { if (!isInstalled(detected)) { try { Gson gson = new GsonBuilder().create(); tabPane.remove(this); packList.add(detected); String latestVersion = ftpClient.getLatestVersion(detected.name); if (!latestVersion.equals(detected.version)) { updatePack(detected); } try (FileWriter fw = new FileWriter( new File(config.getInstallDir(), "modpacks.json"))) { gson.toJson(packList, fw); } log.println("Installation completed successful."); completedListener.actionPerformed(new ActionEvent(this, 1, "Install")); } catch (IOException ex1) { log.println("Installation failed."); Logger.getLogger(InstallPanel.class.getName()).log(Level.SEVERE, null, ex1); } } else { try { EventQueue.invokeAndWait(() -> { tabPane.remove(this); }); } catch (InterruptedException | InvocationTargetException ex) { } if (downloadAddons()) { log.println("Installation completed successful."); log.setStatusText("Ok"); log.reset(); completedListener.actionPerformed(new ActionEvent(this, 1, "Install")); } else { log.println("Installation failed."); EventQueue.invokeLater(() -> { JOptionPane.showMessageDialog(parentFrame, "Installation failed"); }); } } } else { try { EventQueue.invokeAndWait(() -> { JOptionPane.showMessageDialog(parentFrame, "This directory contains modpack " + detected.name + ".\nSelect the correct pack or force reinstall."); }); } catch (InterruptedException | InvocationTargetException ex) { } completedListener.actionPerformed(new ActionEvent(this, 2, "Install")); } writeFiles(); } catch (NetworkException ex) { Logger.getLogger(InstallPanel.class.getName()).log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(parentFrame, ex.getMessage(), "Network error", JOptionPane.ERROR_MESSAGE); ftpClient.close(); log.println( "Timeout. Previous command wasn't received by the server. This is a network error. Please try again later."); log.setStatusText("Error"); } }, "Installer").start(); }
From source file:pl.otros.logview.api.OtrosApplication.java
public void addClosableTab(String name, String tooltip, Icon icon, JComponent component, boolean show) { JTabbedPane tabbedPane = getJTabbedPane(); if (tabbedPane.indexOfComponent(component) == -1) { int tabCount = tabbedPane.getTabCount(); tabbedPane.addTab(name, icon, component); tabbedPane.setTabComponentAt(tabCount, new TabHeader(tabbedPane, name, icon, tooltip)); tabbedPane.setSelectedIndex(tabCount); }//from ww w . java 2 s . co m if (show) { tabbedPane.setSelectedComponent(component); } }