Groovy SwingBuilder : controlling the style of the

2019-06-06 14:25发布

问题:

I can't find a way (is there any ?) to format the style of tabs titles in a JTabbedPane.

I can change the background color of the tab panes (see below), but can't find a way to style the titles of the tabs; I would like to have them bold or red or be able to define the tabs width, for instance, like I could format the style of the labels in the first panel.

Here's the code, mostly inspired by tim_yates (Groovy SwingBuilder : using a scrollpanel to show a list of panels) :

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JTabbedPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import java.awt.Font

Font font = new Font("Serif", Font.BOLD, 13) 
int numPanels = 20

swing = new SwingBuilder()
frame = swing.frame(title:'test', pack:true,   visible:true, defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {
    tabbedPane(id: 'tabs', tabLayoutPolicy:JTabbedPane.SCROLL_TAB_LAYOUT) {
        panel(name: 'Tab 1', background:java.awt.Color.WHITE ) {
            boxLayout(axis: BXL.Y_AXIS) 
            panel(alignmentX: 0f, background:java.awt.Color.WHITE){
                label ( 'Label 1', preferredSize: [104, 24]).setFont(font) 
                label ( 'Label 2', preferredSize: [104, 24]).setFont(font) 
                label ( 'Label 3', preferredSize: [104, 24]).setFont(font) 
            }   
            scrollPane( verticalScrollBarPolicy:JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) {
                vbox (){
                    (1..numPanels).each { num ->
                        def panelID = "panel$num"
                        def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
                            label('description') 
                            textField( id: "description$num", text:panelID, columns: 70 )
                            button( id: "buttonpanel$num", text:panelID, actionPerformed:{
                            swing."$panelID".background = java.awt.Color.RED
                            } )
                        }
                    }
                }
            }
        }       
        panel(name: 'Tab 2', background:java.awt.Color.WHITE) {
            textField(text: 'Some text', columns: 15)
            scrollPane() {
                textArea(text: 'Some text', columns: 15, rows: 4)
            }
        }       
    }
    boxLayout(axis: BXL.Y_AXIS)
    panel(id:'secondPanel', background:java.awt.Color.WHITE){                       
        button('Quit', actionPerformed:{
        dispose()
        })
    }   
}
frame.size = [ frame.width, 600 ]

I found these links which look very difficult (to me) to implement in Groovy :

  • JTabbedPane: icon on left side of tabs

  • How to make JTabbedPane autoresize to fit page dimensions?

  • Controlling Color in Java Tabbed Pane

Also the Java docs do not explain how to do that, and I didn't find any example using styled tabs.

Thanks for your help.

Regards,

Michel.

PS : Ant's offered a link

Groovy SwingBuilder : changing size and/or font of tabs (in jTabbedpane)

to an interesting article, but not directly helpful for my question (initially too vague).

回答1:

I believe you need to call jtabbedpane.setTabComponentAt (the javadoc for which can be found here)

This requires you be using Java 6 (for Java 5, you are going to have to look into writing a custom TabbedPaneUI class, and overriding this -- or use a custom JTabbedPane class from some other source that allows this)

Here's an example of it in action:

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JTabbedPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import java.awt.Font

Font font = new Font("Serif", Font.BOLD, 13) 
int numPanels = 20

swing = new SwingBuilder()

frame = swing.frame(title:'test', pack:true,   visible:true, defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {
    vbox {
        tabbedPane(id: 'tabs', tabLayoutPolicy:JTabbedPane.SCROLL_TAB_LAYOUT) {
            panel( name:'Tab 1', background:java.awt.Color.WHITE ) {
                vbox {
                    panel( background:java.awt.Color.WHITE ){
                        label ( 'Label 1', preferredSize: [104, 24]).setFont(font) 
                        label ( 'Label 2', preferredSize: [104, 24]).setFont(font) 
                        label ( 'Label 3', preferredSize: [104, 24]).setFont(font) 
                    }   
                    scrollPane( verticalScrollBarPolicy:JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) {
                        vbox {
                            (1..numPanels).each { num ->
                                def panelID = "panel$num"
                                def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
                                    label('description') 
                                    textField( id: "description$num", text:panelID, columns: 70 )
                                    button( id: "buttonpanel$num", text:panelID, actionPerformed:{
                                        swing."$panelID".background = java.awt.Color.RED
                                    } )
                                }
                            }
                        }
                    }
                }
            }       
            panel(name: 'Tab 2', background:java.awt.Color.WHITE) {
                textField(text: 'Some text', columns: 15)
                scrollPane() {
                    textArea(text: 'Some text', columns: 15, rows: 4)
                }
            }       
        }
        panel(id:'secondPanel', background:java.awt.Color.WHITE){                       
            button('Quit', actionPerformed:{
                dispose()
            })
        }   
    }
}

// Define a list of labels for our tabs
def tabComponents = [
  swing.label( text:'Tab 1', font:font.deriveFont( Font.ITALIC ) ),
  swing.label( text:'Tab 2', font:font.deriveFont( 20.0f ) )
]
// Set the tab componets to our labels
tabComponents.eachWithIndex { lbl, idx ->
  swing.tabs.setTabComponentAt idx, lbl
}

frame.size = [ frame.width, 600 ]

PS: You might want to remove your other question that I just found... Generally editing a question to include extra informations is better than posting a new question asking the same thing but with more information