Add space between label and jButton on a tabbed pane header - java

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));

Related

java- repaint() method is misbehaving - 2?

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");
}
}
}

Assistance With KeyEvent

I am making a platformer game for a class project and so far all I have been able to do is add the chicken character to the game. I need to be able to have him move forward on the press of "D" or right arrow. My code is:
public class Main extends JFrame {
public Main(){
//Creates Title Image
JLabel title = new JLabel(" ");
ImageIcon tl = new ImageIcon("title.gif");
title.setIcon(tl);
//Creates Start Image
final JButton start = new JButton("");
ImageIcon st = new ImageIcon("start.gif");
start.setIcon(st);
//Creates Options Image
JButton options = new JButton("");
ImageIcon opt = new ImageIcon("options.gif");
options.setIcon(opt);
options.setBackground(Color.BLACK);
//Create first frame for "Start" button
final JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1));
p1.add(start, BorderLayout.CENTER);
//Create second panel for title label
final JPanel p2 = new JPanel(new BorderLayout());
p2.setLayout(new GridLayout(1, 3));
p2.add(title, BorderLayout.WEST);
//Create third panel for "Options" button
final JPanel p3 = new JPanel(new BorderLayout());
p3.setLayout(new GridLayout(1, 1));
p3.add(options, BorderLayout.SOUTH);
//Creates fourth panel to organize all other primary
final JPanel p4 = new JPanel(new BorderLayout());
p4.setLayout(new GridLayout(1, 3));
p4.add(p1, BorderLayout.WEST);
p4.add(p2, BorderLayout.CENTER);
p4.add(p3, BorderLayout.EAST);
//When button is clicked, it changes the level
start.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(start.isEnabled()) {
remove(p4);
setSize(1440, 500);
add(new ContentPanel1());
validate();
}
else {
return;
}
}
});
//Adds fourth panel to frame
add(p4, BorderLayout.CENTER);
}
public static void main(String arg[]) {
Main frame = new Main();
//Finds screen size of monitor
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//Creates the frame
frame.setTitle("Cockadoodle Duty: Awakening");
frame.setSize(screenSize);
frame.setLocale(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
String background = "#000000";
frame.setBackground(Color.decode(background));
}
}
class coordinate {
public static int x;
public static int y;
}
class ContentPanel1 extends JPanel{
Image back = Toolkit.getDefaultToolkit().getImage("level0.gif");
Image chick = Toolkit.getDefaultToolkit().getImage("chicken.gif");
ContentPanel1() {
MediaTracker mt = new MediaTracker(this);
mt.addImage(back, 0);
try {
mt.waitForAll();
} catch (InterruptedException e){
e.printStackTrace();
}
}
public void paintComponent(Graphics g){
coordinate.x = 20;
coordinate.y = 321;
super.paintComponent(g);
int imwidth = back.getWidth(null);
int imheight = back.getHeight(null);
g.drawImage(back, 1, 1, null);
g.drawImage(chick, coordinate.x, coordinate.y, null);
}
public void MoveDirection(KeyEvent e, Graphics g) {
coordinate.x = 20;
coordinate.y = 321;
super.paintComponent(g);
int key = e.getKeyCode();
if(key == 68) {
coordinate.x += 1;
g.drawImage(chick, coordinate.x, coordinate.y, null);
}
}
}
The main trouble I have been having with my code is the bit at the end with the MoveDirection method. The way I have it going is by adding a new chicken to the frame (This was mainly due to the fact that I was just testing to see if the code worked). Is there a better way to do that too?
Start by taking a look at How to Use Key Bindings
NEVER call super.paintComponent(g); (or paintComponent(g);) directly from outside the context of the paintComponent method, there is a lot more to painting then just painting the component background. See Painting in AWT and Swing and Performing Custom Painting for more details. Instead, simply call repaint when you want to, well, repaint the component.
The use of MediaTracker is out of date and you should be using the ImageIO API instead, which will block automatically while reading the image. See Reading/Loading an Image for more details
Don't use Toolkit.getDefaultToolkit().getScreenSize() in combination with JFrame#setSize, the getScreenSize method does not take into account things like the task bar or dock of some OS's, instead use the JFrame#setExtendedState and pass it JFrame.MAXIMIZED_BOTH
frame.setLocale(null); isn't doing what you think it is

