I am trying to color the header of a jtable and I have no idea what my fault is, I appreciate the help
I leave part of the code:
// style jtable
jTable_registry.getTableHeader().setFont(new Font("Segoe UI", Font.BOLD, 12));
jTable_registry.getTableHeader().setOpaque(false);
jTable_registry.getTableHeader().setBackground(new Color(32, 136, 203));
jTable_registry.getTableHeader().setForeground(new Color(255, 255, 255));
jTable_registry.setRowHeight(25);
I'm doing the test to try to get what I want and in that simple way I can't change the background color of the header, it is still the default color ...
Thanks in advance
The code works and makes the change of background color to the table headers, the problem I found with the Nimbus visual theme which uses Painter to render the headers and make them noticeable with effects and such for which it will ignore the instruction in a certain way: setBackground, this does not happen with Metal, GTK or the one that comes with the operating system.
private void Theme() {
try {
if(UIManager.getLookAndFeel().toString().contains("Nimbus"))
UIManager.put("nimbusBlueGrey",new Color(32,136,203));
} catch(Exception e) {
e.printStackTrace();
}
}
The above modifies pre-established values within this Look And Feel through: putusing a form of Key - Value pairs.
Related
I am creating a class to handle the creation of musical notation in my music training app, which is being built with Java Swing. As such, I am using the font Bravura for most of the symbols such as the treble clef and accidentals (using the Graphics drawString method with unicode characters).
However, I am unable to find a way to draw a semibreve, or a whole note, with this method; I get a rectangle instead of the desired character when I input the sequence "\uD834\uDD5D", which should correspond to a whole note, according to my research using fileformat.info.
My code for the JFrame is below:
public MusicalNotation() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JTextArea semibreveTextArea = new JTextArea();
semibreveTextArea.setBounds(50, 100, 200, 200);
semibreveTextArea.setFont(new Font("Bravura", Font.PLAIN, 24));
semibreveTextArea.setText("\uD834\uDD5D");
contentPane.add(semibreveTextArea);
semibreveTextArea.setBackground(getBackground());
semibreveTextArea.setEditable(false);
}
However, the resulting window looks like the below:
Are there any other fonts which have this functionality, or other ways to draw a semibreve?
Any help is much appreciated. Thank you in advance.
Are there any other fonts which have this functionality?. Yes there are (several) and and one of them is the font you are already using.
The Bravura music font already contains the Semibreve and you shouldn't need to draw anything since Bravura is a font. All you need to do is provide the appropriate Unicode value for the notes, lines, and spacing you want and of course, the "\uD834\uDD5D" is valid for displaying the Semibreve note providing the Text component you are displaying the characters in has the Bravura font set to it (not all fonts support all of these Unicode music characters), for example:
try {
// Load the "Bravura.otf" font file.
Font bravura = Font.createFont(Font.TRUETYPE_FONT, new File("Bravura.otf"));
// Font Size - NEEDED! I believe default is 1.
// Set it to what you want but you may find
// size 12 too small.
bravura = bravura.deriveFont(36f);
// Set the font to a JTextArea (or whatever).
jTextArea1.setFont(bravura);
// Display the Semibreve.
jTextArea1.setText("\uD834\uDD5D");
}
catch (FontFormatException | IOException ex) {
ex.printStackTrace();
}
You should see a Semibreve within the JTextAea.
I am trying to create a JTextField with semi-transparent background (i.e. black background with alpha value of 120). My current code is:
public static void designTextField(final JTextField tf) {
tf.setBorder(null);
tf.setFont(new Font("Comfortaa", Font.PLAIN, 30));
tf.setBackground(new Color(0, 0, 0, 120));
tf.setForeground(new Color(200, 200, 200, 200));
}
However, this doesn't seem to be working. Here are the pictures this code results in (there are two text fields):
No text entered:
Text entered:
As you see there're several weird drawing bugs, and both text fields seem to be fully non-transparent. How can I fix this?
Swing perfectly works with transparency. You just need to add
tf.setOpaque(false);
https://docs.oracle.com/javase/tutorial/uiswing/painting/problems.html
section "Problem: Visual artifacts appear in my GUI" explains.
I have knowledge about C programming but I'm a newbie Java Programmer, and I want to ask a question to you. I try to set both spinners values null if they are less then "1" but I couldn't do it. I'm waiting for your helps, thank you.
Spinner spinnermin = new JSpinner();
spinnermin.addFocusListener(new FocusAdapter() {
#Override
public void focusLost(FocusEvent e) {
valuemin =(Integer) spinnermin.getValue();
if(valuemin<0){
spinnermin.setValue(null);
}
}
});
spinnermin.setBounds(478, 215, 76, 20);
frame.getContentPane().add(spinnermin);
JSpinner spinnerhour = new JSpinner();
spinnerhour.addFocusListener(new FocusAdapter() {
#Override
public void focusLost(FocusEvent e) {
valuehour = (Integer) spinnerhour.getValue();
if(valuehour<1){
spinnerhour.setValue(null);
}
}
});
spinnerhour.setBounds(362, 215, 86, 20);
frame.getContentPane().add(spinnerhour);
You state:
I try to set both spinners values null if they are less then "1" but I couldn't do it.
Much better to use a SpinnerNumberModel that doesn't allow for negative values. For example,
JSpinner hourSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 24, 1));
This creates a JSpinner that allows int values from 1 to 24, and that starts at 1.
It really doesn't make sense to assign null to a component that expects to hold a numeric value, and rather than try to do this, better to limit the user's ability to input unacceptable values. Also please have a look at the JSpinner Tutorial.
As a side recommendation, while null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
Here I am using JMonthChooser and JYearChooser. So how to Change BackGround of JMonthChooser and JYearChooser is there any Idea. how to do it.
I am using Netbeans.
I assume that you use toedter's JCalendar, that you can add to NetBeans'palette.
In this case you have to make it in 3 times for a WHITE background, 2 for other background's colors(3rd point of the belowed list is not useful in this case).
get the JCombobox (Java Component). You have to cast it into a JComboBox because the method getComboBox() returns a java.awt.Component.
javax.swing.JComboBox box = (javax.swing.JComboBox) monthChooser.getComboBox();
Modify the JComboBox's Renderer to change list's background (more examples here).
box.setRenderer(new javax.swing.DefaultListCellRenderer() {
#Override
public void paint(java.awt.Graphics g) {
setBackground(new java.awt.Color(255, 255, 255));
setForeground(java.awt.Color.BLACK);
super.paint(g);
}
});
Set the "collapsed list" (selected) background (WHITE only)
box.setOpaque(false);
Hope that help.
Actually JCalender is made of multiple components. So, if you want to change background or foreground of it, then first you have to traverse from all different subcomponents of it and then change each's background color.
In my case:
JDateChooser jdatechooser = new JDateChooser();
//to change background color : <br>
for( Component c : jDateChooser1.getComponents()){<br>
((JComponent)c).setBackground(Color.YELLOW); // whatever color you want to choose<br>
}
I know how to change size, style but how can I set colour of text in Label control? Here is my code so far:
Label myLabel = new Label(shell, SWT.NONE);
myLabel.setText("some text that needs to be for example green");
FontData[] fD = myLabel.getFont().getFontData();
fD[0].setHeight(16);
fD[0].setStyle(SWT.BOLD);
myLabel.setFont( new Font(display,fD[0]));
I see there is no colour property in FontData class.
Make sure you don't mix SWT and AWT colors, and if you build a Color object, make sure you dispose it. You want something like:
final Color myColor = new Color(getDisplay(), 102, 255, 102);
myLabel.setForeground(color);
myLabel.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e)
{
myColor.dispose();
}
});
Or you can just use the built-in system colors:
myLabel.setForeground(getDisplay().getSystemColor(SWT.COLOR_GREEN));
(Do not dispose the system colors.)
myLabel.setForeground(Color fg).
color : The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace.
For more information : see this
For green it'd be something like : myLabel.setForeground(new org.eclipse.swt.graphics.Color(getDisplay(), 102, 255, 102));