Hightlighting selected text with rows in Java(Swing) - java

How I can highlights(Highlighter) in swing(JTextPane) not only text(with rows) when I select two and more rows. I need to highlight selection like in Intellij IDEA.
SwingUtilities.invokeLater(new Runnable() {
JTextPane textPane = new JTextPane();
JScrollPane textPaneWithScroll = new JScrollPane(textPane);
this.getContentPane().add(textPaneWithScroll);
this.setVisible(true);
});
Screenshot

I'm guessing you want the highlight to extend to the right side of the text pane.
If so then you can use:
DefaultHighlighter highlighter = (DefaultHighlighter)textPane.getHighlighter();
highlighter.setDrawsLayeredHighlights(false);
When you add the highlight to the text pane the offset must be one greater than the last character on the line so that the newline character is included in the highlight.

Related

Add popup JTextarea to each column title in JTable

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 .

How to make JTextPane scroll horizontally

I have a JTextPane, when there are too many lines, a vertical scroll bar appears, but when a line is too long, instead of appearing a horizontal scroll bar, the line breaks into two lines, how to make the horizontal bar appear instead of breaking into two lines, my jTextPane is added like this:
JScrollPane jScrollPane1 = new JScrollPane();
jScrollPane1.setViewportView(jTextPane1);
As presented here by our very own Rob Camick, you could try using something like...
JTextPane tp = new JTextPane() {
#Override
public boolean getScrollableTracksViewportWidth() {
return getUI().getPreferredSize(this).width
<= getParent().getSize().width;
}
};
Which will stop line/word wrapping

No text area in JOptionPane window

Is there anyway to have a JOptionPane window without the text area but with only a scroll bar,
JTextArea listBox = new JTextArea(aLineFromFile);
JScrollPane scroll = new JScrollPane(listBox);
listBox.setLineWrap(true);
listBox.setWrapStyleWord(true);
scroll.setPreferredSize(new Dimension(200, 400));
JOptionPane.showMessageDialog(null, scroll, "Dictionary enteries", JOptionPane.PLAIN_MESSAGE);
I don't want the white background behind the text I just want the scroll bar.
Or anyway to make the text in the text area uneditable.
"Or anyway to make the text in the text area uneditable."...
Try:
listBox.setEditable(false);
Also, you might be better off using a JLabel rather than a JTextArea.

Java Swing Storing & Rendering Text with Tabs

My swing app contains a JEditorPane which lets users edit text in a WYSIWYG fashion, including toolbar buttons for bold, italic, and setting fonts, etc.
The content type on the JEditorPane is text/html
The problem: Users want to be able to type tab characters in the editor pane, close the edit dialog (saving HTML text to persistent storage) and see their tabs rendered. However, HTML treats all whitespace runs as a single space.
public class NoteTest {
public static void main(String[] args) {
final JEditorPane editPane1 = new JEditorPane("text/html", "Try typing some tabs");
editPane1.setPreferredSize(new Dimension(400, 300));
JOptionPane.showMessageDialog(null, new JScrollPane(editPane1));
JOptionPane.showMessageDialog(null, new JScrollPane(new JEditorPane("text/html", editPane1.getText()))); // where did the tabs go?
}
}
After typing tabs in the first JEditorPane, we get the HTML text from it, and pass it to a second JEditorPane, which renders the tabs as spaces.
How can I render these tabs? Should I be saving the user-entered content as RTF instead of HTML? Should I parse the HTML and build an HTML table with rows and cells (ugh). Or is there some way to tell swing to render the tab characters as tabs?
For better help sooner, post an SSCCE. This SSCCE does not show the behavior you describe.
Note the 'tab' between the n & g of typin g.
import java.awt.Dimension;
import javax.swing.*;
public class NoteTest {
public static void main(String[] args) {
final JEditorPane editPane1 = new JEditorPane("text/html", "Try typing some tabs");
editPane1.setPreferredSize(new Dimension(400, 300));
JOptionPane.showMessageDialog(null, new JScrollPane(editPane1));
JOptionPane.showMessageDialog(null, new JScrollPane(new JEditorPane("text/html", editPane1.getText()))); // where did the tabs go?
}
}
There were some tabs at the end that disappeared, but that makes sense, since tabs are not supported in HTML correctly unless included in a pre element. I'm guessing that the Swing HTML parsing ignores them as redundant.
What if you use < PRE > < /PRE > tags for the text and for the tab char?

removing horizontal scrollbar for textarea

I'm using Java Swing. I have a textarea in a panel. I don't need a horizontal scrollbar for that textArea, only a vertical scrollbar is needed. I disabled auto scrollbar options, but still the horizontal scrollbar is working. Please help me in thz.
ta.setLineWrap(true)
Sets the line-wrapping policy of the
text area. If set to true the lines
will be wrapped if they are too long
to fit within the allocated width. If
set to false, the lines will always be
unwrapped
Scroll bar comes into text area when you are making your text area too small. This is because your the default column number in netbeans text area is 20.
If you don't want the scroll bar being displayed , then take the textarea properties and change the column number to a value according to your size (say 10).
And the scrollbar will not be shown.
Two examples, the line wrap method and the scroll pane method:
public class Test {
public static void main(String[] args) {
// example text
String rep = "The quick brown fox jumps over the lazy dog.";
String all = rep;
for(int i = 0; i < 100; i++)
all += "\n" + rep;
// create the line wrap example
JTextArea first = new JTextArea(all);
first.setLineWrap(true);
// create the scroll pane example
JScrollPane second =
new JScrollPane(new JTextArea(all),
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// lay it out
JFrame f = new JFrame("Test");
f.setLayout(new GridLayout(1,2));
f.add(first);
f.add(second);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
JTextPane don't have a method to enable or disable line wrap, one best choice is:
private JTextPane noWrapTextPane = new JTextPane() {
#Override
public boolean getScrollableTracksViewportWidth() {
return getUI().getPreferredSize(this).width
<= getParent().getSize().width;
}
};

Categories