I have a JTable in my application. I have a custom renderer setup on the table (a JTextArea, with line-wrapping enabled) which allows for multi-line content. The contents of the JTable cells are expected to overflow the bounds of the cell in some cases. I want to do the following:
Instead of making the user drag the row border to resize the cell, I want the user to be able to double-click the row border, so when I detect this, I can automatically resize the height of the cell (height of the row) to show the entire contents of the cell.
My question is, what is the best way to detect a double-click on the row border? I have gotten this to work by setting up a MouseListener on the JTable with a mouseClicked method that looks like this:
public class MouseButtonInputListener extends MouseInputAdapter
{
private JTable fTable;
public MouseButtonInputListener(JTable parentTable)
{
fTable = parentTable;
}
#Override
public void mouseClicked(MouseEvent e)
{
// Some pre-processing here...
if(e.getClickCount() == 2 &&
fTable.getCursor().getType() == Cursor.N_RESIZE_CURSOR)
{
// Auto-adjust row height to fit contents...
}
}
}
While this works, i'm not very happy with the line:
fTable.getCursor().getType() == Cursor.N_RESIZE_CURSOR
Any suggestions on a better way to do this? Is the above a reliable approach on all platforms?
After some searching online, I found the following page:
http://java.sun.com/products/jlf/ed2/book/HIG.Behavior.html
amongst other things, it specifies mouse cursor behavior under different circumstances. According to the section on "Pointer Feedback", the cursor switches between a N_RESIZE_CURSOR and S_RESIZE_CURSOR depending on whether it is the upper or lower boundary of a component that is being hovered over. It's interesting to note that on 2 out of 3 platforms (Mac and Windows), these cursors are exactly the same. Anyway, it follows that my code should therefore read:
#Override
public void mouseClicked(MouseEvent e)
{
// Some pre-processing here. Determine row at mouse click location
int cursorType = fTable.getCursor().getType();
if(e.getClickCount == 2 &&
(cursorType == Cursor.N_RESIZE_CURSOR || cursorType == CURSOR.S_RESIZE_CURSOR))
{
// Resize row appropriately.
}
}
This will work on all platforms. Thanks for the inputs and comments everyone.
Related
I'm working on an Java app that changes components size after it's window size changes and i have a problem with ComboBox dropdown. After window size changes, first expanding of the dropdown doesnt change it's width and i get this. When i expand the list second time, it works well, but only when i use following code:
comboBoxWindowSize.setCellFactory(new Callback<ListView<WindowSize>, ListCell<WindowSize>>() {
#Override
public ListCell<WindowSize> call(ListView<WindowSize> param) {
ListCell cell = new ListCell<WindowSize>() {
#Override
public void updateItem(WindowSize windowSize, boolean empty) {
super.updateItem(windowSize, empty);
setPrefHeight(padH25);
getListView().setPrefWidth(padW150);
if (!empty) {
setText(windowSize.toString());
} else {
setText(null);
}
}
};
return cell;
}
});
When i don't use this code, width stays incorrect all the time. I want dropdown width to be equal to ComboBox width. Will you help me fix that?
Ok, i found an answer. It's very ugly, but it actually works.
Just like i said above, it's size is incorrect only in the first expanding. So all i had to do, is to expand and collapse it programmatically after i change window size:
comboBoxWindowSize.show();
comboBoxWindowSize.hide();
And thats all.
Is it possible to control whether a column should be available in a column control popup menu? I'm aware of toggling (Disable/enable using CheckBoxList) and gray-out the column. But I do not want column entry in popup menu as The column is must-have column in Jtable. I'm using the JXTable. Anyone that have any hints?
A TableColumnExt has a property hideable which effectly disables the hiding. It is still shown in the popup and you can toggle the checkbox (that's a bug, just filed - the menu item should be disabled ;), but at least the column isn't hidden. To work around the bug, you can implement a custom column control (as Robin correctly suggested) which doesn't add the checkbox, something like:
JXTable table = new JXTable(new AncientSwingTeam());
// here the hideable property is configured manually,
// in production code you'll probably have a custom ColumnFactory
// doing it based on some data state
table.getColumnExt(0).setHideable(false);
ColumnControlButton columnControl = new ColumnControlButton(table) {
#Override
protected ColumnVisibilityAction createColumnVisibilityAction(
TableColumn column) {
if (column instanceof TableColumnExt
&& !((TableColumnExt) column).isHideable())
return null;
return super.createColumnVisibilityAction(column);
}
};
table.setColumnControl(columnControl);
table.setColumnControlVisible(true);
As to not including the menu item: when introducing the hideable property, we decided to go for keeping the item in the list but disable it because users might get confused not seeing all columns in the control. So once the bug will be fixed (just done, committed as of revision #4315), I would recommend to remove the custom column control again. Just my 2 euro-cents, though :-)
ColumnControlButton#createColumnVisibilityAction looks like the method you are looking for. According to the documentation:
Creates and returns a ColumnVisibilityAction for the given TableColumn. The return value might be null, f.i. if the column should not be allowed to be toggled
you can return null for your case.
You should be able to plug this in by using the JXTable#setColumnControl method.
First way:
myTable().getColumnExt(_column_number_).setHideable(false);
This works smooth but has one UI drawback: text in menu is gray and thick is black - bad user experience.
So try to fix it, text will be gray and thick won't be here:
public class MyTable extends JXTable
{
public MyTable(AbstractTableModel model)
{
//first two columns won't be hiddeable
ColumnControlButton controlButton = new ColumnControlButton(this)
{
#Override
protected ColumnControlPopup createColumnControlPopup()
{
return (new NFColumnControlPopup());
}
class NFColumnControlPopup extends DefaultColumnControlPopup
{
#Override
public void addVisibilityActionItems(List<? extends AbstractActionExt> actions)
{
for(int i = 0; i < actions.size(); i++)
{
AbstractActionExt action = actions.get(i);
JCheckBoxMenuItem chk = new JCheckBoxMenuItem(action);
//Disabling unwanted items but they will be still shown for smooth user experience
if(i == 0 || i == 1)
{
chk.setEnabled(false);
chk.setSelected(false);
//chk.setIcon(new ImageIcon(Icons.class.getResource("check.png")));
}
else
{
chk.setSelected(true);
}
chk.addItemListener(action);
super.addItem(chk);
}
}
}
};
this.setColumnControl(controlButton);
}
}
and if you need to hide controls for "show horizontal scrollbar", "pack" and "pack all" add into code:
//remove items for horizontal scrollbar, pack and packall
this.getActionMap().remove("column.horizontalScroll");
this.getActionMap().remove("column.packAll");
this.getActionMap().remove("column.packSelected");
right after calling super(model)
I am trying to build an SWT Tree that has icons at the top level but not at the next level.
Is there any way to avoid the blank space which seems to have been left for the image which I'm not using? I tried using the following code snippets but neither did what I wanted.
SWT.MeasureItem:
tree.addListener(SWT.MeasureItem, new Listener()
{
#Override
public void handleEvent(Event event)
{
TreeItem item = (TreeItem)event.item;
Image image = item.getImage();
if (image == null)
{
event.x -= 40;
}
}
});
SWT.PaintItem:
tree.addListener(SWT.PaintItem, new Listener()
{
#Override
public void handleEvent(Event event) {
TreeItem item = (TreeItem)event.item;
Image image = item.getImage();
if (image == null)
{
event.x -= 40;
}
}
});
In both cases I was just hoping that the text could be drawn a bit further to the left.
This behavior comes from the native controls and is OS-specific (AFAIR, on Macs you won't see this problem). Alas, no easy fix but add some generic icon (or not adding icons at all).
I have done some more investigation myself. As per Eugene's answer this seems to be native behaviour. There are a couple of things worth noting.
If no items in the Tree have an icon then no space is left for icons. However, even a single item with an icon will cause all items to leave space for icons.
A hacky solution can be implemented as follows:
Use no icons so that the native control leaves no icon space
For items where you want an icon prefix some spaces to the text e.g. " " + text
Add a PaintItem listener that draws the icon you want into the space left by the text
This probably doesn't work well across platforms and across system fonts so I've just decided to live with having icons.
I would like to customize JTableHeader so it would offer serval actions (for example 2 buttons which one of them would sort column and second show properties of this column etc). Unfortunately it is not possible to set CellEditor for JTableHeader so i'm stuck with using mouse adapter. But maybe it is possible to dispatch event from this particular JTableHeader component so it will show up a popup menu which will contains all options i desire and it would dispatch event if option other than sorting would be chosen. This way standard JTable sorting operation will be available, along with my operations and it will maintain a decent visual apperance. So my question is - Is it possible and how it should be done.
In response to trashgod comment - i understand that you mean to treat defaultheader as an ordinary component and just use "add" function to add Components. It doesnt work well with JTableHeader. After reading trashgod example i wrote this:
private class mouseList extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
TableColumnModel thisColumnModel = thisTable.getColumnModel();
int xCor = e.getX();
//int Cols = thisColumnModel.getColumnCount();
int thisColNum = thisColumnModel.getColumnIndexAtX(xCor);
int prevWidth=0;
for(int i = 0 ;i<thisColNum;i++)
{
prevWidth+=thisColumnModel.getColumn(i).getWidth();
}
int width = xCor-prevWidth;
/////////////////////////////////////////////////////////////////////////////////////
customHeader thisHeader = (customHeader)((JTableHeader)e.getSource()).getDefaultRenderer();
System.out.println(thisHeader.mainB.getText() + " text of thisHeader");
//////////////////////////////////////////////////
test thisTest = new test(null,false,thisHeader);
thisTest.setVisible(true);
///////////////////////////////////////////////////////////////
//System.out.println(width + " width of the header");
Object thisComp = thisHeader.getComponentAt(width, e.getY());
System.out.println(thisComp + "\n" + width + " + " + e.getY() +"\n" + thisHeader.getMainButton().getText());
((JTableHeader)e.getSource()).repaint();
if(thisComp instanceof JButton)
{
//System.out.println("sdfdsf");
String name = ((JButton)thisComp).getName();
if(name.equals("mainB"))
{
System.out.println("its working on main");
((JButton)thisComp).doClick(1000);
}else{
System.out.println("its working on menu");
((JButton)thisComp).doClick(1000);
}
}
((JTableHeader)e.getSource()).repaint();
}
}
MouseListener is applied to JTableHeader. HeaderRender is an extension of JPanel that contains 2 JButtons. Strange thing happens in line
Object thisComp = thisHeader.getComponentAt(width, e.getY());
When i left lines
test thisTest = new test(null,false,thisHeader);
thisTest.setVisible(true);
(This dialog shows selected component)
uncommented, function "getComponentAt" seems to work allmost fine (allmost because it never goes for else condition even when mouse is targeting second button, and it does not repaint clicked buttons[Strangely its repainting buttons in test dialog window]),otherwise it allways returns null object.
I dont know if it is important but i set Header renderer globally by invoking "setDefaultRenderer" on JTableHeader.
Im pretty much running out of ideas so i would appreciate any help.
This example shows the basic infrastructure, while this answer offers several important caveats regarding usability. This example shows how to change the RowFilter dynamically, but changing the RowSorter is similar. Both examples use JToggleButton to manage two states, but a JComboBox could be used to select from among more alternatives.
I have a find function that locates a string in a JTable with quite a few thousand entries. Michael Meyers was kind enough to help me out with the goto portion of the function. There appears to be a bug though...
When the user searches for the string the application correctly finds the line in the JTable and highlights it. It also attempts to focus on it, but does not always. Sometimes it will jump 10+ lines short of the line I am looking for and I need to scroll down to see it. As I said, there are a few thousand entries in this JTable and if I'm searching for something, it's difficult to scroll around. Is it possible to focus the selected entry in the center of the visible area?
if (logs.get(i).getLine().contains(findStr))
{
logTable.scrollRectToVisible(logTable.getCellRect(thisPos, 1, true)); // goto
logTable.setRowSelectionInterval(thisPos, thisPos); // highlight
}
I'm not sure that it helps any, but here is the JTable setup code:
JTable logTable = new JTable(logTableModel);
logTable.setShowGrid(true);
logTable.setShowVerticalLines(true);
logTable.setShowHorizontalLines(false);
logTable.setRowSorter(sorter);
logTable.getSelectionModel().addListSelectionListener(new LogRowListener());
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(logTable);
scrollPane.setPreferredSize(new Dimension(800, 450));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Thanks
EDIT
Below is a link to download a .jar file. This file is a limited version of the code that illustrates the problem. This version seems to consistently jump 2-3 lines short, that is not always the case in the full version.
Demo.jar
The code even for this demo is still a few hundred lines, so below is the sections that I believe are relevant.
public class Proto extends JFrame implements ActionListener
{
public Proto() { ... }
#Override
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (BUTTON_NEXT_FIND.equals(command))
{
findNext();
}
}
...
private void findNext()
{
String findStr = findField.getText();
int pos = selectedLogRow;
// if we're searching for the same string again step forward once
if (pos == lastFoundPos)
++pos;
// search through the log for the string
while (pos < logs.size())
{
if (logs.get(pos).getLine().contains(findStr))
{
logTable.scrollRectToVisible(logTable.getCellRect(pos, 1, true));
logTable.setRowSelectionInterval(pos, pos);
lastFoundPos = pos;
break;
}
++pos;
}
}
...
}
Sometimes it will jump 10+ lines short of the line I am looking for and I need to scroll down to see it.
Post your SSCCE that demonstrates the problem. You can just create a table with a thousand rows and then use setValueAt(...) method to populate a few random cells with the text you are searching for.
Is it possible to focus the selected entry in the center of the visible area?
You need to adjust the row number you want to scroll to. That is if you are searching down you would need to subtract "x" from the row number so the viewport is position on a row before your row that contains the text.