java: jTabbedPane tab clicked event - java

mainPane.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
mainPaneMouseClicked(evt);
}
});
private void mainPaneMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("hahahahahahhaha");
}
The code above triggers when I click anywhere in the panel, not just the tab.
How do I change it to just fire when the tab is clicked
Also, will I be overriding anything JTabbedPane is doing when a tab is clicked?

You Could...
Use a ChangeListener to detect when the tab selection changes and use something like getSelectedIndex to determine which tab has been selected
You Could...
Instead of moving the JTable from one tab to another, create an instance of a JTable for each tab and share the TableModel between them

Related

How to stop editing a JCombobox in a JTable when changing tab in a JTabbedPane

I need to add a JCombobox which is having an editable textfield in a JTable. This JTable is present in a tab of JTabbedPane.
The issue is, whenever I keep combobox text editor in edit mode and change the tab, the combobox textbox is still in edit state only. I want it to be disabled when we are changing the tab.
I have tried adding requestfocus to tabbedpane but of no use. Please suggest any way to make this textfield of combobox inactive when the tab is clicked.
Add a ChangeListener to your tabbed pane to capture tab change events. From that listener, you can delegate to a method that stops the JTable editor from editing.
ChangeListener changeListener = new ChangeListener() {
int previousIndex = 0;
public void stateChanged(ChangeEvent changeEvent) {
JTabbedPane tbPane = (JTabbedPane) changeEvent.getSource();
stopTableEditor(tbPane.getComponentAt(previousIndex));
previousIndex = tbPane.getSelectedIndex();
}
private void stopTableEditor(Component component) {
JTable table = ... // retrieve table instance from component
if(table.isEditing())
table.getCellEditor().stopCellEditing();
}
};

Setting Actor To Listen For Button Click In LibGDX

Background Information: I am currently working in a Dialog class I have extended for my game. Inside of this dialog's content table I have both an Image and a Table (lets call it ioTable). Inside of ioTable I have a combination of both Labels and TextFields. The idea is that the dialog becomes a sort of form for the use to fill out.
Next, inside of the Dialog's button table, I want to include a "Clear" TextButton (clearButton). The idea that clearButton will clear any values written to the TextFields of ioTable.
My Question: Is is possible to add a listener to each of the TextFields of ioTable that will trigger when clearButton is pressed. As always, any other creative solution is more than welcome.
You could just give the EventListener a reference to the table you want to clear:
// Assuming getSkin() and ioTable are defined elsewhere and ioTable is final
TextButton clearButton = new TextButton("Clear", getSkin());
clearButton.addListener(new EventListener() {
#Override
public boolean handle(Event event) {
for(Actor potentialField : table.getChildren()) {
if(potentialField instanceof TextField) {
((TextField)potentialField).setText("");
}
}
return true;
}
});
// Add clearButton to your dialog
If you see yourself creating multiple clearButtons, you could easily wrap this in a helper method or extend TextButton.

Using JButtons as Tabs

Could I use JButtons as tabs? Since the JTabbedPane cannot hold the same component in multiple tabs, would there be a way for a JButton to be a tab? I know it looks like tiDE(Website) uses the JButtons as a tab. How would I do that?
I could make something like this
JButton newTab = new JButton("New Tab");
newTab.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JButton tab = new JButton("Tab 1");
JToolBar.add(tab)
tab.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
makeTextAreaTab();
}
}
);
}
}
);
But, how would I make the method makeTextAreaTab()? It would have to be the same component as my other editor(JTextArea), and have the same functionality as a JTabbedPane.
You state in a comment:
I would like to be able to have buttons serve the functionality of tabs. Click on one button, it moves to one editor. Click on another button, it moves to another editor.
Consider using a CardLayout for this where your JButtons (or perhaps better, a JComboBox) tells the CardLayout-using container which "card" (which component -- here a JScrollPane/JTextArea combination) to display.

How action a button to open a new pane in java for a GUI

I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).

Toggle a component's 'enable' property according to a radio button in netbeans

I have two radio buttons in a button group and in the same panel I have a text box and a button. I want to enable the text box and the button only when the second button is selected and be disabled when the other radio button is selected. I've tried this and it didn't work.
private void radio_button2ActionPerformed(java.awt.event.ActionEvent evt) {
if(buttonGroup1.getSelection()==radio_button2)
{
button.setEnabled(true);
textbox.setEnabled(true);
}
Where have I gone wrong?
You don't want to use an ActionListener because the event only fires when you click the button. Instead you can use an ItemListener so an event is generated when the item is selected or deselected (by clicking the other radio button). Something like:
radioButton2.addItemListener( new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
JRadioButton button = (JRadioButton)e.getSource();
component1.setEnabled( button.isSelected() );
component2.setEnabled( button.isSelected() );
}
});

Categories