JTextPane: What's the renderer of/for setCharacterAttributes? - java

I have a JTextPane in my application and I prepare some styles for various words/letters. These styles are applied via setCharacterAttributes. Now I plan to style the background of some styles: If a style defines a background (say gray) and I want to soften the corners (round corners with ie. 3px).
Is there a way to add a special renderer that is used within setCharacterAttributes? Or, do you recommend HighlightPainter added via pane.getHighlighter().addHighlight(...)?

Ok, Your question is not very clear, but if I understand correctly, you intend to customize the default behaviour of the background properties on characters. You describes 2 approaches, and one is probably much easier than the other one.
1) Based on Character attributes (complex one) : You need to define your behaviour by overriding the paint method in javax.swing.text.GlyphView. Then, you will need to change the ViewFactory of your EditorKit to make it take your change into account. I would not recommend this approach.
2) Based on Highlights (easier one) : You need to define a new javax.swing.text.Highlighter.HighlightPainter that paints the round borders as you wish. Then you need to find every set of text where a background is set. You remove the background and add your custom highlighter instead. You can optimize the process, but I think you already got this part.

Related

Get foreground (font) in a particular JTable cell

There are many answers to how to set the font (use renderer), but I want to get this font. The reason why is for unit-testing: test for the renderer doing its job OK.
The human eye can see whether a component for a given cell is rendered with a red foreground, but how can we do this programmatically?
later Just a word about my comment "get hold of the screen real estate". That's not quite it: each one of these table cells contains something... but what? And is it possible to reference it? It almost seems that each one is the "ghost" of the component (the same one) used successively to render each individual cell. Obviously I don't really know about the "architecture" of the representation of a JTable. Would be interesting to find out... although I suspect strongly that these "ghosts" (or whatever) are inaccessible.
Use the JTable.prepareRenderer(...) method.
This method will return the Component that will be used to do the rendering of the cell. So you can then invoke the getForeground() method on the Component.

Override color value in color.xml file

Is there a way to override a color that is already set in your color.xml file? For example - I set the background color on the layout to backgroundColor = "#color/background" The value of background = #000000
Can I override the actual color of background in Java and set the #color/background to say - #FFFFFF?
It's possible, but rather costly. #color/background is equivalent to R.color.background, where R is your generated class. At runtime, you could use reflection to make the fields writable, then change them. I'm not really convinced it would work how you want.
A different idea would be to use themes for the various colors you want, possibly letting the user switch between them. That'd keep your design and logic separate.
You can use .setBackgroundColor(getResources().getColor(R.color.myColor)); to set the background color in your Java code. This should override the xml background when it is set in the android:background xml tag, it did on my tested table row at least.

Make all tooltips multiline in Java (Swing)

Tooltips in my application can be quite long, therefore I'd like them to have line breaks.
I don't want to use html as I'd prefer to set a (max) width of my tooltips instead and have the line breaks dynamically.
In the accepted answer the this similar question Multi-line tooltips in Java? I read about JMultiLineToolTip. Unfortunately the provided link doesn't work anymore and there are many different JMultiLineToolTip out there. Therefore my two questions:
Which JMultiLineToolTip is a good one to use?
How can I use such a class to represent all of the tooltips in my application?
EDIT: as everyone seems to recommend the use of html, is there a way to define the width of my tooltip in pixels (or some other unit than number of characters) using html?
If you are not afraid of extending swing tool tip, you can create your own JMultiLineToolTip:
Extend JTooltip In the extended Tool tip component implementation,
set a custom tool tip UI In customUI implementation
Implementpaint() method to write given string in multi line
Here is an example - it shows how to use it as well
However, to answer your questions:
Which JMultiLineToolTip is a good one to use?
Use <html>
How can I use such a class to represent all of the tooltips in my application?
Per compopnent, it is easy but tedious to achieve as you will have to override creatreToolTip() API. But if you want to change it globally, you may:
(i)Simple way - Register your custom tooltip UI with the UIManager at the beginning of your execution.
UIManager.put( "ToolTipUI", "SeNormToolTipUI" );
UIManager.put( "SeNormToolTipUI",Class.forName( multiLineToolTipUIClassName ) );
(ii) complex way
You will have to start implementing your own look and feel. In the look and feel implementation, you would provide defaults for ToolTipUI as your UI implementation and then set that look and feel to the application you are running. For instance take a look at the MetalLookAndFeel implementation. You may just extend that part and implement your on lnf.
So, it is better to use <html>
1) Html is easiest of ways for plain JToolTip
2) use JWindow(un_decorated JDialog) with JTextArea, better would be JTextPane (supporting stylled text),
the disadvantage is you have to manually set window to the Point, you have to manually set for setInitialDelay and setDismissDelay (Swing Timer), setVisible(true/false)
the advantage is that you using full manageable top level container with definitions for own parent
3) I use JLabel with Html formatted and stylled text added to the GlassPane, notice easiest alternative is use non_opaque JLayeredPane (Java6) or JLayer (Java7)

