So, my question is: When select the "Perimeter" from the JComboBox, how would I go about making so that tab(held within the JTabbedPane) comes to the front? and likewise with area or other Tabs.
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
String selectedItem = jComboBox1.getSelectedItem().toString();
if(selectedItem != null)
{
//if perimeter option selected, show the tab for it
if(selectedItem.equals("Perimeter"))
{
//bring the permeter tab to the front, all contained within a tabbedPane
}
if(selectedItem.equals("Area"))
{
//bring the area tab to the front, all contained within a tabbedPane
}
}
}
You could use it as a show and not show option, so that if they select perimeter it shows the perimeter information in the tabbed pane.
frame.setVisible(true);
or if you are looking to put this tabbed pane on top of the other you can try:
Java Swing - how to show a panel on top of another panel?
I think you might be looking for this:
http://download.oracle.com/javase/6/docs/api/javax/swing/JTabbedPane.html#setSelectedIndex%28int%29
TabbedPane .setSelectedIndex(int)
Related
I'm trying now for a couple of hours to optimize my user interface but I'm not getting any further right now.
I got a JTabbedPane to show datasets. There is one textfield in there with should also represent the tabs title. Right now there is a button labeled "save" which does nothing else but read this text field from the current tab and updates the tabs title. I'd love to replace this by updating the tab's title when the field is changed. I got the event listener up and running, so no problems here, but how to I get to call the JTabbedPane object?
I tried to put a JTabbedPane variable into my JPanel class and store a reference here, but this keeps crashing the moment I call the setter for this variable...
Well, not actually crashing, but it throws an exception:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
[...]
The setter is quite simple:
public void setTabContainer(JTabbedPane cont){
container = cont;
}
Any ideas?
I got the event listener up and running, so no problems here, but how to I get to call the JTabbedPane object?
You can use SwingUtilities class as follows to get the tabbed pane that is the ancestor of your text field:
JTabbedPane tabbedPane = (JTabbedPane)SwingUtilities.getAncestorOfClass(JTabbedPane.class, textField);
Then you can iterate over the tabbed pane's components in order to find the index where your text field is placed and finally update the tab's title:
for(int i = 0; i < tabbedPane.getTabCount(); i++) {
if(SwingUtilities.isDescendingFrom(textField, tabbedPane.getComponentAt(i))) {
tabbedPane.setTitleAt(i, textField.getText());
break;
}
}
See the API for:
SwingUtilities#getAncestorOfClass(Class c, Component comp)
SwingUtilities#isDescendingFrom(Component a, Component b)
JTabbedPane#getComponentAt(int index)
Try:
JTabbedPane tabbedPane = (JTabbedPane) SwingUtilities.getAncestorOfClass(JTabbedPane.class, this);
tabbedPane.setTitleAt(tabbedPane.indexOfTabComponent(this), title);
Assumes that this is the tab component, and title is the new title. Note that you must have set this as the content for the tab.
Uses:
JTabbedPane#indexOfTabComponent
JTabbedPane#setTitleAt
SwingUtilities#getAncestorOfClass
In IntelliJ IDEA's form designer each tab is a JPanel hence you can use the following method to set the title from within Java code:
public static void setTabTitle(JPanel tab, String title)
{
JTabbedPane tabbedPane = (JTabbedPane) SwingUtilities.getAncestorOfClass(JTabbedPane.class, tab);
for (int tabIndex = 0; tabIndex < tabbedPane.getTabCount(); tabIndex++)
{
if (SwingUtilities.isDescendingFrom(tab, tabbedPane.getComponentAt(tabIndex)))
{
tabbedPane.setTitleAt(tabIndex, title);
break;
}
}
}
This solution is very similar to the one given by dic19 though.
I have a Java Swing application, and i have created a special sub JPopupMenu for it that is scrollable so a user can simply scroll through it and select one item from it, like in this screenshot :
I have used the code from this post and i have pasted it here so you can check it: http://codeshare.io/Jgqa7
Now if the user has opened this sub-menu for an item that he has already made a selection for it before from that sub-menu then i want to automatically scroll to the selected item to show it, just like the ensureIndexIsVisible(...) method for a JList. I have spent some time trying to figure this out but with no progress. So, is there a way to accomplish this ?
--------------------------------------------------------> Edit: The code i'm using :
I have tried using this code to force scroll to the item "invented" in the scrollable menu but it failed :
JScrollPopupMenu pm = (JScrollPopupMenu)myPopupMenu.getPopupMenu();
for( Component comp: myPopupMenu.getMenuComponents() ) {
if( comp instanceof JRadioButtonMenuItem ) {
JRadioButtonMenuItem rb = (JRadioButtonMenuItem)comp;
if( rb.getText().equals( "invented" ) ) {
myPopupMenu.scrollRectToVisible( rb.getBounds() ); // Does nothing.
pm.setSelected( rb ); // Does nothing.
}
}
}
For some reason it doesn't scroll to the item i want !
I needed a solution to scroll as well. Since I did not find a scrollpane, I needed to implement my own scrollRectToVisible method in JScrollPopupMenu:
#Override
public void scrollRectToVisible(Rectangle t){
// need to adjust scrollbar position
if (t.getY()<0){
// scroll up
JScrollBar scrollBar = getScrollBar();
scrollBar.setValue(scrollBar.getValue() + (int)(t.getY()));
}else if (t.getY()+t.getHeight()>getBounds().getHeight()){
// scroll down
JScrollBar scrollBar = getScrollBar();
scrollBar.setValue(scrollBar.getValue() - (int)(getBounds().getHeight()-t.getY()-t.getHeight()));
}
doLayout();
repaint();
}
Calling this with the bounds of the clicked JMenuItem (I used a mouselistener with mouseentered) scrolls the panel so that the item is visible.
Hope it helps!
I have a JFrame extended class that implements a multi-tab chat. Every tab is a chat with someone or with a group of people. What I have implemented works fine, except when I assign a ToolTipText to the label of a tab. In this case I can't click anymore (and select) the tab that has a ToolTipText assigned. The others work fine.
Graphical example:
As you can see the tabs are properly being added, and the first two tabs ("Gruppo prova" and "Gruppo test") have a ToolTipText, the other two don't. I can switch between the last two, but I can't do the same with the first two. I thought that the icon next to the label could be a problem, but I removed it and still doesn't work. However I can still click all the 'X' (close) buttons (working properly).
This is a piece of the code I used to add a tab:
// Some stuff...
JChat chat = new JChat(gui.chatClient, email, name, group);
jTabbedPane.add(email, chat); // I instantiated this before
int index = jTabbedPane.indexOfTab(email);
JPanel pnlTab = new JPanel(new GridBagLayout());
pnlTab.setOpaque(false);
// Core function
JLabel lblTitle;
if (group == 1) {
// If it's a group and not a single chat I assign a name, an icon and a ToolTipText to the tab
lblTitle = new JLabel(name, icon, JLabel.LEFT);
lblTitle.setToolTipText(membersList.toString());
} else {
// otherwise I only assign a name to the tab
lblTitle = new JLabel(name);
}
jTabbedPane.setTabComponentAt(index, pnlTab);
// This applies the 'X' (close) button next to the tab name
CloseButton btnClose = new CloseButton(this, jTabbedPane, tabs, email);
lblTitle.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
pnlTab.add(lblTitle);
pnlTab.add(btnClose);
Is this a Swing bug or am I doing something wrong?
you can use :
void setToolTipTextAt(int, String) to set tool-tip text to specific tab.
void setIconAt(int index, Icon icon) to set the icon to specific tab.
No need to use JLabel for setting tool-tip text or icon.
The above solution, however doesn't however answer your question:
except when I assign a ToolTipText to the label of a tab. In this
case I can't click anymore (and select) the tab that has a ToolTipText
assigned
The only reason i am suspecting:
JLabel doesn't register to any mouse listener by default. When no mouse listener is set to JLabel any mouse clicked event will go through to the UI objects underneath: in this case the JTabbedPane. But when we are setting tool-tip text using setToolTipText(text), the ToolTipManger adds a mouse listener to this JLabel, which will continue to consume the mouse click event.
Check the following code snippets demonstrating the issue and providing a work around setSelectedIndex function:
JLabel label = new JLabel("a Label");
System.out.println(label.getMouseListeners().length); // length is printed as 0
label.setToolTipText("Danger: setting tool tip will consume mouse event");
System.out.println(label.getMouseListeners().length); // length is printed as 1
jTabbedPane1.setTabComponentAt(0, label);
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
int index = jTabbedPane1.indexOfTabComponent((Component)e.getSource());
jTabbedPane1.setSelectedIndex(index);
}
});
With this code I am able to find what tab is selected but I need to do stuff with what is inside the tab. How do I work with the hierarchy?
EditPane.addChangeListener(new ChangeListener() {
// This method is called whenever the selected tab changes
public void stateChanged(ChangeEvent evt) {
JTabbedPane pane = (JTabbedPane)evt.getSource();
// Gets current tab
int sel = pane.getSelectedIndex();
}
});
The component that is inside the tab is a JScrollPane.
You don't need the index of the pane, you need the component selected underneath.
use getSelectedComponent() - e.g.
JTabbedPane pane = (JTabbedPane)evt.getSource();
JComponent myComponent = pane.getSelectedComponent();
To clarify your original goal, you want to manipulate the client object living in the JScrollPane. You're missing some objects.
in your JScrollPane you need to invoke getViewport().getViewportView() from the ScrollPane. (Source: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html )
# Dasdasd
I already checked it out but it only returns ViewPorts and ScrollBars
yes that correct, (probalby there you put JPanel) then you have to repeats your steps again, until as you will not find JPanel into ViewPort, that's possible get JComponents another way(s), but this is very good lesson for Hierarchy of JComponents
Component[] components = xxx.getComponents();
for (int i = 0, l = components.length; i < l; i++) {
if (components[i] instanceof JScrollPane) {
JScrollPane scr = (JScrollPane) components[i];
Component[] components1 = scr.getComponents();n
i used textarea1.setVisible(false); but still i can see the border of the text area at run time. i want the textarea to be completely invisible
Can anyone help in this issue?
It sounds like you have a Panel around your text area since setVisible(false) should definitely hide the entire component. If so, make the panel invisible. Care to post some code so we can examine and help?
You have to hide the scroll pane which your text area is sitting in. If for some reason you have no direct access to it here is the way to get it:
public static final JScrollPane getScrollPane( JComponent component ) {
Container p = component .getParent();
if (p instanceof JViewport) {
Container gp = p.getParent();
if (gp instanceof JScrollPane) {
return (JScrollPane)gp;
}
}
return null;
}
Find your textarea scrollpane, then set the visibility to false, like this:
jScrollPane4.setVisible(false);