A part of Text should be bold in jList - java

can i set a part of the text in a jList bold?
in some component i can set the text with html-marks to bold but not here.. is there any other way to do this?..

The default font is bold for a JList (in the Metal LAF). So you would first need to change the default font and then add your HTML string to the ListModel to only bold the text that you want displayed bold. Something like:
String[] items = { "one", "<html>normal <b>bold</b> normal</html>" };
JList list = new JList( items );
list.setFont( list.getFont().deriveFont(Font.PLAIN) );
If you have problems then post your SSCCE demonstrating the problem.

You should be able to tie into the ListCellRenderer. Since the DefaultListCellRenderer extends JLabel, I'd expect there to be some way to wedge in HTML that's getting passed over in the default usage.

Have you tried creating a custom list cell renderer yet? If not, you may wish to give this a try. The tutorials will show you how. Please have a look here:
http://download.oracle.com/javase/tutorial/uiswing/components/list.html
http://download.oracle.com/javase/tutorial/uiswing/components/list.html#renderer
http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

Related

Can i set different name from the value of a JcomboBox in netBeans

First of all I'm sorry if I cannot formulate this question correctly but my problem is as follows. I have a JcomboBox used in a file search program. In the jcombobox you have to chose the type of the object and so I did something like this.
type = new javax.swing.JComboBox();
type.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ".jpg", ".png", ".gif", ".psd", ".ppt", ".pptx", ".doc", ".docx" }));
I made a JcomboList whith ".jpg", ".png", ".gif", ".psd", ".ppt", ".pptx", ".doc", ".docx" but I didn't like the look of the JcomboBox.
My question is this, can I assign a different name to the elements of the Jcombobox, different from their value in NetBeans?
You need to store a custom Object in the combo box. This object will contain two properties:
a value for display in the combo box
a value used for processing in the application.
Then you need to renderer the Object. Two approaches
Override the toString() method of the custom object to display the appropriate value. Check out Combo Box With Hidden Data for a working example using this approach.
Create a custom renderer to display the appropriate value. Check out Combo Box With Custom Renderer. This would be the preferred approach as long as you use the included rendering class. This provides a more complete solution than you will find in the Swing tutorial, since the tutorial example break default combo box functionality.
x.setEditable(true);
x.setValue(str);
x.setEditable(false); //If You Want

How to format a table in JOptionpane?

So, I have a program which searches through a file to find desired information that the user wants, and I am trying to output it in a nice table in JOptionPane to make it easier to read, but I keep getting something like this:
I would like the lines to line up accordingly and be formatted properly, here is the code for that particular message:
String message=String.format("%-10s|%7s|%14s|%13s|%22s", element,symbol,atomicNumber,atomicMass,valence);
JOptionPane.showMessageDialog(null, "Name |Symbol |Atomic Number |Atomic Mass |# of Valence Electrons \n _________________________________________________________________ \n " + message);
Can't seem to figure out what i'm doing wrong here, I used the exact same format code in a different assignment in which it only utilized the terminal and it worked fine, but now that I'm trying to use it in JOptionPane it's not formatting properly.
Any ideas on how to fix this or make it work?
Try JTable instead:
Object[][] rows = {
{element,symbol,atomicNumber,atomicMass,valence}
};
Object[] cols = {
"Name","Symbol","Atomic Number","Atomic Mass", "# of Valence Electrons"
};
JTable table = new JTable(rows, cols);
JOptionPane.showMessageDialog(null, new JScrollPane(table));
The difference between the terminal and your Swing dialog is that the former using a fixed width font, while the latter uses a variable width font. For example: if you look at the picture you posted, you can see that the 'i' is far less wide than the 'm'. That means that a String 15 characters long does not necessarily take up the same space as another String of the same length.
The correct solution to format something like this in Swing is to use either a JTable or a GridBagLayout. If you are happy to use a third party library I recommend looking at DesignGridLayout.
You could also set the font explicitly, but that would still require you to change your code and it would look a bit archaic in the context of a Swing application.
Any ideas on how to fix this or make it work?
In the context of a brute force solution for the JOptionPane you should be able to specify an Array of Objects to be used by the option pane. So the code would be something like:
JLabel[] labels = new JLabel[3];
JLabel heading = new JLabel(...);
heading.setFont( ("monospaced", Font.PLAIN, 12) );
labels[0] = heading;
JLabel line = new JLabel(...);
...
JLabel data = new JLabel(...);
...
JOptionPane.showMessageDialog(null, labels);
This is not a very good solution, but I just wanted to demonstrate the flexibility of JOptionPane to display multiple components organized vertically.
Another option might be to play with the UIManager and change the default font for the option pane. The code would be something like:
Font original = UIManager.getFont("Label.font");
UIManager.put("Label.font", new Font(...)); // specify your monospaced font here
JOptionPane.showMessage(...);
UIManager.put("Label.font", original); // restore default font
I'm not sure if this font applies to all the components on the option pane or just the buttons or just the display components.
The JTable would be the better approach for my money.

Changing the font of a text in a JTextPane dynamically

I want to change the certain texts written on a JTextPane dynamically. I have a String array containing the words which should be changed
String ListMethod [] = {"forward", "backward", "left", "right"};
I've gone through some posts and many suggest to use JTextPane or JEditorPane to edit text but most of the answers given work on static text. I want to do it in a way such that as I type "forward" or "backward", etc... in the textpane, it detects this words and changes the color. How can I go about it?
Thanks for your help.
See here how to implement a DocumentListener Value Change Listener to JTextField. The have a look at javax.swing.text.Highlighter and javax.swing.text.HighlightPainter.
You have to capture the appropriate event and perform actions. For example in your case you can create an ActionListener that changes color and use registerKeyBoardAction to attach it on your JTextPane.
Oracle has a good tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/index.html on event listeners. I suggest you start getting yourself familiar there

how to set text to a jcombobox, but the text set should not be an element of jcombobox

is their any way to set text for a combo box, which is not an element of the combobox.
cboSubjects=new JComboBox();
cbo.addItem("Maths"); // and few more subjects are added
cbo.setSelectedItem("subjects"); // this does not set the default text of combobox
Is their any way to solve this problem ? I need something which works like combobox.text
property of combobox in visual basic
I am working on school management system. I need help.
I'd like to show you a possible alternative :)
Note, you need to call label.setDisplayedMnemonic('s'); and label.setLabelFor(combo) to complete the effect!
You can make jComboBox editable, cbo.setEditable(true);, and after that set the text you want: cbo.setSelectedItem("subjects");

Get font of selected text in JEditorPane

Basically the question says it all;
I have a JEditiorPane with the content type 'text/html'. I have created a font family and font size combo box and enabled them using the StyledEditorKit actions. This works great and I can change the font attributes of selected text (you what it does...)
The only thing is when I select the text it's a bit confusing because the ComboBox's still show the users previous selection. I would love to implement the functionality to update the combo box values according the selected text but cannot figure out how to get the selected texts font! I've tried all sorts of crafty workarounds - non of which have worked
Any Help appriciated
Thanks In Advance
Andy
Add a CaretListener. On each caretUpdate use the code
AttributeSet attrs=((StyleEditorKit)editorPane.getEditorKit()).getInputAttributes()
StyleConstants.getFontFamily(attrs);

Categories