How to add close button to a JTabbedPane Tab?

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);
}
});

Java graphics JApplet

I'm currently working on a program which is for school. We have to make an Applet and I have opted for JApplet. For some reason, the panel which I am using to display a specific string will not show. What might be the issue here? Please point me in the right direction. Also, I looked at a few a tutorials, some suggested that I have "start", "stop" and "destory" methods and some didn't. Do any of these methods have an effect on why my JPanel won't show the graphic?
Thank you
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Shape extends JApplet {
/**
*
*/
private static final long serialVersionUID = 1L;
// making the radiobuttons for the shape choices
JRadioButton squareButton = new JRadioButton("Square",true);
JRadioButton ovalButton = new JRadioButton("Oval",false);
JRadioButton rectangleButton = new JRadioButton("Rectangle",false);
JRadioButton triangleButton = new JRadioButton("Triangle",false);
// making radiobuttons for the color choices
JRadioButton redButton = new JRadioButton("Red",true);
JRadioButton blueButton = new JRadioButton("Blue",false);
JRadioButton greenButton = new JRadioButton("Green",false);
JRadioButton yellowButton = new JRadioButton("Yellow",false);
// making buttons draw and animate
JButton drawButton = new JButton("Draw!");
JButton animateButton = new JButton("Animate!");
// making JTextFields for length and width
JTextField lengthField = new JTextField("Enter a length",15);
JTextField widthField = new JTextField("Enter a width",15);
// making JPanel, in which the radiobuttons will go01
JPanel shapePanel = new JPanel();
JPanel colorPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JPanel textPanel = new JPanel();
drawPanel dPanel;
ButtonGroup shapeGroup = new ButtonGroup();
ButtonGroup colorGroup = new ButtonGroup();
// variables that will dictates the shape, size and color
int length = 200;
int width = 200;
Color color = Color.RED;
String shape = "square";
public void init() {
setLayout(new FlowLayout()); // setting layout for the applet
setSize(680,480);
// setting the layout for the shapePanel - gridlayout 2 rows, 2 cols and space of 5
shapePanel.setLayout(new GridLayout(2,2,5,5));
// adding a border to the shapePanel to indicate to the user what it does "titledBorder"
shapePanel.setBorder(BorderFactory.createTitledBorder("Choose a shape"));
// setting layout for the color panel - gridlayout 2 rows, 2 cols and space of 5
colorPanel.setLayout(new GridLayout(2,2,5,5));
// adding a border to the colorPanel to indicate to the user what it does "titledBorder"
colorPanel.setBorder(BorderFactory.createTitledBorder("Choose a color"));
// setting the layout for the buttonPanel - gridlayout 1 row, 2 cols and space of 5
buttonPanel.setLayout(new GridLayout(1,2,5,5));
// adding a color border
buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));
// setting the layout of the textField - gridlayout 1 row, 2 cols and space of 5
textPanel.setLayout(new GridLayout(1,2,5,5));
// adding some attributes for lengthField and widthField
lengthField.setFont(new Font("Arial",Font.PLAIN,12));
lengthField.setForeground(new Color(150,150,150));
widthField.setFont(new Font("Arial",Font.PLAIN,12));
widthField.setForeground(new Color(150,150,150));
// using shapegroup to organize the JRadioButtons
shapeGroup.add(squareButton);
shapeGroup.add(ovalButton);
shapeGroup.add(rectangleButton);
shapeGroup.add(triangleButton);
// using colorgroup to organize the color radiobuttons
colorGroup.add(redButton);
colorGroup.add(blueButton);
colorGroup.add(greenButton);
colorGroup.add(yellowButton);
// add the shape buttons to the panel so they appear in a square form
shapePanel.add(squareButton);
shapePanel.add(ovalButton);
shapePanel.add(rectangleButton);
shapePanel.add(triangleButton);
// adding color buttons to the color panel
colorPanel.add(redButton);
colorPanel.add(blueButton);
colorPanel.add(greenButton);
colorPanel.add(yellowButton);
// adding jbuttons
buttonPanel.add(drawButton);
buttonPanel.add(animateButton);
// adding textfields to the textPanel
textPanel.add(lengthField);
textPanel.add(widthField);
dPanel = new drawPanel();
// adding panels to the applet
add(shapePanel);
add(colorPanel);
add(buttonPanel);
add(textPanel);
add(dPanel);
// adding focus listener to lengthField and widthField
lengthField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
lengthField.setText("");
lengthField.setForeground(Color.black);
}
public void focusLost(FocusEvent e) {}
});
widthField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
widthField.setText("");
widthField.setForeground(Color.black);
}
public void focusLost(FocusEvent e) {}
});
drawButton.addActionListener(new drawListener());
}
// when the person presses paint, this will be executed to paint the specific shape, color with the width and length
class drawListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int mylength = 5;
int mywidth = 5;
try {
mylength = Integer.parseInt(lengthField.getText());;
mywidth = Integer.parseInt(widthField.getText());;
}catch(Exception ex) {
JOptionPane.showMessageDialog(null,""+ex,"",JOptionPane.ERROR_MESSAGE);
}
if((mylength > 200 || mylength < 5)) {
JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200",
"Invalid length message", JOptionPane.ERROR_MESSAGE);
}else if((mywidth > 200 || mywidth < 5)) {
JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200",
"Invalid width message", JOptionPane.ERROR_MESSAGE);
}else {
length = mylength;
width = mywidth;
// checking which color button is selected
if(redButton.isSelected()) {
color = Color.RED;
}else if(blueButton.isSelected()) {
color = Color.BLUE;
}else if(greenButton.isSelected()) {
color = Color.GREEN;
}else if(yellowButton.isSelected()) {
color = Color.YELLOW;
}
// checking which shape has been selected
if(rectangleButton.isSelected()) {
shape = "rectangle";
}else if(triangleButton.isSelected()) {
shape = "triangle";
}else if(ovalButton.isSelected()) {
shape = "oval";
}else if(squareButton.isSelected()) {
shape = "square";
}
//System.out.printf("%3d %3d %s %s \n",length,width,shape,color);
}
}
}
// This will be used to do the painting
class drawPanel extends JPanel {
private static final long serialVersionUID = 1L;
//Paint Method
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
g2.drawString("My awesome string", 200, 200);
}
}
}
A JPanel with no components has a default size of 0x0. Try this source:
// <applet code=Shape width=640 height=480></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Shape extends JApplet {
/**
*
*/
private static final long serialVersionUID = 1L;
// making the radiobuttons for the shape choices
JRadioButton squareButton = new JRadioButton("Square",true);
JRadioButton ovalButton = new JRadioButton("Oval",false);
JRadioButton rectangleButton = new JRadioButton("Rectangle",false);
JRadioButton triangleButton = new JRadioButton("Triangle",false);
// making radiobuttons for the color choices
JRadioButton redButton = new JRadioButton("Red",true);
JRadioButton blueButton = new JRadioButton("Blue",false);
JRadioButton greenButton = new JRadioButton("Green",false);
JRadioButton yellowButton = new JRadioButton("Yellow",false);
// making buttons draw and animate
JButton drawButton = new JButton("Draw!");
JButton animateButton = new JButton("Animate!");
// making JTextFields for length and width
JTextField lengthField = new JTextField("Enter a length",15);
JTextField widthField = new JTextField("Enter a width",15);
// making JPanel, in which the radiobuttons will go01
JPanel shapePanel = new JPanel();
JPanel colorPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JPanel textPanel = new JPanel();
drawPanel dPanel;
ButtonGroup shapeGroup = new ButtonGroup();
ButtonGroup colorGroup = new ButtonGroup();
// variables that will dictates the shape, size and color
int length = 200;
int width = 200;
Color color = Color.RED;
String shape = "square";
public void init() {
setLayout(new FlowLayout()); // setting layout for the applet
// This is set by HTML!
//setSize(680,480);
// setting the layout for the shapePanel - gridlayout 2 rows, 2 cols and space of 5
shapePanel.setLayout(new GridLayout(2,2,5,5));
// adding a border to the shapePanel to indicate to the user what it does "titledBorder"
shapePanel.setBorder(BorderFactory.createTitledBorder("Choose a shape"));
// setting layout for the color panel - gridlayout 2 rows, 2 cols and space of 5
colorPanel.setLayout(new GridLayout(2,2,5,5));
// adding a border to the colorPanel to indicate to the user what it does "titledBorder"
colorPanel.setBorder(BorderFactory.createTitledBorder("Choose a color"));
// setting the layout for the buttonPanel - gridlayout 1 row, 2 cols and space of 5
buttonPanel.setLayout(new GridLayout(1,2,5,5));
// adding a color border
buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));
// setting the layout of the textField - gridlayout 1 row, 2 cols and space of 5
textPanel.setLayout(new GridLayout(1,2,5,5));
// adding some attributes for lengthField and widthField
lengthField.setFont(new Font("Arial",Font.PLAIN,12));
lengthField.setForeground(new Color(150,150,150));
widthField.setFont(new Font("Arial",Font.PLAIN,12));
widthField.setForeground(new Color(150,150,150));
// using shapegroup to organize the JRadioButtons
shapeGroup.add(squareButton);
shapeGroup.add(ovalButton);
shapeGroup.add(rectangleButton);
shapeGroup.add(triangleButton);
// using colorgroup to organize the color radiobuttons
colorGroup.add(redButton);
colorGroup.add(blueButton);
colorGroup.add(greenButton);
colorGroup.add(yellowButton);
// add the shape buttons to the panel so they appear in a square form
shapePanel.add(squareButton);
shapePanel.add(ovalButton);
shapePanel.add(rectangleButton);
shapePanel.add(triangleButton);
// adding color buttons to the color panel
colorPanel.add(redButton);
colorPanel.add(blueButton);
colorPanel.add(greenButton);
colorPanel.add(yellowButton);
// adding jbuttons
buttonPanel.add(drawButton);
buttonPanel.add(animateButton);
// adding textfields to the textPanel
textPanel.add(lengthField);
textPanel.add(widthField);
dPanel = new drawPanel();
dPanel.setPreferredSize(new Dimension(500,300));
// adding panels to the applet
add(shapePanel);
add(colorPanel);
add(buttonPanel);
add(textPanel);
add(dPanel);
// adding focus listener to lengthField and widthField
lengthField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
lengthField.setText("");
lengthField.setForeground(Color.black);
}
public void focusLost(FocusEvent e) {}
});
widthField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
widthField.setText("");
widthField.setForeground(Color.black);
}
public void focusLost(FocusEvent e) {}
});
drawButton.addActionListener(new drawListener());
}
// when the person presses paint, this will be executed to paint the specific shape, color with the width and length
class drawListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int mylength = 5;
int mywidth = 5;
try {
mylength = Integer.parseInt(lengthField.getText());;
mywidth = Integer.parseInt(widthField.getText());;
}catch(Exception ex) {
JOptionPane.showMessageDialog(null,""+ex,"",JOptionPane.ERROR_MESSAGE);
}
if((mylength > 200 || mylength < 5)) {
JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200",
"Invalid length message", JOptionPane.ERROR_MESSAGE);
}else if((mywidth > 200 || mywidth < 5)) {
JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200",
"Invalid width message", JOptionPane.ERROR_MESSAGE);
}else {
length = mylength;
width = mywidth;
// checking which color button is selected
if(redButton.isSelected()) {
color = Color.RED;
}else if(blueButton.isSelected()) {
color = Color.BLUE;
}else if(greenButton.isSelected()) {
color = Color.GREEN;
}else if(yellowButton.isSelected()) {
color = Color.YELLOW;
}
// checking which shape has been selected
if(rectangleButton.isSelected()) {
shape = "rectangle";
}else if(triangleButton.isSelected()) {
shape = "triangle";
}else if(ovalButton.isSelected()) {
shape = "oval";
}else if(squareButton.isSelected()) {
shape = "square";
}
//System.out.printf("%3d %3d %s %s \n",length,width,shape,color);
}
}
}
// This will be used to do the painting
class drawPanel extends JPanel {
private static final long serialVersionUID = 1L;
//Paint Method
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
g2.drawString("My awesome string", 200, 200);
}
}
}

How to hide the arrow buttons in a JScrollBar

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);
}
}

Categories