A Swing component to display separate, selectable strings on different lines? - java

I'm looking for a Swing component that displays several separate strings (probably from a string array or list) on separate lines within a pane or field (scrollable if there's too many to show at once). The user needs to be able to select one of these lines (double-clicking would be ideal) and thereby trigger a listener that does some magic with that line's string. Can anyone point me in the right direction?

Try a JList: http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JList.html. Can be added to a ScrollPane if you want to include scrolling ability.

I suggest you to use a JList inside a vertical JScrollPane.

The user needs to be able to select one of these lines (double-clicking would be ideal) and thereby trigger a listener that does some magic with that line's string
You can use the List Action for this.

Related

.How can I spread my text over multiple jtextpanes?

I´m building a simple word-editor in java. Currently, everything´s working fine. Now I want to create "Pages", like in word. The JTextPane representing a Page is supposed to check if it´s full and then create a new JTextpane under it. With a scrollbar I would be able to scroll between them. So far this wouldn't be problematic. However, all the pages should belong to a single document, and if I were to delete a line on let´s say page 2, every line on every other page will be moved up. (For example) Is there an easy way to do this, or will I have to create DocumentListeners for each JTextPane, changing everything on each change? Also, is there a way to extend selections over multiple pages?
Personally I havn't tried anything as of yet, since I want some tips before writing myself into a corner. I thought that I could make the pages uneditable, and instead use a caretListener to check the position the user clicks on, to edit an invisible infinite JTextPane containing the actual document, which would write it´s content to the visible pages.
Lots of unknowns, but maybe the following will give you something to think about.
all the pages should belong to a single document,
Agreed.
The JTextPane representing a Page is supposed to check if it´s full and then create a new JTextpane under it
Maybe add each text pane to a JScrollPane, but don't display the scrollbars or Border of the scroll pane.
if I were to delete a line on let´s say page 2, every line on every other page will be moved up
You would need to manually control the viewport of each scroll pane. The first page would position the viewport at offset 0. The next page would position the viewport at the offset that represents your page height.
Then any changes to the Document should be automatically reflected in all the text panes.
You would also probably need to use setAutoScrolls(false) to prevent the viewport from scrolling as you drag the mouse.
is there a way to extend selections over multiple pages?
Selection is a property of the text pane, not the Document.
Not sure what will happen as you try to drag the mouse from one text pane to another.
I'm guessing you might need some special logic. Maybe using mouseEntered/Exited events to trigger this type of processing.

Parameterizing Swing dialog

I need to create a number of dialogs that are of the same basic structure that looks like this:
There will be a varying number of rows, each with a labeled checkbox and two combo boxes that have integers, the range of which varies. The check box just enables the combo boxes. When the selection in the first combobox is changed, the second one gets initialized and enabled.
Since I have to do over 50 over these I'd like automate the programming. I believe some of the code can be handled with loops, selecting combobox names from preset string arrays. What I can't figure out is how to parameterize things like action listeners.
First question is can this be done at all. If it can, how?
Ed
First create a notional RowModel containing a Boolean value for the checkbox, a String for the label and two instances of ComboBoxModel, one for each of the combos. Handle the combo dependency as shown here. Let your program maintain a List<RowModel> for each distinct dialog. You can manage an arbitrary number of rows in a suitable TableModel and display them is a JTable as shown here.
Actually, I just want to say that MadProgrammer provided an answer that worked for me in his Comment.

Java method for easier validation for batch of Jcomponents containing null

This is a GUI Panel I'm working on. I was just thinking if there's any way to simplify the checking of empty fields, empty list and unselected checkboxes instead of writing multiple lines of
TextField1.getText().isempty()
checkBox1.isSelected()
list1.isEmpty()
TextField2.getText().isempty()
checkBox2.isSelected()
list2.isEmpty()
I want to reduce the number of lines or codes if possible. Maybe my question really is if it is possible to check whether a batch of components within a panel contains empty(null) or not. This will help me reduce the lines of codes if there's such a function or listener that does it. If there's no such method or listener, what are the other options?
There are other modules i'm working with that has more checkboxes and text fields that's why I asked.
I'd appreciate any suggestion or help.
Thanks.

Listener on text within JLabel

I'm building a program in Java, using Swing, that will act as an interactive presentation.
I have paragraphs I need to display (presumably in JLabels) , and within each paragraph are certain words and phrases that need to be formatted differently (have a different color), and I need them to call a method that will display something else when clicked or hovered over.
I know there must be a way to accomplish this...
If you want to apply some style, you can use HTML in your JLabel's content.
If you need some custom behavior and you want to handle it in a different way for some parts of your JLabel, you need your component to be split in a more detailed way.
Create a container(JPanel) and arrange you components within it. From now, your smaller components will be able to listen for events like mouseEntered and mouseClicked and handle them separately, not confusing the whole JLabel component.
In this way, every smart part of text will be a standalone component.

Display a row of Strings on a JComponent so that the single Strings are selectable/their location getLocation()-able?

In order to be able to display a sentence on a, say, JPanel with a GridLayout(1,0) [i.e., only one line/row] and then be able to draw a syntax tree (or similar) above it, I want to display the sentence as a row of Strings, which each include one word.
The single Strings should then be either selectable (as in a JList), or I should at least be able to get their Location on the JPanel via getLocation().
Up to this point I have tried the following options, and had the following issues:
- Single Strings as JLabels: The JLabels are stretched out to fill the JPanel width, re-sizing them to fit the single String they're displaying seems complicated. I would want to be able to do this, however, to make the sentence look like a sentence and not like a badly layed out table.
- JList: All the functionality I want, but I'm unaware of an option to re-size the "cells" of a single String (cf. JLabel above). Also, I'm having difficulties restricting display of the JList to a single line/row (cf. another of my questions).
- JTextArea: I couldn't get my head round how to get the Location of the single Strings that I had appended to the JTextArea.
I'm aware that drawString() might be an option, but I'm afraid to use it since I don't want to mix AWT and Swing. Also, I would need to calculate the int values for x and y for every single String. And I'm not sure whether I'd be able to get their Locations at all (although I could of course save their ints in a Map or Vector since I have to calculate them anyway).
Thankful for any suggestions! Thanks!
I would use JTextArea and method modelToView()/viewToModel() to get x,y for position in nthe string and position in the string for coordinates x and y.
Also use Utilities class getWordStart() getWordEnd() getRowStart() getRowEnd() methods.
EDIT: As noted by camickr in the comments, setSize() is not an appropriate way to lay out Components (as this is automatically done by the respective LayoutManager, I have removed the respective code from my answer.
Triggered by StanislavL's answer, I have found a solution to do it via JTextField, albeit by using one for each String rather than just one (as suggested by StanislavL).
I can now easily getLocation() for each JTextField. Simple, really!
I'd like to thank StanislavL for his answer, without which I'd never have though about this, and camickr for his comment.

Categories

Resources