How to insert new tab between existing tabs without removing any tab? - java

I need a method that can insert at specified index new tab and tabs after that index should go to the right.
I don't want to remove all tabs after new one and insert them back. I only want to add new tab between existing.
The code:
public class MainTabbedPane extends JTabbedPane {
private SyntaxHighlighterManager syntaxHighlighterManager;
private Map<Integer, Rectangle> tabsBounds = new HashMap<>();
public MainTabbedPane() {
this.syntaxHighlighterManager = SyntaxHighlighterManager.getInstance();
Map<String, Action> actions = MainFrame.getInstance().getActions();
Action closeTabAction = actions.get(CloseTabAction.CLOSE_TAB);
Action closeAllTabsAction = actions.get(CloseAllTabsAction.CLOSE_ALL_TABS);
Action closeAllTabsButThis = actions.get(CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS);
super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
(KeyStroke) closeTabAction.getValue(Action.ACCELERATOR_KEY), CloseTabAction.CLOSE_TAB);
super.getActionMap().put(CloseTabAction.CLOSE_TAB, closeTabAction);
super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
(KeyStroke) closeAllTabsAction.getValue(Action.ACCELERATOR_KEY), CloseAllTabsAction.CLOSE_ALL_TABS);
super.getActionMap().put(CloseAllTabsAction.CLOSE_ALL_TABS, closeAllTabsAction);
super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
(KeyStroke) closeAllTabsButThis.getValue(Action.ACCELERATOR_KEY),
CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS);
super.getActionMap().put(CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS, closeAllTabsButThis);
// super.setUI(new MainTabUI());
// TabReorderHandler.enableReordering(this);
}
/**
* Central method for adding new tab.
*
* #param title
* #param fileViewer
* #param tip
*/
private void addNewTab(String title, Container fileViewer, String tip) {
if (fileViewer != null) {
super.addTab(title, null, fileViewer, tip);
// icon is set in tabComponent MainTabComponent
super.setTabComponentAt(super.getTabCount() - 1, new MainTabComponent(title, this));
tabsBounds.put(super.getTabCount() - 1, super.getUI().getTabBounds(this, super.getTabCount() - 1));
}
}
Method addNewTab adds new tab.
Thank you!

Method in JTabbedPane:
public void insertTab(String title,
Icon icon,
Component component,
String tip,
int index)
Inserts a new tab for the given component, at the given index, represented by the given title and/or icon, either of which may be null.
Parameters:
title - the title to be displayed on the tab
icon - the icon to be displayed on the tab
component - the component to be displayed when this tab is clicked.
tip - the tooltip to be displayed for this tab
index - the position to insert this new tab (> 0 and <= getTabCount())
Throws:
IndexOutOfBoundsException - if the index is out of range (< 0 or > getTabCount())
Source:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#insertTab%28java.lang.String,%20javax.swing.Icon,%20java.awt.Component,%20java.lang.String,%20int%29

Related

Rapidclipse - multiple windows in one screen using a filter to select

