Add popup JTextarea to each column title in JTable - java

I am trying to make a JTable with popup help menus for each column section. For instance, you right click on the first column title and a JTextArea pops up that explains what the column is for and what type of data should be put into it. I have the following code establishing the JTable and the mouselistener event. Is there a way I can write an If statement using ColumnAtPoint() so that if the right click happens at Column 1, then it opens up my JTextArea? Then I can create a second and third separate JTextAreas for my other columns.
final DefaultTableModel tblModel = new DefaultTableModel(null, colHdrs);
final JTable table = new JTable(tblModel);
table.getTableHeader().addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e))
{
JOptionPane.showMessageDialog(null, textArea1, "Type", JOptionPane.PLAIN_MESSAGE);
}
}
});

Try to use JToolTip I think it might be much more suited for your use ;) :)!
You can also add e.g. to a
JLabel label = new JLabel("My Number Label");
a tooltip text like :
label.setToolTipText("Only Numbers From 1-10 are allowed!");
This is also possible for other swing stuff, you can try it :).
The text will appear as soon as you hover over the label .

Related

How to clear contents of selected cells in JTable?

I use mouse to drag a selection area in some JTable cells, the selection area is the yellow, so can anyone told me exactly how to clear the contents of selected cells by pressing "Delete" key in keyboard or a JButton?
The captured pic of selected cells:
Create an Acton to find the selected cells and clear the text. The easiest way is to loop through each cell in the table.
The basics of the Action would be something like:
Action clearAction = new Action()
{
#Override
public void actionPerformed(ActionEvent e)
{
for (each row in the table)
for (each column in the row)
if (table.isCellSelected(...))
table.setValueAt("", ...);
}
}
Then you create a button to invoke the Action:
JButton clearButton = new JButton( "Clear" );
clearButton.addActionListener( clearAction );
If you also want to use the Delete key then you can use Key Bindings to share the same Action.
The basic logic to add a new Key Binding to the JTable would be:
String keyStrokeAndKey = "DELETE";
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke, keyStrokeAndKey);
table.getActionMap().put(keyStrokeAndKey, action);
Check out Key Bindings for more information.

How to add right-click popup menu in jTable row?

I have a simple code for Add button like this :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.addRow (new Object[] {name.getText(),mobile.getText()});
}
This code will get text from JTextField and insert into JTable row
I want to add function right-click popup menu when i right-click on table row, and add some like add delete rename.
How can I do that ?
Start by reading the section from the Swing tutorial on How to Bring up a Popup Menu for the basics of displaying a menu and a working demo.
In the case of a JTable, you will probably want to highlight the row that was clicked on so your Actions can act on the selected row.
So you would need to add code like the following to the maybeShowPopup(...) method from the demo example in the tutorial:
if (e.isPopupTrigger())
{
JTable source = (JTable)e.getSource();
int row = source.rowAtPoint( e.getPoint() );
int column = source.columnAtPoint( e.getPoint() );
if (! source.isRowSelected(row))
source.changeSelection(row, column, false, false);
popup.show(e.getComponent(), e.getX(), e.getY());
}

Why Vaadin ignors first component

I am new to Vaadin.
I want to create a page where on the left side there are three options. Depending on the option selection combo boxes will appear on the right side which is specific to the option on the left. I want the combobox with label.
Name: "Combo box menu"
So far I can click the option on the left side and that displays the combo box on the right side but could not display the labels with the combo box. Label is a component and so is the combo box. When I add both to the panel. Expecting both label and combo box to showup but the last one (Combo Box) ignores the first one (Label). I am not sure why is that. I would really appreciate if someone could help me with that.
Thanks
Here is my code:
HorizontalLayout hLayout;
Panel hpanel;
HorizontalSplitPanel hsplit;
VerticalSplitPanel vsplit;
tree.setImmediate(true);
tree.addItem("OP1");
tree.addItem("OP2");
tree.addItem("OP3");
hsplit.setFirstComponent(tree);
tree.addListener(new Component.Listener() {
public void componentEvent(Component.Event event) {
Object o = tree.getValue();
System.out.println("selected = " + o);
showWindowtab(o);
}
});
hpanel.setContent(hsplit);
hLayout.addComponent(hpanel);
hLayout.setSpacing(true);
final VerticalLayout main = new VerticalLayout();
main.setMargin(true);
setContent(main);
main.addComponent(hLayout);
private void showWindowtab(Object itemClicked) {
Label label = new Label("Here is example of Combo Box");
hsplit.setSecondComponent(label);
String document[] = { "X", "Y", "Z" };
ComboBox cb = new ComboBox();
cb.setInputPrompt("Select values");
cb.setInvalidAllowed(false);
cb.setNullSelectionAllowed(false);
for (int i = 0; i < document.length; i++) {
cb.addItem(document[i]);
}
cb.setImmediate(true);
setFocusedComponent(cb);
hsplit.setSecondComponent(cb);
}
You call setSecondComponent() on both label and cb, so the second may override the first.
Maybe what you wanted was setfirstComponent(label)?
Another option is to call addComponent(label) on the component where you want to see your label.

Changing the colour of JTable cell border on starting editing (not on click, only when cursor appears)

I am trying to change the border of a JTable cell on starting of editing, as in: when the text cursor appears. How would you do it?
For that purposes you can write your own TableCellEditor or use DefaultTableCellEditor.
With second way you can do it with this code (table is your table):
for(int i =0;i<table.getColumnCount();i++){
table.getColumnModel().getColumn(i).setCellEditor(getCellEditor());
}
and code of getCellEditor() method:
private TableCellEditor getCellEditor() {
JTextField f = new JTextField();
f.setBorder(BorderFactory.createLineBorder(Color.RED));
return new DefaultCellEditor(f);
}
Here I use DefaultCellEditor with JTextField which has red border.
I think it helps you.

Can't switch between tabs having ToolTipText assigned (JTabbedPane)

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);
}
});

Categories