JTabbed Pane
I have a JTabbedPane with 2 tabs on it. When i launch my program the wrong tab comes up by default. How can I chage what tab is opened at run time. What code do I need for this?
Thanks
No. I use this code in several places.
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setSelectedIndex(2);
In fact, here goes an example of using this:
package test;
import java.awt.FlowLayout;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.KeyStroke;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
public class TestBug4 extends JFrame {
JTextField textField;
JComboBox comboBox;
JTabbedPane tabbedPane;
public TestBug4(String name) {
super(name);
JPanel panel1 = new JPanel(new FlowLayout());
textField = new JTextField(10);
panel1.add(textField);
JPanel panel2 = new JPanel(new FlowLayout());
JTextField textField2 = new JTextField(15);
panel2.add(textField2);
JPanel panel3 = new JPanel(new FlowLayout());
comboBox = new JComboBox(new String[] {"One", "Two", "Three"});
comboBox.setEditable(true);
panel3.add(comboBox);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("e = " + e);
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println("e = " + e);
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab 1", panel1);
tabbedPane.addTab("Tab 2", panel2);
tabbedPane.addTab("Tab 3", panel3);
getContentPane().add(tabbedPane);
public void setDefaultFocus() {
tabbedPane.setSelectedIndex(2);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// textField.requestFocus();
comboBox.requestFocus();
private class MyTabbedPane extends JTabbedPane {
public MyTabbedPane() {
ActionMap actionMap = getActionMap();
actionMap.put("SelectLeft", _leftNavigationAction);
actionMap.put("SelectRight", _rightNavigationAction);
InputMap inputMap = getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,
ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK), "SelectLeft");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,
ActionEvent.ALT_MASK + ActionEvent.SHIFT_MASK), "SelectRight");
public static void main(String[] args) {
TestBug4 test = new TestBug4("TestBug");
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.pack();
test.setLocation(100, 70);
test.setVisible(true);
test.setDefaultFocus();
}
Similar Messages
-
X JPanels in a JTabbed pane.
I'm wondering if anyone can help.
I have an application consisting of a JTabbed pane, within this there should be 'x' JPanels. Each JPanel has 3 RadioButtons and 5 Text fields. I need to be able to refer to each radio button and text field as RB0 - RBx and TF0 - TFx.
It is obviously easy enough to do this for just 3 or even 20 (despite a lot of coding), however, my problem is getting a variable number (x) of Panels with respective variable numbers of TF and radio buttons.
Simply I need to produce an array of panels, radiobuttons and textfields....
anyone anyideas?another way of looking at it is to think how can I make 5 of exactly the same panel (just with different names) in the same tabbed frame in just one bit of code that is called 5 times - or x in this case.
-
2nd time posting: Flashing JTabbed Pane?
I'm trying to use a JTabbed Pane wherein tabs are created dynamically from the server. In order to alert the the user of the client software that a new tab has been created, I would like the top of the tab to flash or blink with a different color. any ideas how to do this? I really do want to avoid doing stuff like writing my own component etc.
Tia,
carlosYou can use:
tabPane.setBackgroundAt(i, Color.red);to set the background of tab number "i" to red, and
you can use:tabPane.setBackgroundAt(i, tabPane.getComponentAt(i).getBackground());to set the background of tab number "i" back to its original color. If you do these in a loop with a delay, then you have a flashing tab.
You can also change the font color if you want by using tabPane.setForeground(...) instead of setBackground(...) -
How do I set FocusListener to fire with I select a specific tab in a JTabbed pane? If I put the FocusListener on the Tabben pane it fires rquardless of which tab I select and if I try to use an if statement to single out a specific tab nothing happens.
If you are interested in listening to when a tab is selected you can use a ChangeListener:
JTabbedPane tab = ...
tab.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
// get the selected tab-index:
int index = ((JTabbedPane)e.getSource()).getSelectedIndex();
});Btw, Swing related posts should be posted in the Swing forum. -
hello...
How can I put an Icon into the JTABBED PANE?
So every TAB will have its own Icon?
I believe JBuilder 4.0 do that.
Thank you.Read http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html
tabbedPane.addTab("Title", image, panel, "Tooltip"); -
JCombobox on a jtabbed pane, AND Choice on jtabbed pane problems
I have a Choice on a jtabbed pane, on a panel that isn't showing when the jtabbed pane is made visible. The Choice on the hidden pane is "bleeding" through to the first tabbed panel. help!
If I click on the second tabbed panel (where the Choice is), and then click on the first tabbed panel, the Choice becomes hidden like it is supposed to.
AND, I have a JComboBox on a JTabbedPane, it works for the most part, but when you click on it, the scrollbox underneath it doesn't appear! Instead, a grey box appears below it. Help!I was having similar problems when I was working with JFrames, Anything not covered by a piece of GUI would show other parts of my program (like the progress bar or buttons and stuff).
Anyway, it went away when I used the setBackground() method. My guess is that using this forces the container to be opaque or something. Anyway, it worked for me, maybe it will work for you also.
Steve -
Is there a way to make a JTabbed Pane transparent, include the tabs?
What do you mean? A JTabbedPane simply displays the currently selected component. In order for the whole thing to be transparent, the components contained in the JTabbedPane also need to be transparent.
-
Jtabbed pane is listening to the keyboard, how do i stop?
hello
this is probobly a simple Swing problem. i have a j3d canvas inside a tabbed pane with other info on other panes. i need the keyboard to listen on the canvas so i can move around the j3d world, but the left and right are being detected by the jtabbed pane, but i didnt add any listen to those. how can i stop this behavior from occuring? its very annoying.
i appreciate any help you can lend..The reason it is responding to keyboard events is becuase it has the focus. Request focus on the component in the tab, and the tabbed pane should stop responding. If that doesn't work, try this:
JTabbedPane tabbed = new JTabbedPane();
KeyStroke[] keys = tabbed.getRegisteredKeyStrokes();
for (int i=0; i<keys.length; i++) {
int code = keys.getKeyCode();
if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT) {
tabbed.unregisterKeyboardAction(keys[i]); -
Jtabbed pane problem + flags printout
Hi,
I am having difficulty displaying a JTabbedPane which is contained by a dialog. When I display the Dialog for the first time the Tabbed displays perfectly - anytime after that it disappears.
I found this really strange so I put in some printout statements for the JTabbed pane here's what I got
When it displays the prinout for the JTabbed pane is ............................
extCellTabbedPane
rnh.adj.cellhandovermgt.gui.refactor.ExtCellTabbedPane[,0,0,0x0,invalid,layout=javax.swi
ng.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout,alignmentX=null,alignmentY=null
,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,haveRegistered=false,t
abPlacement=TOP]
When it does n't display the prinout for the JTabbedPane is
rnh.adjcellhandovermgt.gui.refactor.ExtCellTabbedPane[,0,67,193x45,inv
alid,layout=javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout,alignmentX
=null,alignmentY=null,border=,flags=16,maximumSize=,minimumSize=,preferredSize=,
haveRegistered=false,tabPlacement=TOP]
What I am interested in is the meaning of the parameter flags. In one case it is 0 and in one case it is 16? What does this parameter mean? Where I can get some information on this as I think this will tell me what is happening to the JTabbedPane.
Thank you in Advance
AlexThe flags item reflect the flags value in JComponent
class. It represents several status
of the component:
REQUEST_FOCUS_DISABLE
IS_DOUBLE_BUFFERED
ANCESTOR_USING_BUFFER
IS_PAINTING_TITLE
HAS_FOCUS
IS_OPAQUE
As my understand about your problem, this may be no
help for you to locate the error
As the message you post, you might use a third
implementation of JTabbedPane, so the
problem might be in the implementation. I
have used JTabbedPane often with no error
like you say :)
Hope this Helps!Thanks for your help here - I would like more information if possible i.e. a numerical mapping for each property - do you know where I could get this. I don't really understand what you mean about the Third implementation. Do you mean simply extend the JTabbedPane? Thisis what I do but I don't really know what part to change to stop it disappearing. Thanks once again. Anymore help would be greatly appreciated. -
JTabbed pane customize title on selection
Hi all
i want to change the title font when a tab is selected.I add a ChangeListener to my JTabbed pane to intercept tab selection and to change font via html.
I have some proble ex:
if i click on tab4, it correctly change font but the tab2 take the title of the tab1 and so on.
This is the code
ChangeListener changeListener = new ChangeListener()
public void stateChanged(ChangeEvent changeEvent)
JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.
getSource();
int index = sourceTabbedPane.getSelectedIndex();
for (int i = 0; i < sourceTabbedPane.getTabCount(); i++)
if (i == index)
sourceTabbedPane.setTitleAt(i,
"<html><b>" + sourceTabbedPane.getTitleAt(i) + "</html>");
else
String desc = sourceTabbedPane.
getTitleAt(i).replaceAll("<html>", "").
replaceAll("<b>", "").replaceAll("</html>", "");
sourceTabbedPane.setTitleAt(i, desc);
System.out.println("***********************");
for (int i = 0; i < sourceTabbedPane.getTabCount(); i++)
System.out.println(sourceTabbedPane.getTitleAt(i));
};The last system.out show me the correct tirtle, only the selected with html but on screen i see some mixed title.Where i'm wrong?I hope can you help me:
public static void main(String[] arg)
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Tab1", new JPanel());
jTabbedPane.addTab("Tab2", new JPanel());
jTabbedPane.addTab("Tab3", new JPanel());
jTabbedPane.addTab("Tab4", new JPanel());
jTabbedPane.addTab("Tab5", new JPanel());
jTabbedPane.addTab("Tab6", new JPanel());
jTabbedPane.addTab("Tab7", new JPanel());
jTabbedPane.addTab("Tab8", new JPanel());
ChangeListener changeListener = new ChangeListener()
public void stateChanged(ChangeEvent changeEvent)
JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.
getSource();
int index = sourceTabbedPane.getSelectedIndex();
for (int i = 0; i < sourceTabbedPane.getTabCount(); i++)
if (i == index)
sourceTabbedPane.setTitleAt(i,
"<html><b>" + sourceTabbedPane.getTitleAt(i) + "</html>");
else
String desc = sourceTabbedPane.getTitleAt(i).replaceAll(
"<html>", "").
replaceAll("<b>", "").replaceAll("</html>", "");
sourceTabbedPane.setTitleAt(i, desc);
System.out.println("***********************");
for (int i = 0; i < sourceTabbedPane.getTabCount(); i++)
System.out.println(sourceTabbedPane.getTitleAt(i));
jTabbedPane.addChangeListener(changeListener);
JDialog dialog = new JDialog(new JFrame(),true);
dialog.getContentPane().add(jTabbedPane);
dialog.setPreferredSize(new Dimension(300,300));
dialog.setSize(new Dimension(300,300));
dialog.setVisible(true);
} -
Hi,
I would like to ask one question - In a JTabbed pane which has WRAP_TAB_LAYOUT , the tabs will be shown in more than one rows when number of tabs is more -When I click a tab in upper row, that row will come down - Is there any way to override this default behaviorur - that is the tabs should reamai in same position when I click
Message was edited by:
anikuts
Message was edited by:
anikutsIf you force a particular L&F you can do this. For example, if you force the basic L&F you can create a subclass of BasicTabbedPaneUI which uses a subclass of BasicTabbedPaneUI.TabbedPaneLayout which overrides calculateTabRects.
-
the panels that i am using in JtabbedPane load images in background at run time when i seperately run them images are loaded but when the panels are used in tabed pane images are not visible
I have overriden the function . what i am using for my panels is the Image panel which is derived from panel class and i create an instance of that image panel on my panel that is to be added on jtabbed control but it is not showing
-
How to add a line between the two text fields in JTabbed Pane
Hi Friends,
I would like to add a Line between some text fields to seperate them.. I added those text Fields to a tabbed Pane.. Please help me to create a line between the TextFields......http://java.sun.com/docs/books/tutorial/uiswing/components/separator.html
-
Hello,
I am wondering is there anyway to create a Table View and attache that to a tab in a tabbed pane?
I am confused, because always Table extends from JFrame. (It seems like it is a stand alone application). If I create the table this way, how can I put this in a tab of a tabbed pane?
LaliYou can always include a JTable in a JTabbedPane.
A component can hold another component within it.
Put the table view inside a JScrollPane,by which you can get a scrollable view of the JTable. -
Firefox style jtabbed pane questions
hey
i am wondering if it is possible to create in java tabbed pane that can be closed independentaly
for example firefox tabs, they can be closed by an X button on the rightI'm having trouble with the JTabbedPane too. I cut and pasted this right from the Sun tutorial on the JTabbedPane, but when I try to run it under Eclipse, it won't compile. It gives me
1) Syntax error on token(s); misplaced construct(s)
2) Syntax error on token ItemPanel, VariableDeclaratorId expected after this token
3) Syntax error on token "Items"; invalid FormalParameterList
<code>
import javax.swing.*;
import javax.swing.SwingConstants;
public class TabContainer
JTabbedPane tabbedPane = new JTabbedPane();
ItemDataPanel itemPanel = new ItemDataPanel();
tabbedPane.add("Items", itemPanel);
</code>
This is EXACTLY what the tutorial says to do. Every other source says the same thing. What is wrong with it? -
Hi,
i have a JTabbedPane with 8 tabs displaying 8 different panels. on one tab, a calendar view is provided with the option to select date range. each day is represented by a button with a small image.if there is some event on one particular day for a given entry that button is highlighted with a colored image.my problem is tha t if we select a longer date range ie a period of 10 years , the calendar view is shown. but if we switch tabs, it will take some time to display the tab with calendar view.(for 10 years 365 X 10 X 24 buttons will be there).but if we switch the tabs for a few times(2 -3 times ),then it will display within a fraction of the second. if the number of buttons is less, then the tabbed pane is switching tabs easily.what is the problem? why its taking time initially to switch tabs to show the big panel? ChangeListener is not implemented for the JTabbedPane.should i change the design to limit the no: of buttons in the panel? ie using previous /next buttons to browse list? please help
thanks and regards
Anoop Kshould i change the design to limit the no: of buttons in the panel? ie using previous /next buttons to browse list?Thats the way I've seen most calendars work. I've seen implementation where you have a button to move forward one month and another button to move forward one year.
Maybe you are looking for
-
Can't start the tns Lisenter -oracle xe 11g installed on window 7 64 bit
Hi I have followed the below website's advise installed xe 11g R2 on my window 7 64 bits. http://blog.mclaughlinsoftware.com/2011/09/13/oracle-11g-xe-installer/ It worked at the time when I installed. but the next day, I restart the pc , then I can a
-
How to move the custom hooks in /etc/rc.d/functions.d to systemd
I have a few scripts placed in /etc/rc.d/functions.d that were launched during the boot process. Now I moved to systemd but I have not found any documentation about how to implement those hooks in the new boot process. As far as I got I need to write
-
Query on object-relational data takes forever
hello all i have a problem with a query performance... it seems like whenever i call a specific object function, the query executes very very slow. The results though are correct. let me explain what i do... I have some relational tables, and i recre
-
We are in process of implementing Multiple assignments for a clients despite the assignment carries same Payroll and GRE .Could anyone list on the impact of having multiple assignments , any document id to review, Basically to take a stock of bad eff
-
How easy is it to optimise site made in iWeb? easily add alt and title tags, change page titles, descriptions etc? Does anyone experienced with making a few sites think that it is just a blog simple tool for people to make personal home pages or can