Is there a possibilty to create a certain number of windows in one screen. Each window has the same layout but different content. I would like to include a filter so that I can choose which window I would like to see. Right now I have a table with four entries. For each entry one window is generated, but that looks quite messy.
final int i = this.table.size();
for (int h = 1; h <= i; h++) {
final Window win = new Window("Window" + h, new SecondView());
this.getParent().getUI().addWindow(win);
}
Edit:
The Starting point is as described. I have a table with content from a database. In this table i can select multiple rows. For each row a pre-defined window should open. In this window i have textFields, that should contain the values of the matching attributes from the selected row. In order to get a better structure i want to choose which window should be visbile.
final int i = this.table.size();
List<Window> windowList= new LinkedList<>();
for (int h = 1; h <= i; h++) {
Window win = new Window("Window" + h, new SecondView());
windowList.add(win);
}
Then later on, you can itterate the list and do addWindow(...) or removeWindow(...)with the correct window instance
Here is what you need to do:
1) fetch the selected items from the table. Not the amount of selected items, but the actual items - instances of Person if your table is of type Person.
2) iterate over these selected items.
2.a) For each item, create a new Window and pass that item into the constructor of your SecondView, so each SecondView can show values of that item or bind inputs to this bean.
2.b) Add that window to the UI
Since I am not very accustomed to the Table class I use a newer Grid instead in my example. I'm sure the table has some API to get the selected rows - use that instead.
Button openDetailWindowsButton = new Button("Open Details", click -> {
Set<Person> selectedItems = grid.getSelectedItems();
for (Person selectedItem : selectedItems) {
Window win = new Window(selectedItem.getName(), new SecondView(selectedItem));
this.getParent().getUI().addWindow(win);
}
});
Thank you for your help, but meanwhile i found another solution.
private void button_buttonClick(final Button.ClickEvent event) {
final int i = this.table.getSelectedItems().size() - 1;
for(int h = 0; h <= i; h++) {
final int personId = this.table.getSelectedItems().get(h).getBean().getId();
final Window win = new Window("Person", new SecondView(personId));
this.getUI().addWindow(win);
}
That's the button click event.
And here is the constructor for my SecondView
public SecondView(final int personId) {
super();
this.initUI();
final Person myPerson = new PersonDAO().find(personId);
this.fieldGroup.setItemDataSource(myPerson);
}
The one last problem that i have, is that for every selected person/row each window shows seperately. My intent was to only show one window where i can select between the different selected persons/rows.

Why do I need to open all expansion panes in a list view, else they give null? - Gluon JavaFX

I don't know if this is a code issue or a bug. But this picture shows us a Glisten List View where I have included Glisten DropdownButtons inside a ExpandedPaneContainer etc.
The issue here is that I need to open the first the first expansion pane, then the second one, named Layer 2, and then the third one, named Layer 3.
I cannot directly open Layer 2 or Layer 3 at the beginning. After I have open all expanded panes and close them, then I can open them again in a random order.
Question:
Why does it behave like this? Is it a bug?
If you want to run the program, just download the code from here: https://github.com/DanielMartensson/Deeplearning2C and then create a new file inside the software, by clicking the "pen"-icon. Give it a name and then go to "Configurations -> Layer Configuration". Try to open the last expansion panel at the bottom first.
Here is the method I use when I add all these expansion panes inside a expandedPaneContainer. I know there is lot of code, but the idea is that:
Create a expansion panel
Add the expansion panel into expanded panel container
Create a collapsed panel and set its labels etc.
Set the collapsed panel to expansion panel collapsed content
Create a expanded panel and set a grid pane into it
Add a labels at column 0, row j, and drop down buttons box at column 1 and row j inside the grid pane.
Repeat the process again
private void addLayer() {
/*
* Create a expanded panels from these
*/
String[] dropDownTypes = dL4JSerializableConfiguration.getDropdownTypes();
/*
* New expansions panel and add it to the expansions panel container
*/
ExpansionPanel expansionPanel = new ExpansionPanel();
expansionPanelContainer.getItems().add(expansionPanel);
/*
* Create an collapsed panel and first set a label with the name of the layer index e.g. layer 0, layer 1 etc and the layer type
*/
CollapsedPanel collapsedPanel = new CollapsedPanel();
collapsedPanel.getTitleNodes().add(0, new Label("Layer " + expansionPanelContainer.getItems().size())); // Layer index
collapsedPanel.getTitleNodes().add(1, new Label("DenseLayer")); // Layer type
expansionPanel.setCollapsedContent(collapsedPanel); // Set collapsed panel into expansion panel
/*
* Create an expanded panel and begin to set a grid pane into it
*/
ExpandedPanel expandedPanel = new ExpandedPanel();
GridPane gridPane = new GridPane();
expandedPanel.setContent(gridPane);
expansionPanel.setExpandedContent(expandedPanel);
/*
* Add all drop downs and labels
*/
for(int j = 0; j < dropDownTypes.length; j++) {
/*
* Create the element label and set its position inside the grid
*/
Label elementLabel = new Label(dropDownTypes[j]);
gridPane.add(elementLabel, 0, j); // To the left
/*
* Create the drop down button and add the elements and set its position inside the grid
*/
DropdownButton elementDropdown = new DropdownButton();
if(dropDownTypes[j].equals(dropDownTypes[0])) { // Layer types
elementDropdown.getItems().addAll(new MenuItem("DenseLayer"), new MenuItem("LSTM"), new MenuItem("OutputLayer"));
}else if(dropDownTypes[j].equals(dropDownTypes[1])) { // Inputs
for(int i = 1; i <= 100; i++)
elementDropdown.getItems().add(new MenuItem(String.valueOf(i)));
}else if(dropDownTypes[j].equals(dropDownTypes[2])) { // Outputs
for(int i = 1; i <= 100; i++)
elementDropdown.getItems().add(new MenuItem(String.valueOf(i)));
}else if(dropDownTypes[j].equals(dropDownTypes[3])) { // Activations
for(Activation activation : Activation.values())
elementDropdown.getItems().add(new MenuItem(activation.name()));
}else if(dropDownTypes[j].equals(dropDownTypes[4])) { // Loss functions
for(LossFunctions.LossFunction lossFunction : LossFunctions.LossFunction.values())
elementDropdown.getItems().add(new MenuItem(lossFunction.name()));
}
gridPane.add(elementDropdown, 1, j); // To the middle
}
}

ComboBoxCellEditor - get rid of the text box?

I have a TableViewer with a ComboBoxCellEditor. When I click into the cell, first I get a text box (similar to the TextCellEditor) with an arrow next to it. If I click the arrow, I get the drop down list with the values I put into it.
Is there any way for me to skip the text box step? I want it to open up the combo box right away when I click/traverse into the cell. Hand in hand with this is that I also don't want to allow any options other than the ones in the list.
I thought maybe this behavior is controlled by a style, but the only styles I found were
/**
* The list is dropped down when the activation is done through the mouse
*/
public static final int DROP_DOWN_ON_MOUSE_ACTIVATION = 1;
/**
* The list is dropped down when the activation is done through the keyboard
*/
public static final int DROP_DOWN_ON_KEY_ACTIVATION = 1 << 1;
/**
* The list is dropped down when the activation is done without
* ui-interaction
*/
public static final int DROP_DOWN_ON_PROGRAMMATIC_ACTIVATION = 1 << 2;
/**
* The list is dropped down when the activation is done by traversing from
* cell to cell
*/
public static final int DROP_DOWN_ON_TRAVERSE_ACTIVATION = 1 << 3;
and they didn't seem to be relevant. In fact, I set all of them, and I wasn't able to get the functionality I'm looking for.
How can I have the dropbox show without having the accompanying text box?
It's possible to change the styling of the underlying Combo widget by using the constructor:
ComboBoxCellEditor(Composite parent, String[] items, int style)
and passing SWT.READ_ONLY as style

How to detect when mouse (pointer) is over text in JList?

I am using a JList to display elements. I want to provide a popup menu to interact with the specific elements under the mouse. I am using a MouseInputListener, isPopupTrigger(), locationToIndex(), getCellBounds(), etc. I haven't posted code for this as it's not the point, just background for the question. What I ultimately want to do is only post the popup menu when the correct (platform- and UI-dependent) action occurs over the text in the JList cell - not just anywhere in the row. My JList is in a ScrollPane which is in a SplitPane. The width of the JList cells can be much larger than the text. If the user is able to post the popup by clicking far to the right of the text in the row when the SplitPane is much larger than the extent of the text, it will be unclear just which row is being operated on. I don't want to select the row that the user would be interacting with using the popup menu because selection has a different meaning in this context. So the basic question is: how can I determine if the mouse location when the popup trigger occurs is actually over the text in the row, rather than just in the row?
If the JList's cell renderer returns JLabels (which it will by default, or if you have set the renderer to a DefaultListCellRenderer), you can use SwingUtilities.layoutCompoundLabel to determine the bounds of the text:
static <E> boolean isOverText(Point location,
JList<E> list) {
int index = list.locationToIndex(location);
if (index < 0) {
return false;
}
E value = list.getModel().getElementAt(index);
ListCellRenderer<? super E> renderer = list.getCellRenderer();
Component c = renderer.getListCellRendererComponent(list, value, index,
list.isSelectedIndex(index),
list.getSelectionModel().getLeadSelectionIndex() == index);
if (c instanceof JLabel) {
JLabel label = (JLabel) c;
Icon icon = null;
if (!label.isEnabled()) {
icon = label.getDisabledIcon();
}
if (icon == null) {
icon = label.getIcon();
}
Rectangle listItemBounds =
SwingUtilities.calculateInnerArea(label, null);
Rectangle cellBounds = list.getCellBounds(index, index);
listItemBounds.translate(cellBounds.x, cellBounds.y);
listItemBounds.width = cellBounds.width;
listItemBounds.height = cellBounds.height;
Rectangle textBounds = new Rectangle();
Rectangle iconBounds = new Rectangle();
SwingUtilities.layoutCompoundLabel(label,
label.getFontMetrics(label.getFont()),
label.getText(),
icon,
label.getVerticalAlignment(),
label.getHorizontalAlignment(),
label.getVerticalTextPosition(),
label.getHorizontalTextPosition(),
listItemBounds,
iconBounds,
textBounds,
label.getIconTextGap());
return textBounds.contains(location);
}

adding items to JList

I want to add items to my List. My list is first initialized by initComponent() called automatically by instructor (I'm using NetBeans, and all GUI componenets are initialized by the prog automatically).
My questions is:
let's say that we have a Frame1, in this frame we have a Button "show images", when click on it
open Frame2 which has JList...
images list are added through Frame3 successfully...
Below is my code where i want to list all images in my list:
private void setImagesToList()
{
***//bLayer is my Business Layer and _getNomOfSelectedImg() returns number of
//images.***
int imagesCount = bLayer._getNomOfSelectedImg();
***// through my searches i fount that i've to create ListModel to hold my items***
DefaultListModel listModel = new DefaultListModel();
if (imagesCount > 0) // there is/are image(s)
{
for(int i=0; i < imagesCount ; i++)
{
// ***i want to add image name and tooltip (image path) ***
String imgName = bLayer._getImageName(i);
String imgPath = bLayer._getImagePath(i);
listModel.add(i, imgName);
break;
}
images_List.setModel(listModel);
}
}
when I run this code it throws NullPointerException in the last line images_List.setModel(listModel);
What to do to display these items, allow multi-selection, adding mouse click event?
Yes, you can add tooltips. You just have to set the tooltip text on the component returned by the renderer. The JList will use those component tooltip's to determine the correct tooltip text. This can be seen in the JList#getTooltipText implementation of which I copied the relevant part
Component rComponent = r.getListCellRendererComponent(
this, getModel().getElementAt(index), index,
lsm.isSelectedIndex(index),
(hasFocus() && (lsm.getLeadSelectionIndex() ==
index)));
if(rComponent instanceof JComponent) {
MouseEvent newEvent;
p.translate(-cellBounds.x, -cellBounds.y);
newEvent = new MouseEvent(rComponent, event.getID(),
event.getWhen(),
event.getModifiers(),
p.x, p.y,
event.getXOnScreen(),
event.getYOnScreen(),
event.getClickCount(),
event.isPopupTrigger(),
MouseEvent.NOBUTTON);
String tip = ((JComponent)rComponent).getToolTipText(
newEvent);
if (tip != null) {
return tip;
}
Could you also update your question with those new questions, as your 'answer with the new question' will float to the bottom
i found my great mistake :( :( i called the functions which set the images to the list before calling initComponent(), that's why the exception was thrown..
thnx all for your answer, but i have to more questions:
1) could i add ToolTipText to the list item, i want to add the image path
2) what did you mean about "my accept ratio"...

Categories