how to set font weight in Java for Swing components - java

I want to set different font weights for components on my JFrame dialog. How do I do this?
In the below Java statement
setFont(new Font("Dialog", Font.BOLD, 12));
when I use Font.BOLD it is too bold and when I use Font.Plain it is too plain. I want something in-between.

welle is partially correct. You can use TextAttributes to obtain a font:
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.FAMILY, Font.DIALOG);
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
attributes.put(TextAttribute.SIZE, 12);
label.setFont(Font.getFont(attributes));
A better approach is to derive your font from the font installed on the Swing component by the look-and-feel:
Font font = label.getFont();
font = font.deriveFont(
Collections.singletonMap(
TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD));
label.setFont(font);
That will preserve the font's family and size, which users may have set in their desktop preferences for readability reasons.

maybe i wrong but i think class Font has only Bold ,plain but you can change after that in number
setFont(new Font("Dialog", Font.BOLD, 12));
setFont(new Font("Dialog", Font.plain, 27));
but in class java.awt.font.TextAttribute
you have WEIGHT_BOLD and WEIGHT_SEMIBOLD ...

The solution is to load by name the variant of the font, for example:
Font font = new Font("Segoe UI Semibold", Font.PLAIN, 12);

Related

Change Font of JOptionPane showMessageDialog Boxes

I have been trying to change the font of the boxes within showMessageDialog, but I am unsure how to do so. Here's the code I have so far:
Object[] choice = {"Previous", "Next"};
value = JOptionPane.showOptionDialog(
null,
"<html><body><b style='font-family: Sitka Display; font-size: 14px'; <p style='width: 400px;'>" + text,
"Header Test",
JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
choice,
choice[0]);
Output
Thanks in advance.
You only need to create a JLabel to achieve this.
JLabel label = new JLabel("...");
label.setFont(new Font("Verdana", Font.PLAIN, 14));
And pass it to showOptionDialog as a parameter.

In JFreeChart, how to position the Domain Axis Label next to the Legend?

Essentially, I have a chart that looks like this:
but I want the Domain axis label to show on the left, on the same line as the legend, on the right:
so I can better utilize the small space I got.
My chart source looks like this:
Color lightGray = new Color(200, 200, 200);
Color gray = new Color(150, 150, 150);
JFreeChart chart = createChart(createDataset(), yAxisTitle, xAxisTitle);
chart.setBackgroundPaint(Color.BLACK);
chart.getPlot().setBackgroundPaint(Color.BLACK);
chart.getPlot().setInsets(new RectangleInsets(0,2,2,2));
chart.getXYPlot().setBackgroundPaint(Color.BLACK);
chart.getXYPlot().getDomainAxis().setAxisLinePaint(lightGray);
chart.getXYPlot().getDomainAxis().setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getDomainAxis().setTickLabelPaint(lightGray);
chart.getXYPlot().getDomainAxis().setLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getDomainAxis().setLabelPaint(lightGray);
chart.getXYPlot().getRangeAxis().setAxisLinePaint(lightGray);
chart.getXYPlot().getRangeAxis().setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getRangeAxis().setTickLabelPaint(lightGray);
chart.getXYPlot().getRangeAxis().setLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getRangeAxis().setLabelPaint(lightGray);
chart.getTitle().setFont(new Font("Arial", Font.PLAIN, 12));
chart.getTitle().setBackgroundPaint(Color.BLACK);
chart.getTitle().setPaint(lightGray);
chart.getLegend().setItemFont(new Font("Arial", Font.PLAIN, 10));
chart.getLegend().setBackgroundPaint(Color.BLACK);
chart.getLegend().setItemPaint(gray);
chart.setTextAntiAlias(true);
chart.setAntiAlias(true);
chart.getLegend().setHorizontalAlignment(HorizontalAlignment.RIGHT);
panel.add(new ChartPanel(chart, false));
How can I accomplish this? Is there a way?
Thank you!
Found it.
To move the axis to the desired position, I used:
chart.getXYPlot().getDomainAxis().setLabelLocation(AxisLabelLocation.LOW_END);
Then to move the legend up in the same line, I had to adjust it's padding to use a negative value, which essentially moves it up, like so:
RectangleInsets padding = chart.getLegend().getPadding();
chart.getLegend().setPadding(new RectangleInsets(padding.getTop() - 20, padding.getRight(), padding.getBottom(), padding.getLeft()));
chart.getLegend().setBounds(chart.getLegend().getBounds());
This results in what I was looking for:
Hope this helps someone else out there!

