How can I select a tab as if it was clicked by clicking on a button?
I have googled and looked at all the actions but there are just just so many... :(
Anyone know off hand?
Thanks in advance!
Add an action listener to the button that calls setSelectedComponent, or setSelectedIndex on the JTabbedPane.
I'm not sure what you mean about the button, but you might be looking for setSelectedComponent or setSelectedIndex.
If your jtabbedpane's name is mytabbedpane it goes like this:
mytabbedpane.getSelectedIndex();
which returns the int of that tab (0,1 .. n) or
mytabbedpane.getSelectedComponent();
which returns the String of the tab's name ("Firts tab","Second tab",...).
If you want to use the "getSelectedComponent()" for boolean logic you should write something like:
if (mytabbedpane.getSelectedComponent().equals("First tab")) {
//code here
}
and for the "getSelectedIndex()" one is of course:
if (mytabbedpane.getSelectedIndex() == 0) {
//code here
}
Double click on button, put the follwing code
JTabbedPane.setSelectedIndex(1);
Tabs starts from 0 to N left to right order
Try this code:
tabbedPane.addTab(tabName, component);
int count = tabbedPane.getTabCount();
tabbedPane.setSelectedIndex(count-1);
Related
I have a JInternalFrame, where I wanted to display a JOptionPane when my JTable was double clicked. I looked around in the internet and found that the only way of doing it was to override mousePressed() method and this is how I did it:
tblJobs.addMouseListener (new MouseAdapter() {
#Override
public void mousePressed (MouseEvent e) {
JTable tbl = (JTable)e.getSource();
int row = tbl.rowAtPoint(e.getPoint());
if (e.getButton() == MouseEvent.BUTTON1 && row != -1) {
if (e.getClickCount() == 2) {
JOptionPane.showMessageDialog(null, "Double click detected");
}
}
}
});
The thing is, I also have a button to hide the frame, and when the frame was hidden and re-shown, I found that upon double clicking my JTable, the JOptionPane got displayed twice. The number of times the JOptionPane got displayed seemed to increase along with the number of times I hid and show the frame. My guess is, the mouseListener got called again and again when I hide and re-show my frame. Is there a way of removing the mouseListener that was added in this way? Or is there another way to stop the JOptionPane from displaying more than once? And also, if what my code was stupid in any way, feel free to tell me! :) Thanks in advance!
How about setting a Boolean calss variable hidden = true when you hide, hidden = false when you unhide ?
Set the first line in mousePressed() to if(hidden) return;
Can somebody give me a hint because I am stucked. And I haven't found the appropriate solution for my problem.
I have a Grid, with 1-3 rows. I click on the row -> the row is selected.
I want to have this row to be deselected after I click somewhere else (outside this row) but inside the grid.
Here is simple screenshot, to help you visialize it better.
What kind of listener should I use for this case? I have tried ItemClickListener -> haven't helped.
Try putting your grid in a separate layout and add LayoutClickListener to it:
gridLayout.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {
#Override
public void layoutClick(LayoutEvents.LayoutClickEvent event) {
if(grid.getSelectedRow() != null) {
grid.deselectAll();
}
}
});
gridLayout.asSingleSelect().addSelectionListener(e->{
if(e.getFirstSelectedItem().isPresent()) {
system.out.println("selected");
}else {
// when deselected make some actions here
system.out.println("deSelected");
}
});
Selection:
If you click on the same row of data for the first time , it will print selected ,since there are values present on what you clicked .
Deselection:
Now try to click on the same selected row for the second time , now it will go to deselection mode. since there are no values when you deselected.Now no need to click anywhere on the grid ,you can click on the same row of data for two times for the selection and deselection.
So I'm trying to make a little game for Android. It's about turning lights on and off. I have 25 ImageButtons and, when I press one, I need the ones that are adjacent (up, left, down, right) to change too.
The thing is that I can't find a way to get/identify this buttons.
I tried using "view.getX()" and "view.getY()" but don't know how to implement them the right way (if there is one).
Here's the code I use to change ONE button:
public void change(View view)
{
String state = view.getTag().toString();
if (state.equals("on"))
{
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lightOff));
view.setTag("off");
lightsOn -= 1;
txtNumLights.setText(String.valueOf(lightsOn));
}
if (state.equals("off")) {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lightOn));
view.setTag("on");
lightsOn += 1;
txtNumLights.setText(String.valueOf(lightsOn));
}
}
I use the tags to know if it's On or Off and set a txtView with the number of ON lights.
But, to the point, how do I get the ones adjacent to this one?
Thanks in advance.
You have to store the buttons on a bidimensional array.
When you click on one button, you set the tag and the background on the buttons you are interested on.
Pseudocode:
If (button_matrix[row][column] is clicked) then
Set_tag_background (button_matrix[row-1][column])
Set_tag_background (button_matrix[row+1][column])
Set_tag_background (button_matrix[row][column-1])
Set_tag_background (button_matrix[row][column+1])
Set_tag_background (button_matrix[row][column])
...
On the implementation you should take care about not generating ArrayOutOfBoundsException...
/*code inside of 1st button*/
private void btn1......{
double a1,a2,result;
a1=Double.parseDouble(jTextField1.getText());
a2=Double.parseDouble(jTextField2.getText());
result=a1+a2;
jTextField3.setText(String.valueOf(result));}
/*code inside of 2nd button*/
private void btn2......{
double a1,a2,result;
a1=Double.parseDouble(jTextField4.getText());
a2=Double.parseDouble(jTextField5.getText());
result=a1+a2;
jTextField6.setText(String.valueOf(result));}
/*code inside of 3rd button*/
private void btn3......{
double a1,a2,result;
a1=Double.parseDouble(jTextField3.getText());
a2=Double.parseDouble(jTextField6.getText());
result=a1+a2;
jTextField7.setText(String.valueOf(result));}
So i am adding 2 numbers with the first button and another two with the second button and i want with a third button to take the sum of them but when i click on 3rd button doesn't show anything on textfield why?
why do u have a '.' operator after parseDouble?
a2=Double.parseDouble.(jTextField2.getText());
Correcting that would help.. gud luck
I find that using Double.valueOf(String str) works smoother. Perhaps try that.
Then when you put it into a field as text, just field.setText("" + doubleNum);
Of course, this will all go to the dogs if the values in the text fields are not actually numbers... so perhaps add some error checking for that?
I have a JTextField represents a day in a week, such as "Friday", when I click on it, I want to have a choice such as "1st of month, 3rd of month or last of month", so I came up with two options :
<1> Hold down a number or letter, let's say "2" or "L", then click on "Friday" means 2nd (or last) Friday of the month, in this case, how to get the number while mouse clicks on the JTextField ?
<2> Right mouse click on the "Friday" JTextField, drop down a menu, with either buttons or checkboxes that let me choose, then close the menu and get the value.
My code look like this so far :
private final JTextField[] dayHeadings=new JTextField[]{new JTextField("Su"),
new JTextField("Mo"),
new JTextField("Tu"),
new JTextField("We"),
new JTextField("Th"),
new JTextField("Fr"),
new JTextField("Sa")};
......
for (int ii=0; ii < dayHeadings.length; ii++)
{
dayHeadings[ii].setEditable(false);
dayHeadings[ii].setFocusable(false);
dayHeadings[ii].addMouseListener(new MouseAdapter() { public void mouseClicked(final MouseEvent evt) { onHeadingClicked(evt); } });
add(dayHeadings[ii],new AbsoluteConstraints(x,38,X_Cell_Size+1,Y_Cell_Size+1));
}
......
void onHeadingClicked(final java.awt.event.MouseEvent evt)
{
final javax.swing.JTextField fld=(javax.swing.JTextField) evt.getSource();
...
}
How to do either of the above, are they doable in Java ?
getModifiers is actually that what I needed. a sample for the modifiers can be found
here
Option 1:
There is no way to do this in one step. You would need to add a KeyListner to track whenever a key is pressed and then save the character value. Then you would need to add a MouseListener to listener for mousePressed events. When the mousePressed event fires you would need to to check which character is saved and then do your processing. Therefore your listener would to implement both the KeyListener and MouseListener interfaces.
Option 2:
You need to add a mouse listener and listen for a right mouse click, then display a popup menu.
I think option 2 is more intuitive and more easily done. Its always easier to work with one hand then be forced to use two hands.
Another, lazier way to do it would be using getModifiers() on the mouseclick event. It shows which modifier keys (ctrl, alt, shift, etc), if any, were pressed during the mouse click. Using these buttons isn't as intuitive as a drop down menu or numbers in my opinion, but could work.
Read more here