JFileChooser set multi selection mode - java

I am attempting to use a JFileChooser to select multiple files. I know I can setMultiSelectionEnabled(true) but this selects all the files between the first file clicked and the second. Is there a way to make it only select the files that are clicked? I am looking for something similar to the MULTIPLE_INTERVAL_SELECTION in JList.

You can do so by holding Control (Ctrl) button while clicking the selections. The selection behaviour is standard behaviour and cannot be controlled programmatically.

Related

Java AWT Robot - how to read item text / label?

I need to select appropriate option from the popup window. Unfortunately, it is not the popup displayed by the web page, so I cannot use Selenium framework for that purpose.
As I checked, it is possible to navigate to different option by pressing arrow keys. Thus keyPress and keyRelease​ from Java AWT Robot should work for me perfectly to select option and confirm the selection by pressing Enter key.
Unfortunately, I do not see a method to read currently selected item text. Without that I cannot find appropriate option. Is it possible to read item label using Java AWT Robot?
If the target application is another java application, then you should be able to get a reference to the component hierarchy and traverse it until you find the text field & label in question.
Assuming it's not a java application, then I don't believe it's possible to do this - at least directly. Robot just provides mouse / keyboard input, but you could use it combined with other classes in the toolkit to at least partially solve your problem.
Use the Robot methods keyPress & keyRelease to input CTRL-A. Then CTRL-C.
Once the text is in the clipboard, you could read it using Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor)
You might be able to use the same approach for a text label, assuming it is selectable.
This question is talking about using java.awt.Robot to alter text in another program (MS excel), but it might provide you some additional ideas for tackling the problem:
How can I select text in another application with java.awt.robot (example: shift+home)

JCheckBoxes inside JComboBox

I have added checkboxes inside combo box in Java. But when I open drop down menu and check one check box, the drop down menu closes. So to select each check box I have to open it every time.
Is there any way so that I can keep the drop down list opened till the time I dont click outside so that I can select any number of check boxes at one time only.
Please help!!
I have added checkboxes inside combo box in Java. But when I open drop
down menu and check one check box, the drop down menu closes. So to
select each check box I have to open it every time. Is there any way
so that I can keep the drop down list opened till the time I dont
click outside so that I can select any number of check boxes at one
time only.
no there isn't, this is default property of (BasicXxx)Popup implemented in Swing API, workaround for series of Bugs in Java1.4_xxx
no_way, only by using dirty hacks, usage of can be Java Version sensitive, or required left mouse button as accelerator
don't do that, another way (and proper of possible ways) is usage of JWindow/undecorated JDialog but required to override ESC Key and Focus lost in Windows three (as you can see in good Java JCalandars/JDatePickers),
I recommend that you to use the Japura API to deal with this, check this link:
http://www.japura.org/checkcombobox
Best Regards :)

Dynamic Jframe and Jtable

I am coding a small script and stuck on this issue. This is my scripts screenshot.
In ... button it should be automaticly open the browse and the user can select the directory.
In addition If the user click the + Button , the script should create a new path place and the user can select the second path. The problem is how can I add the second or third path place to the gui without damage general view. Like Show Me button has to always seems in gui.
For this problem what is your advice ? Should I use first JScrollPane and then Jtree? or more easy way?
I need your helps and some tricks.
Thanks in advance.

GWT- PopupPanel with search?

I am looking for a way to create a popup dialog box when a user double clicks a textinput field that will contain a scroll-able list (from database table) where the user can select a field, hit ok, and have it placed into the textbox when popup closes.
The other major requirement is to have a filter/ or search field in the popup to aid the user in finding the correct option to select from quicker.
What is the best way to implement this?
Modification to gwt's popup panel? maybe a JOptionPane? are there any simple solutions already designed for free commercial use?
You could implement this with a com.google.gwt.user.client.ui.PopupPanel. You can make a PopupPanel that contains a ListBox with your data from the database, along with a OK button. When a user selects a value and hits OK, you should utilize an EventBus along with a custom Event that will pass the value to the field on the page. The page will have an event handler that will catch the event and put it into the field.
Another option is to use a com.google.gwt.user.client.ui.SuggestBox. It is a box that autocompletes / suggests values as you type, kind of like the Youtube search bar.
I can offer more resources to help you accomplish this, if you'd like.

JTable that can save to a file

Does anyone know of a JTable based Swing component OR code example that can save to a file? i.e: provides a menu item or button that when clicked on prompts the user for a file location and saves the table's contents to a file (CSV, XLS, TXT, or whatever).
The easy part is looping through the rows and saving to a file. But there also needs to be a UI component ON the table itself that allows the user to initiate the save.
Write your own. All you do is use the table.getModel().getValueAt(...) method and loop through the rows and columns and then write the data to a file.
I have implemented this using the following approach:
Create an Action implementation: DelimitedExportAction. I typically add this action to a JToolBar, JMenuBar or JPopupMenu.
Expose a method void registerTable(JTable tbl). Internally this method adds a FocusListener to the JTable. When a given JTable gains focus set the tableToExport instance variable and enable the action.
When the actionPerformed(ActionEvent) method is called, invoke your export logic if tableToExport != null.
Rather than iterate over the TableModel I recommend iterating over the JTable, and for each row calling getValueAt(int, int) on the underlying TableModel, remembering to convert between view and model row / column indices. This is more intuitive to the end user as it means any sorting and filtering applied to the JTable is preserved during the export. (In my implementation I actually offer users the choice of exporting all data or visible data.)
Define a DelimitedExportFormatter whose role is to convert each object returned by getValueAt(int, int) to a String. Similar to when providing renderers to a JTable this class should permit you to provide a Format for a given Class or specific column, whereby a column specific format takes precedence. The default format applied to all other values should be: value == null ? "" : value.toString().
I allow the action to be configured in different modes (which also governs the action's icon):
Export to file: Launches a file chooser dialog allowing user to specify save destination.
Export to Excel: Saves the exported data as a temporary file and opens it within Excel.
Configurable export: Launches a dialog whereby the user can specify: row delimiter, field delimiter, columns to export, rows to export (all, visible, selected), save destination (Excel / File).
I'm afraid I can't provide the code as it is proprietary but hopefully this will put you on the right track. Good luck!
I recently created a very simple tutorial that exports data from JTable into excel file, using Tab-Separated Values(TSV) format. The application provides an Export button, which then triggers a dialog box (JFileChooser) to assist the user in specifying the file location/destination. I hope this helps somehow.
https://sites.google.com/site/teachmemrxymon/java/export-records-from-jtable-to-ms-excel
I don't know of any JTable-like Swing component that fulfills this exact need.
However, where would you expect the button to be placed on the table? In my opinion you would be better served by either adding the JTable to a JScrollPane and putting your "save" button on the JScrollPane or adding the JScrollPane to a JPanel and putting the "save" button on the JPanel. I don't see the logic behind having a button on the JTable itself.
If you want a menu item, you'd probably want to create the menubar and add the JTable to whatever container is holding the menubar. There's still no adding of a button to the table itself, mind you, but it would be the same thing visually.

Categories

Resources