Is there a way to change JOptionPane.showMessageDialog font?

Is there a way to change the JOptionPane.showMessageDialog font? I'm trying to write code for a message dialog and I need to change the font if it's possible.
JOptionPane jopt = new JOptionPane();
String result;
result = "your message";
JLabel resLabel = new JLabel(result);
resLabel.setFont(new Font("Monospaced", Font.BOLD, 50));
jopt.showMessageDialog( null, resLabel, "Results", JOptionPane.PLAIN_MESSAGE );
Create a new label with your custom font and set the jlabel as a component in joptionpane.

Java JTable - Transparence with Nimbus/System L&F

I have this JTable:
List<MyTuple> l= new ArrayList<>();
l.add(new Element(1,"One"));
l.add(new Element(2,"Two"));
l.add(new Element(3,"Three"));
l.add(new Element(4,"Four"));
l.add(new Element(5,"Five"));
l.add(new Element(6,"Six"));
l.add(new Element(7,"Seven"));
JScrollPane pane = new JScrollPane();
pane.setOpaque(false);
pane.setBorder(new EmptyBorder(0, 0, 0, 0));
pane.setBackground(new Color(0,0,0,0));
pane.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
pane.setBounds(3, 101, 707, 297);
MyTableModel tm=new MyTableModel(l);
table = new MyTable(tm);
table.setBorder(new EmptyBorder(0, 0, 0, 0));
table.setOpaque(false);
//table.getTableHeader().setBackground(new Color(0,0,0,0));
table.getTableHeader().setOpaque(true);
table.getTableHeader().setBorder(new EmptyBorder(0,0,0,0));
table.setBackground(new Color(0,0,0,0));
table.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
table.setFillsViewportHeight(false);
table.getTableHeader().setFont(new Font("Segoe UI Semibold", Font.PLAIN, 13));
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
pane.setViewportView(table);
pane.getViewport().setOpaque(false);
table.setGridColor(Color.GRAY);
table.setShowGrid(true);
getContentPane().add(pane);
MyTuple, MyTable and MyTableModel classes don't set any custom rendering.
This is how my table looks like under System L&F (which I really like):
This is how my table looks like under Nimbus L&F (note the added big rectangle):
I want my table to look like the one with System L&F.
The problem is that if I set one L&F like I want, the other gets ugly, I want a sort of "compatibility", is there a way to do so?
Maybe using the UIManager.put() method?
You forgot to set the border around the viewport of the scrollpane. Possible solutions:
pane.setViewportBorder(null);
pane.setViewportBorder(BorderFactory.createEmptyBorder());
pane.setViewportBorder(new EmptyBorder(0, 0, 0, 0));
Note that pane.getViewport().setBorder(...) will not work, because a JViewport doesn't actually have a border, setViewportBorder just draws a border around the viewport. (Reference)
To make the table header transparent:
pane.setColumnHeader(new JViewport());
pane.getColumnHeader().setOpaque(false);
table.getTableHeader().setOpaque(false);

Jdatechooser text alignement to RIGHT

I need to align the text in Jdatechooser text field. It always aligns the text(the selected date) to LEFT but i need at RIGHT side.
I have tried this but its not working,
StartJCal.setAlignmentX(RIGHT_ALIGNMENT);
The setTextAlignment method is not available for Jdatechooser.
StartJCal = new JDateChooser();
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));
This is the piece of code am using.How can i align the text please help
StartJCal = new JDateChooser();
JTextFieldDateEditor dateEditor = (JTextFieldDateEditor)StartJCal.getComponent(1);
dateEditor.setHorizontalAlignment(JTextField.RIGHT);
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));
try this. it will solve your problem
Similar but not passing through getComponent()
StartJCal = new JDateChooser();
JTextFieldDateEditor dateEditor = (JTextFieldDateEditor)StartJCal.getDateEditor();
dateEditor.setHorizontalAlignment(JTextField.RIGHT);
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));

Categories