Swing components : vertical text question

If I have a button like the one in this image :


how could I make the text display itself vertically ? As in


I would like to know how to do the same thing for JLabel . I'm sure there has to be a better way than to create as many labels as there are characters in the string . Right ?

EDIT: how can I insert an image into my post ? The button for the image shows the image in the preview section , but when I actually post the data , I only get some text back , like the tags are getting messed up .

Asked by: Abigail837 | Posted: 23-01-2022

Answer 1

I don't know if this is useful to you, but this sample shows how to make a vertical label. The difference is that it rotates all of the text (and/or the icon) instead of stacking the letters. I don't know how difficult it would be to modify it to do what you're asking.

The advantage of doing it this way is if your labels can change at runtime; generating big HTML strings might be a pain compared to a simple label.setUI(new VerticalLabelUI(true)); (and you don't have to worry about <> in the label text).

Answered by: Emily638 | Posted: 24-02-2022

Answer 2

You can use HTML for JButton or JLabel. So

button = new JButton("<html>J<br>b<br>u<br>t<br>t<br>o<br>n<br>1</html>");

should do the trick.

Answered by: Emily335 | Posted: 24-02-2022

Similar questions

swing - Java - how to center vertical column of components?

It's very simple, what I want to do, but I can't figure out a way to do it. In a JFrame or JPanel, how do you center components vertically? That is, analogous to using the center tag in HTML. The components are in a column and they are all centered. I have tried BoxLayout, with Y_AXIS and PAGE_AXIS, but it aligns the components in a strange way for me. I have tried to use FlowLayout with preferred size set so it w...

java - Adding vertical distance to components in gridBag Layout

I am adding four components in gridbagLayout. I want to add vertical distances between them how to add vertical distance? components are datfeild (Jlabel) billno (JTextFEILD) date (JTextField) search JBUTTOn) delete(JBUTTOn) I tried by making widthy=1.0; but not getting. Please help me.. thank you.. enter code here b() { GridBagLayout gridBag = new GridBagLayout(); GridBagCons...

java - How do I have multiple components respond to a single event in JSF?

Here's the sit: I have a JSF component which is basically a list of 'documents' I have any number of document viewer components on the same page. None of these components "know" about each other. In other words, they cannot be configured at design time to link to each other or anything like that. When the user clicks a document link I wish each one of the document viewer compo...

java - Do I need to free Swing components before letting them get garbage collected?

Wnen I use external resources such as files or DB connection I need to close them before I let them go. Do I need to do the same thing with Swing components ? If yes then how ?

user interface - Java GUI Creating Components

I hope it is correct term-wise to say that components in a GUI is like JButton, JPanel, JTextField, all that good stuff. I want to create a text field that takes in an integer. Then a submit button can be pressed and based on the integer that was inputted, create that many textfields in a popup window or whatever. I have no clue, could someone get me started in the right direction? The trouble I'm h...

swing - Java get JPanel Components

I have a JPanel full of JTextFields... for (int i=0; i&lt;maxPoints; i++) { JTextField textField = new JTextField(); points.add(textField); } How do I later get the JTextFields in that JPanel? Like if I want their values with TextField.getText(); Thanks

JAVA Swing GUI Components howto RTL view?

How can i make my Java Swing GUI Components [Right To Left] for Arabic language from NetBeans Desktop Application?

user interface - Java: How do you style Swing components?

I'm writing my first Java project. I was wondering if it's possible to style the Swing components (set a background color, remove the borders from the buttons, etc.)

java - Swing components inside HTML

Does anybody know of a pure java (ar at least cross platform) open source component/library which displays HTML which can contain swing components inside it? All I could find were either abandoned or incomplete. I am asking because I'm thinking about trying out approach to do use this combination for creating desktop application. EDIT:Seems like this question is missunderstood, so let me clarify it....

user interface - Correct way to use Actions to create menus, toolbars, and other components in Java

The naive way of writing building a menu in a Java Swing app is to do something like: JMenu fileMenu = new JMenu("File"); JMenuItem openItem = new JMenuItem("Open..."); openItem.addActionListener(new ActionListener() { /* action listener stuff */ } ) fileMenu.addMenuItem(openItem); A more experienced developer will recognize that actions can be accessed through a variety of mechanisms - me...

java - ADF Custom Components

Where is the best place to find custom compnonents? Ideally a repository, as opposed to finding a few here and there. We are currently about to re-design our look and feel UI for about 200 forms and we were hoping to find a lot of custom components... We were told they were everywhere by the pre-sales architect but even a simple

java - Testing EJB 3.0 components

I've got a couple of questions concerning the integrated testing EJB 3.0 components using JUnit. In your JUnit tests you can inject to session beans resource-local entity managers and therefore "simulate" ejb-container. But what if... What if you need to test if transaction attributes on your ejb methods are handled properly? Can this be done outside the container? If not, what is the simplest way...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)