I need to hide the arrow buttons of java.awt.Scrollbar(VERTICAL) in an AWT application.
Does anyone know how this can be achieved?
I saw an example here, but the code just hides the buttons. The vacant space for the buttons still remains; it is not occupied by the scroll bar.
To be more exact, here is the screenshot of what I should achieve. I am not sure which direction to go about it.
Update : I was looking for a solution in AWT. But now I am open to suggestions in Swing as well.
Try this.. it replaces the regular buttons on the Vertical ScrollBar with buttons that are 0x0 in size.
It does limit your look and feel though :(
JScrollPane scroller = new JScrollPane(mainPane);
scroller.setPreferredSize(new Dimension(200,200));
// ... etc
scroller.getVerticalScrollBar().setUI(new BasicScrollBarUI()
{
#Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
#Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
private JButton createZeroButton() {
JButton jbutton = new JButton();
jbutton.setPreferredSize(new Dimension(0, 0));
jbutton.setMinimumSize(new Dimension(0, 0));
jbutton.setMaximumSize(new Dimension(0, 0));
return jbutton;
}
});
Update: sorry, this is a swing solution
Using Nimbus Look and Feel you can use this to remove the arrow buttons:
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:\"ScrollBar.button\".size", 0);
UIManager.getLookAndFeelDefaults().put(
"ScrollBar.decrementButtonGap", 0);
UIManager.getLookAndFeelDefaults().put(
"ScrollBar.incrementButtonGap", 0);
Here is a full example:
public class ScrollDemo extends JFrame {
public ScrollDemo() {
String[] columnNames = {"Column"};
Object[][] data = {
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
};
add(new JScrollPane(new JTable(data, columnNames)));
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
// No Nimbus
}
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:ScrollBarThumb[Enabled].backgroundPainter",
new FillPainter(new Color(127, 169, 191)));
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:ScrollBarThumb[MouseOver].backgroundPainter",
new FillPainter(new Color(127, 169, 191)));
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:ScrollBarTrack[Enabled].backgroundPainter",
new FillPainter(new Color(190, 212, 223)));
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:\"ScrollBar.button\".size", 0);
UIManager.getLookAndFeelDefaults().put(
"ScrollBar.decrementButtonGap", 0);
UIManager.getLookAndFeelDefaults().put(
"ScrollBar.incrementButtonGap", 0);
new ScrollDemo();
}
});
}
}
Code for the Painter used:
public class FillPainter implements Painter<JComponent> {
private final Color color;
public FillPainter(Color c) { color = c; }
#Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.setColor(color);
g.fillRect(0, 0, width-1, height-1);
}
}
Related
This question is an extension of java- repaint() method is misbehaving?
(Reading it, is optional)
I am working on a Music Player
I am using a JSlider as seek bar and using a JLabel to draw text on screen, such as song name.
I am new to Graphics2D
Here's the minimized code:
public class JSliderDemo extends JFrame
{
JLabel label;
JSlider seek = new JSlider();
int y = 10;
public JSliderDemo()
{
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
createWindow();
setVisible(true);
startThread();
}
public void createWindow()
{
JPanel panel = new JPanel(new BorderLayout());
panel.setOpaque(true);
panel.setBackground(Color.BLUE);
panel.setBorder(new LineBorder(Color.YELLOW));
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
label = new Component();
label.setSize(300, 300);
createSlider();
layeredPane.add(seek, new Integer(50));
layeredPane.add(label, new Integer(100));
panel.add(layeredPane);
add(panel);
}
protected void createSlider()
{
seek.setUI(new SeekBar(seek, 300, 10, new Dimension(20, 20), 5,
Color.DARK_GRAY, Color.RED, Color.RED));
seek.setOrientation(JProgressBar.HORIZONTAL);
seek.setOpaque(false);
seek.setLocation(10, 50);
seek.setSize(300, 20);
seek.setMajorTickSpacing(0);
seek.setMinorTickSpacing(0);
seek.setMinimum(0);
seek.setMaximum(1000);
seek.setBorder(new MatteBorder(5, 5, 5, 5, Color.CYAN));
}
protected void startThread()
{
Thread thread = new Thread(new Runnable(){
#Override
public void run()
{
try
{
while(true)
{
if(y == label.getHeight()){y = 1;}
label.repaint();
y += 1;
Thread.sleep(100);
}
}
catch(Exception ex){}
}
});
thread.start();
}
protected class Component extends JLabel
{
#Override
public void paintComponent(Graphics g)
{
Graphics2D gr = (Graphics2D) g;
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gr.setColor(Color.RED);
gr.setFont(new Font("Calibri", Font.PLAIN, 16));
gr.drawString("Song Name", 50, y);
gr.dispose();
}
}
public static void main(String[] args)
{
new JSliderDemo();
}
}
The problem is, when I call repaint() for JLabel it automatically repaints JSlider with it even though JSlider is not included in JLabel.
Output :
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted.........
Now if I remove label.repaint() from the Thread, then the JSlider is not re-painted.
Output:
Slider re-painted
Slider re-painted
Is the repaint() method supposed to work like this?
In my last question, I was told to use Layout Manager and when I did use GridLayout just for checking if it's the solution, then it worked!
Only JLabel was repainted.
But I want to overlap JLabel on JSlider, so I thought of using JLayeredPane. And now, the problem is back.
How can I solve this?
Bottom Line : How can I overlap JLabel on JSlider without leading to repaint() method misbehave ?
OR
Does the repaint() method work like this?
As was already mentioned in the comments, the reason for your JSlider being repainted is that it has overlapping bounds with the JLabel. Even though your label doesn't paint over the area of the slider swing will still mark the overlapping area as dirty (i.e. the overlapping part of the slider will need to be repainted) because swing doesn't know that you are only painting in one part of the component.
To reduce the amount of repaints you will need to make the size of your JLabel smaller. Preferably only as large as it needs to be by invoking its getPreferredSize() method. You'll then be able to move the text by moving the location of the label.
Also you shouldn't be doing updates to the gui in a plain Thread. Use javax.swing.Timer instead. It ensures that all updates to the gui happen on the swing event thread, which is where they should be made.
After making these adjustments to your code the slider is only repainted while the label is actually visually over the slider.
public class JSliderDemo extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(JSliderDemo::new);
}
private final JLabel label = new CustomLabel();
public JSliderDemo() {
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
createWindow();
setVisible(true);
startTimer();
}
public void createWindow() {
JPanel panel = new JPanel(new BorderLayout());
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
label.setLocation(0, 0);
label.setBorder(new LineBorder(Color.RED));
label.setSize(label.getPreferredSize());
layeredPane.add(createSlider(), Integer.valueOf(50));
layeredPane.add(label, Integer.valueOf(100));
panel.add(layeredPane);
setContentPane(panel);
}
protected JSlider createSlider() {
JSlider seek = new CustomSlider();
seek.setOrientation(JProgressBar.HORIZONTAL);
seek.setOpaque(false);
seek.setLocation(10, 50);
seek.setSize(300, 20);
seek.setMajorTickSpacing(0);
seek.setMinorTickSpacing(0);
seek.setMinimum(0);
seek.setMaximum(1000);
seek.setBorder(new LineBorder(Color.BLUE));
return seek;
}
private void startTimer() {
new Timer(100, e -> {
int y = label.getY();
int maxY = label.getParent().getHeight();
if (y == maxY) {
y = -label.getHeight();
}
label.setLocation(label.getX(), y + 1);
label.repaint();
}).start();
}
private static class CustomLabel extends JLabel {
protected CustomLabel() {
setFont(new Font("Calibri", Font.PLAIN, 16));
setText("Song Name");
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Painting Label");
}
}
protected static class CustomSlider extends JSlider {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Painting Slider");
}
}
}
as you can see in the image above, I've a tabbed pane. On the tab header I've a JLabel (Tab Test) and a JButton (X). They are placed next to each other but I want them to have a small gap to look natural.
I've tried with a Box but it has the same background has the text making it not look natural as well. The Box has no setBorders method.
Here's how it look like with a Box:
Here's my code:
System.out.println("NewTableEvent!!!!");
final String tittle = table.getTabName();
JButton jButtonClose = new JButton("X");
jButtonClose.setBorderPainted(false);
jButtonClose.setBorder(null);
JPanel tabComponent = new JPanel(new BorderLayout());
tabComponent.add(new JLabel(tittle), BorderLayout.WEST);
tabComponent.setToolTipText("Close this tab.");
Component box = Box.createRigidArea(new Dimension(25,0));
tabComponent.add(box, BorderLayout.CENTER);
tabComponent.add(jButtonClose, BorderLayout.EAST);
// rightTabbedPane.addTab(null, table.getTable());
rightTabbedPane.addTab(null, new JPanel());
// Get total tabs
final int totalTabs = rightTabbedPane.getComponentCount();
System.out.println("Total tabs: " + totalTabs);
// Set the custom tab component
rightTabbedPane.setTabComponentAt(0, tabComponent);
So, how can I make space the JLabel and JButton and keep the background from that distance neutral?
I wasn't able to test it, but I believe you would just have to tell the box to not be opaque before you add it to the tabComponent:
box.setOpaque(false);
Hopefully, that should work for you.
EDIT
You may be able to set borders around the label and button to accomplish this:
JLabel label = new JLabel(tittle);
tabComponent.add(label);
//add more space between the label and the button
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
tabComponent.add(jButtonClose);
Demo at: http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
Thanks to #Gavin Markee, I came up with this solution:
First, create this new class (It's the exact same class given in the example from Gavin MArkee's link, I'm just posting it here in case it is romoved in the future):
public class ButtonTabComponent extends JPanel {
private final JTabbedPane pane;
public ButtonTabComponent(final JTabbedPane pane) {
// Unset default FlowLayout' gaps
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
if (pane == null) {
throw new NullPointerException("TabbedPane is null");
}
this.pane = pane;
setOpaque(false);
//make JLabel read titles from JTabbedPane
JLabel label = new JLabel() {
#Override
public String getText() {
int i = pane.indexOfTabComponent(ButtonTabComponent.this);
if (i != -1) {
return pane.getTitleAt(i);
}
return null;
}
};
add(label);
//add more space between the label and the button
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
//tab button
JButton button = new TabButton();
add(button);
//add more space to the top of the component
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
private class TabButton extends JButton implements ActionListener {
public TabButton() {
int size = 17;
setPreferredSize(new Dimension(size, size));
setToolTipText("close this tab");
//Make the button looks the same for all Laf's
setUI(new BasicButtonUI());
//Make it transparent
setContentAreaFilled(false);
//No need to be focusable
setFocusable(false);
setBorder(BorderFactory.createEtchedBorder());
setBorderPainted(false);
//Making nice rollover effect
//we use the same listener for all buttons
addMouseListener(buttonMouseListener);
setRolloverEnabled(true);
//Close the proper tab by clicking the button
addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
int i = pane.indexOfTabComponent(ButtonTabComponent.this);
if (i != -1) {
pane.remove(i);
}
}
//we don't want to update UI for this button
#Override
public void updateUI() {
}
//paint the cross
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
//shift the image for pressed buttons
if (getModel().isPressed()) {
g2.translate(1, 1);
}
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.BLACK);
if (getModel().isRollover()) {
g2.setColor(Color.MAGENTA);
}
int delta = 6;
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
g2.dispose();
}
}
private final static MouseListener buttonMouseListener = new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(true);
}
}
#Override
public void mouseExited(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(false);
}
}
};
}
And then when you create your tabe you just have to:
tabbedPane.addTab(myTabTitle, myTable);
tabbedPane.setTabComponentAt(tabIndex, new ButtonTabComponent(tabbedPane));
When I set the window to non-opaque,the font look like changed!Who can tell me why and help me ,thanks!
I guess this is affected by the "RenderingHints.KEY_ANTIALIASING",but I test in many way,and ther is no my desired result.
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setUndecorated(true);
AWTUtilities.setWindowOpaque(frame, false);
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 10));
mainPanel.add(new JLabel("Why this changed?"));
JLabel lbl2 = new JLabel() {
#Override
public void paint(Graphics g) {
// super.paint(g);
// ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawString("There is no change", 0, 15);
}
};
lbl2.setPreferredSize(new Dimension(240, 22));
mainPanel.add(lbl2);
JPanel toolPanel = new JPanel(new FlowLayout());
final JCheckBox ckxWindowOpaque = new JCheckBox("WindowOpaque");
ckxWindowOpaque.setSelected(!AWTUtilities.isWindowOpaque(frame));
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
boolean b = AWTUtilities.isWindowOpaque(frame);
if (b == !ckxWindowOpaque.isSelected()) return;
if (b) {
AWTUtilities.setWindowOpaque(frame, false);
} else {
AWTUtilities.setWindowOpaque(frame, true);
}
}
};
ckxWindowOpaque.addActionListener(al);
toolPanel.add(ckxWindowOpaque);
toolPanel.add(new JButton(new AbstractAction("Exit") {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}));
frame.getContentPane().add(toolPanel, BorderLayout.NORTH);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
((JComponent) frame.getContentPane()).setBorder(BorderFactory.createLineBorder(Color.black));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(200, 200, 200, 200);
frame.setVisible(true);
}
This will have to do with how Swing/AWT deals with the different requirements between an opaque and transparent window and changes being made internally to the anti aliasing.
For example, if I use
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
To render the text, I can get it to appear the same way when it's made transparent
I also get the same result from VALUE_TEXT_ANTIALIAS_DEFAULT, VALUE_TEXT_ANTIALIAS_GASP
But if I use VALUE_TEXT_ANTIALIAS_LCD_HBGR or VALUE_TEXT_ANTIALIAS_LCD_HRGB it will act the same as the JLabel in both modes
These are system level decisions and I don't think you can effect them (easily).
You might like to take a look at LCD Text: Anti-Aliasing on the Fringe, which is an interesting read, but I'm not sure it will help much...
I have been debugging and tracing my program,and I found the key code here:SwingUtilities2.drawString/drawChars/drawTextAntialiased.
So,I modified the JRE's code in the "SwingUtilities2.drawString/drawChars",I add code like this :
if (UIManager.getBoolean("MYLAF.AATextInfo.Disable")) {
g.drawChars(data, offset, length, x, y);
return nextX;
}
Finally,on the begin of my program,I add the setting "UIManager.put("MYLAF.AATextInfo.Disable",true)".
If you wan't modify the SwingUtilities2,you can use "myJComponent.setClientProperty(AA_TEXT_PROPERTY_KEY,null)".
I'm working in with a JTabbedPane, I need to add a close button in the tabs to close the current one.
I have been searching and as I understand I must extend from JPanel and add the close button as they say here
But, is there a way to add the close buttons extending JTabbedPane or is there a easier way to do it?
Thanks in advance, I really appreciate your time and your help.
Essentially, you're going to need to supply a "renderer" for the tab. Take a look at JTabbedPane.setTabComponentAt(...) for more information.
The basic idea is to supply a component that will be laid out on the tab.
I typically create a JPanel, onto which I add a JLabel (for the title) and, depending on what I want to display, some kind of control that acts as the close action.
tabPane.addTab(title, tabBody);
int index = tabPane.indexOfTab(title);
JPanel pnlTab = new JPanel(new GridBagLayout());
pnlTab.setOpaque(false);
JLabel lblTitle = new JLabel(title);
JButton btnClose = new JButton("x");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
pnlTab.add(lblTitle, gbc);
gbc.gridx++;
gbc.weightx = 0;
pnlTab.add(btnClose, gbc);
tabPane.setTabComponentAt(index, pnlTab);
btnClose.addActionListener(myCloseActionHandler);
Now somewhere else, I establish the action handler...
public class MyCloseActionHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Component selected = tabPane.getSelectedComponent();
if (selected != null) {
tabPane.remove(selected);
// It would probably be worthwhile getting the source
// casting it back to a JButton and removing
// the action handler reference ;)
}
}
}
Now, you just as easily use any component you like and attach a mouse listener to it and monitor the mouse clicks...
Updated
The above example will only remove the currently active tab, there are a couple of ways to fix this.
The best is to probably provide some means for the action to find the tab it's associated with...
public class MyCloseActionHandler implements ActionListener {
private String tabName;
public MyCloseActionHandler(String tabName) {
this.tabName = tabName;
}
public String getTabName() {
return tabName;
}
public void actionPerformed(ActionEvent evt) {
int index = tabPane.indexOfTab(getTabName());
if (index >= 0) {
tabPane.removeTabAt(index);
// It would probably be worthwhile getting the source
// casting it back to a JButton and removing
// the action handler reference ;)
}
}
}
This uses the name of tab (as used with JTabbedPane#addTab) to find and then remove the tab and its associated component...
I found a tab example (from the java site) that appears to do that, at least in theirs. (Though I thought, when I tried it in the past, that it also closed the currently selected tab, though it works properly when you run their example, though I think when I updated it to work on a tabbed java notepad, it was closing the currently selected tab, though maybe I did it wrong.
http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java
Yes, my thing is working now! This WILL work for the actual tab, rather than the currently selected tab!
Hopefully you have got the answer to your question. I want to give a link that was very useful for me.
JTabbedPane with a close button
Here is some code as well.
public static void createAndShowGUI()
{
JFrame frame = new JFrame("Tabs");
frame.setMinimumSize(new Dimension(500, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel = new JPanel();
panel.setOpaque(false);
tabbedPane.add(panel);
tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel), getTitlePanel(tabbedPane, panel, "Tab1"));
JPanel panel1 = new JPanel();
panel1.setOpaque(false);
tabbedPane.add(panel1);
tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel1), getTitlePanel(tabbedPane, panel1, "Tab2"));
JPanel panel2 = new JPanel();
panel2.setOpaque(false);
tabbedPane.add(panel2);
tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel2), getTitlePanel(tabbedPane, panel2, "Tab3"));
JPanel panel3 = new JPanel();
panel3.setOpaque(false);
tabbedPane.add(panel3);
tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel3), getTitlePanel(tabbedPane, panel3, "Tab4"));
frame.add(tabbedPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
I made some changes in the code of oracle.
http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java
Giving the possibility to add an icon to the tab , plus the close tab button. Hope that helps.
public static void addTag(JTabbedPane tab, String title, Icon icon, int index){
MouseListener close = new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//your code to remove component
//I use this way , because I use other methods of control than normal: tab.remove(int index);
}
};
final ButtonClose buttonClose = new ButtonClose (title, icon, close );
tab.setTabComponentAt(index, buttonClose);
tab.validate();
tab.setSelectedIndex(index);
}
public class ButtonClose extends JPanel {
public ButtonClose(final String title, Icon icon, MouseListener e) {
JLabel ic = new JLabel(icon);
ic.setSize(icone.getIconWidth(), icone.getIconHeight());
JLabel text= new JLabel(title);
text.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
ButtonTab button = new ButtonTab();
button.addMouseListener(e);
button.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
JPanel p = new JPanel();
p.setSize(getWidth() - icone.getIconWidth(), 15);
p.add(text);
p.add(button);
add(ic);
add(p);
}
private class ButtonTab extends JButton {
public ButtonTab() {
int size = 13;
setPreferredSize(new Dimension(size, size));
setToolTipText("Close");
setUI(new BasicButtonUI());
setFocusable(false);
setBorderPainted(false);
addMouseListener(listener);
setRolloverEnabled(true);
}
#Override
public void updateUI() {
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
if (getModel().isPressed()) {
g2.translate(1, 1);
}
g2.setStroke(new BasicStroke(2));
g2.setColor(new Color(126, 118, 91));
if (getModel().isRollover()) {
g2.setColor(Color.WHITE);
}
int delta = 3;
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
g2.dispose();
}
}
private final MouseListener listener = new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setContentAreaFilled(true);
button.setBackground(new Color(215, 65, 35));
}
}
#Override
public void mouseExited(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setContentAreaFilled(false); //transparent
}
}
};
}
Check out Peter-Swing here. It has a JClosableTabbedPane class in it, as well as many others.
When you download the jar file you can run it and have examples of all the classes.
You can have a JLabel named "x" and use the mouseListener
private final JLabel l = new JLabel(); // this is the label for tabbedPane
private final JLabel b = new JLabel("x");//Close Button
if (closeable)
{
b.setToolTipText("Click to close");
b.setOpaque(false);
b.setBackground(Color.gray);
b.addMouseListener(new MouseAdapter()
{
#Override
public void mouseExited(MouseEvent e)
{
b.setBorder(bordere);
b.setOpaque(false);
}
#Override
public void mouseEntered(MouseEvent e)
{
b.setBorder(borderl);
}
#Override
public void mouseReleased(MouseEvent e)
{
b.setOpaque(false);
b.repaint();
if (b.contains(e.getPoint()))
{
b.setBorder(borderl);
if (confirmTabClosing())
{
tab.remove(tabIndex());
if(tab.getTabCount() == 0)
spacialTabComponent.maximizeOrRestore.doClick();
}
}
else
b.setBorder(bordere);
}
#Override
public void mousePressed(MouseEvent e)
{
b.setOpaque(true);
b.repaint();
}
});
b.setBorder(bordere);
add(b, getLeftAlignedBothFilledGBC(1, 0, new Insets(0, 0, 0, 0), 0, 0));
}
}
jbCloseButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int index = jtbMainTabbedPane.indexOfTabComponent(jbCloseButton);
jtbMainTabbedPane.remove(index);
}
});
I need to customize the knob of JSlider. I need to put my own knob's image over default knob of Jslider.
The problem is that currently two knobs are coming in response. One my own knob and second the default knob. Please tell me how i can i hide the default knob or any other solution.
Below code is used to do so.
public class ImageTest {
JSlider slider;
JLabel label;
public ImageTest()
{
JPanel panel = new BackgroundPanel();
slider = new BackgroundSlider();
slider.setMaximum(300);
slider.setMinimum(0);
slider.setValue(50);
slider.setExtent(10);
slider.addChangeListener(new MyChangeAction());
label = new JLabel("50");
panel.setLayout(new GridBagLayout());
panel.setSize(797,402);
slider.setOpaque(false);
slider.setPaintTrack(false);
label.setOpaque(false);
slider.setPreferredSize(new Dimension(340, 20));
GridBagConstraints gridBagConstraintsSlider = new GridBagConstraints();
gridBagConstraintsSlider.gridy = 0;
gridBagConstraintsSlider.gridx = 0;
gridBagConstraintsSlider.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraintsSlider.insets = new Insets(0, 283, 260, 0);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = 1;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new Insets(0, 50, 240, 0);
panel.add(slider, gridBagConstraintsSlider);
panel.add(label, gridBagConstraints);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setSize(797,402);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
WindowUtil.locateCenter(frame);
}
public static void main(String[] args) {
ImageTest im= new ImageTest();
}
public class MyChangeAction implements ChangeListener{
public void stateChanged(ChangeEvent ce){
int value = slider.getValue();
String str = Integer.toString(value);
label.setText(str);
if(value==300)
{
label.setText("Max");
}
}
}
}
class BackgroundSlider extends JSlider
{
Image image;
public BackgroundSlider()
{
try
{
image = javax.imageio.ImageIO.read(new File("slider.png"));
}
catch (Exception e) { /*handled in paintComponent()*/ }
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image != null)
g.drawImage(image, this.getValue(),(int)this.getAlignmentY(),10,20,this);
g.setColor(Color.RED);
//draw a centered horizontal line
g.drawRect(15,this.getHeight()-1,this.getValue(),this.getHeight()+2);
g.fillRect(15,this.getHeight()-1,this.getValue(),this.getHeight()+2);
}
}
Thanks
Jyoti
To hide the knob, override the UIManager's Slider.horizontalThumbIcon property with an blank icon, like this:
public static void main(String[] args) throws Exception {
UIManager.getLookAndFeelDefaults().put("Slider.horizontalThumbIcon",new Icon(){
#Override
public int getIconHeight() {
return 0;
}
#Override
public int getIconWidth() {
return 0;
}
#Override
public void paintIcon(Component c, Graphics g, int x, int y) {
//do nothing
}
});
JFrame f = new JFrame();
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
f.add(slider);
f.setSize(200,200);
f.setVisible(true);
}
A solution with a different BasicSliderUI looks like this:
public class SuperSlider extends JSlider {
public SuperSlider(int min, int max, int value) {
super(min,max,value);
setUI(new SuperSliderUI(this));
}
private class SuperSliderUI extends BasicSliderUI {
#Override
public void paintThumb(Graphics g) {
}
}
}
The UIManager solution only works in the Metal LAF from what I can tell.
If you want to change the behavour of the UI then you need to change the UI. In this case you would need to the BasicSliderUI (or one of its sub classes). Then I believe you would need to override the paintThumb() method.