How to set all Java Swing GUI component backgrounds and foreground(fonts) colors at once?

I have tons of jbuttons, jtextfields, jlabels, jmenus, gui items and it is extremely time consuming to set the background color and foreground color one at a time.
I want to be able to color the fonts(foreground) and backgrounds all the jmenus, jmenuitems,jtextfields,jbuttons, etc quickly/concisely in my project instead of having to set them one at a time.
Is there any technique to do this more concisely instead of doing it one at a time?
1) most eficient way would be to use Custom Look and Feel, part of them have got a nice Themes
2) set value to the UIDefault, Listing UIDefault Properties
EDIT:
best of all UIManager Defaults by #camickr
You can combine Swing with CSS or use a Swing Look & Feel in order to create a standard look for your components. The Java site says:
Before we get into a CSS implementation, let's consider the alternative: a custom look and feel. Swing Look and Feels (L&Fs) are sets of classes that implement the actual drawing of components at a very low level (think lines and bitmaps). They can be swapped out for new ones at runtime, often to implement the look of a native platform; i.e., the JDK for OSX has a set of classes that make Swing apps look like native Aqua apps, with candy buttons and blue tint. Custom L&Fs are powerful, but not trivial or quick to build. You will usually have to touch 20 or so classes and implement a whole bunch of special drawing code.
So CSS is easier to use. The same article goes on to give a tutorial about how to implement the CSS with Swing. They provide a nice walkthrough of creating the right rules and then going on to implement them in CSS. However, this is not simply "copy and paste" code.
If you'd just like to use a package (without having to code it yourself) the answers to the question Can I use CSS for Java Swing? suggest Flying Saucer and Jaxx.
They're all JComponents so you can make an ArrayList of everything:
//Adding everything to the ArrayList
ArrayList<JComponent> myComponents = new ArrayList<JComponents>();
JButton b1 = new JButton("Button 1");
myComponents.add(b1);
JMenuItem item = new JMenuItem("Menu Item 1");
myComponents.add(item);
//Coloring the foreground/background
for(JComponent j : myComponents) {
j.setForeground(new Color("BLUE"));
j.setBackground(new Color("RED"));
}
If you use a Look and Feel that honors the UI constants in javax.swing.UIManager then you can just set them. There are values for e.g. panel background. If not or if you can't control the look enough by this you can write you own UI delegate that draws a specific component (e.g. javax.swing.plaf.ButtonUI for JButtons). If even this is not enough you can write your own Look And Feel. If you just extend the Metal LnF it is not that hard, you would write own UI delegates and set properties, like above, but centralized.

How can I dynamically alter the Java LAF UIDefaults?

I'm working on cusomizing a Swing application by dynamically altering the UIDefaults. The end goal is to alter many of them (colors, fonts, sizes, borders, etc) and save the result on a per user basis. While it may give the application some non-standard looks, the client asketh and the client shall receive.
The only problem I'm running into is that GUI will only update ONCE. The first time I change a ui property everthing is great, subsequent changes don't affect anything.
// called from the EDT
// uiKeyName points to some ColorUIResource
UIManager.getDefaults().put(uiKeyName, <<color from color picker>>);
SwingUtilties.updateComponentTreeUI(rootWindow);
It works once, but never again. Ideas?
When you put the new color (for example) in the table, is it a Color object, or a ColorUIResource? As I understand it, the new value will only be taken up if the current value is null or an instance of UIResource. Thus, if you insert a plain old Color object, any subsequent changes you make will be ignored.
Also, as the linked doc page suggests, this may only help with the font and foreground/background colors; when it comes to changing things like borders and margins, you may be out of luck. This is not what Swing's Pluggable LookAndFeels were designed for. Maybe you could interest the client in a Synth-based LAF?

Categories

Resources