Search Results

Search found 1571 results on 63 pages for 'swing'.

Page 36/63 | < Previous Page | 32 33 34 35 36 37 38 39 40 41 42 43  | Next Page >

  • JLabel wont change color twice

    - by Aly
    Hi, I have the following code: public class Test extends JFrame implements ActionListener{ private static final Color TRANSP_WHITE = new Color(new Float(1), new Float(1), new Float(1), new Float(0.5)); private static final Color TRANSP_RED = new Color(new Float(1), new Float(0), new Float(0), new Float(0.1)); private static final Color[] COLORS = new Color[]{ TRANSP_RED, TRANSP_WHITE}; private int index = 0; private JLabel label; private JButton button; public Test(){ super(); setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); label = new JLabel("hello world"); label.setOpaque(true); label.setBackground(TRANSP_WHITE); getContentPane().add(label); button = new JButton("Click Me"); button.addActionListener(this); getContentPane().add(button); pack(); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource().equals(button)){ label.setBackground(COLORS[index % (COLORS.length - 1)]); } } public static void main(String[] args) { new Test(); } } When I run it I get the label with the TRANSP_WHITE background and then when I click the button this color changes to TRANSP_RED but when I click it again I see no change in color. Does anyone know why? Thanks

    Read the article

  • How to display an icon on JOptionPane.showInputDialog() in Java?

    - by Lion
    The following segment of code shows JOptionPane.showInputDialog() method. It works fine but I need to display a custom icon on it. I currently left it null as shown below. String operatingSystem = System.getProperty("os.name"); Object o[] = {"Turn Off", "Restart", "Stand By", "Log Off"}; Frame frame = new Frame(operatingSystem); Object selectedValue = JOptionPane.showInputDialog(frame, "What would you like to do with the system?", "Select a task", JOptionPane.INFORMATION_MESSAGE, null, o, o[0]); //<---- Here it is. How can an icon be displayed on it replacing null?

    Read the article

  • Java MVC project - either I can't update the drawing, or I can't see it

    - by user1881164
    I've got a project based around the Model-View-Controller paradigm, and I've been having a lot of trouble with getting it to work properly. The program has 4 panels, which are supposed to allow me to modify an oval drawn on the screen in various ways. These seem to work fine, and after considerable trouble I was able to get them to display in the JFrame which holds the whole shebang. I've managed to get them to display by breaking away from the provided instructions, but when I do that, I can't seem to get the oval to update. However, if I follow the directions to the letter, I only ever see an empty frame. The project had pretty specific directions, which I followed up to a point, but some of the documentation was unclear. I think what I'm missing must be something simple, since nothing is jumping out at me as not making sense. I have to admit though that my Java experience is limited and my experience with GUI design/paradigms is even more so. Anyway, I've been searching the web and this site extensively trying to figure out what's wrong, but this is a somewhat specific example and honestly I just don't know enough about this to generalize any of the answers I've found online and figure out what's missing. I've been poring over this code for far too long now so I'm really hoping someone can help me out. public class Model { private Controller controller; private View view; private MvcFrame mvcFrame; private int radius = 44; private Color color = Color.BLUE; private boolean solid = true; //bunch of mutators and accessors for the above variables public Model() { controller = new Controller(this); view = new View(this); mvcFrame = new MvcFrame(this); } } Here's the model class. This seems to be fairly simple. I think my understanding of what's going on here is solid, and nothing seems to be wrong. Included mostly for context. public class Controller extends JPanel{ private Model model; public Controller(Model model) { this.model = model; setBorder(BorderFactory.createLineBorder(Color.GREEN)); setLayout(new GridLayout(4,1)); add(new RadiusPanel(model)); add(new ColorPanel(model)); add(new SolidPanel(model)); add(new TitlePanel(model)); } } This is the Controller class. As far as I can tell, the setBorder, setLayout, and series of adds do nothing here. I had them commented out, but this is the way that the instructions told me to do things, so either there's a mistake there or something about my setup is wrong. However, when I did it this way, I would get an empty window (JFrame) but none of the panels would show up in it. What I did to fix this is put those add functions in the mvcFrame class: public class MvcFrame extends JFrame { private Model model; public MvcFrame(Model model){ this.model = model; //setLayout(new GridLayout(4,1)); //add(new RadiusPanel(model)); //add(new ColorPanel(model)); //add(new SolidPanel(model)); //add(new TitlePanel(model)); //add(new View(model)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setSize(800,600); setVisible(true); } } So here's where things kind of started getting weird. The first block of commented out code is the same as what's in the Controller class. The reason I have it commented out is because that was just a lucky guess - it's not supposed to be like that according to the instructions. However, this did work for getting the panels to show up - but at that point I was still tearing my hair out trying to get the oval to display. The other commented line ( add(new View(model)); ) was a different attempt at making things work. In this case, I put those add functions in the View class (see commented out code below). This actually worked to display both the oval and the panels, but that method wouldn't allow me to update the oval. Also, though I just had the oval displaying, I can't seem to figure out what exactly made that happen, and I can't seem to make it come back. public class View extends JPanel{ private Model model; public View(Model model) { this.model = model; //setLayout(new GridLayout(4,1)); //add(new RadiusPanel(model)); //add(new ColorPanel(model)); //add(new SolidPanel(model)); //add(new TitlePanel(model)); repaint(); } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); //center of view panel, in pixels: int xCenter = getWidth()/2; int yCenter = getHeight()/2; int radius = model.getRadius(); int xStart = xCenter - radius; int yStart = yCenter - radius; int xWidth = 2 * radius; int yHeight = 2 * radius; g.setColor(model.getColor()); g.clearRect(0, 0, getWidth(), getHeight()); if (model.isSolid()){ g.fillOval(xStart, yStart, xWidth, yHeight); } else { g.drawOval(xStart, yStart, xWidth, yHeight); } } } Kinda same idea as before - the commented out code is stuff I added to try to get things working, but is not based on the provided directions. In the case where that stuff was uncommented, I had the add(new View(model)); line from the mvcFrame line uncommented as well. The various panel classes (SolidPanel, ColorPanel, etc) simply extend a class called ControlPanel which extends JPanel. These all seem to work as expected, not having much issue with them. There is also a driver which launches the GUI. This also seems to work as expected. The main problem I'm having is that I can't get the oval to show up, and the one time I could make it show up, none of the options for changing it seemed to work. I feel like I'm close but I'm just at a loss for other things to try out at this point. Anyone who can help will have my sincerest gratitude.

    Read the article

  • Java Disabled JLabel Reports Mouse Clicked

    - by ikurtz
    colLabels[i].addMouseListener(new MyAdapter()); private class MyAdapter extends MouseAdapter { @Override public void mouseClicked(MouseEvent event) { ColJLabel colJLabel = (ColJLabel)event.getComponent(); System.out.println(colJLabel.ColID); setColumnHeader(false); } } colLabels[i].setEnabled(flag); The situation is this: Mouse clicks are trapped correctly but when i have the JLabel (ColJLabel) control disabled it still reports mouse clicks. How can I make so that mouse clicks are only reported when the control is enabled? Thanks.

    Read the article

  • Rendering Dragged object during drag

    - by marco
    Hello, I have been stuck on this problem for a while now. I want to drag a row in a jtable to reorder it. But I really want the user to see the row while dragging it and not just the default "drag" rectangle symbol. So I took a look at the java DnD tutorial finding it very useful, but.. The problem is that I still dont know which method I should override to get this going. Can somebody help me out?

    Read the article

  • Laying out JPanels to make a simple GUI

    - by Elvis
    Hello, first of all, this is more or less my first GUI and ive learned Java for not more then a week, so it might contain some serious programming errors. What i have right now is: Buttons and labels are part of OptionPanel and are on the left, DrawingPanel is about 5x5 px in size and is on the right. What I am trying to do is a simple test, to get me more familiar with the GUI. The rectangle should be movable and re-sizeable by the user when clicking the respective buttons: http://www.upload.ee/image/612005/JFrame2.jpg Right now i have: JFrame MainFrame - Makes JFrame (Not using the setSize function. using .pack() instead. not sure about it) JPanel MergedPanel - FlowLayout - Adds JPanel OptionsPanel and JPanel DrawingPanel together and gets injected to JFrame MainFrame JPanel DrawPanel - This JPanel is responsible of drawing the rectangle. JPanel OptionPanel - FlowLayout - This JPanel is responsible of the buttons. Help please.

    Read the article

  • how to create internal frame?

    - by joseph
    I created class NewProject extends JInternalFrame. Then I create New...Action named "NEW", localised in File menu. I put code NewProject p = new NewProject(); p.setVisible(true); to the ActionPerformed method of the action. But when I run the module and click "NEW" in file menu, nothing appears. Where can be problem?

    Read the article

  • How should I link two items with MouseMovementListeners?

    - by user1708810
    I'm working on a program that will display two "views" of the same set of items. So I need to have something set up so that when the top down view is changed, the side view is updated (and vice versa). Here's a brief outline of the relevant code so you can get an idea of my structure so far: public class DraggableComponent extends JComponent { //Contains code for MouseMovementListener that makes the item draggable } public class ItemGraphic extends DraggableComponent { //Code to render the graphic } public class Item { private ItemGraphic topGraphic; private ItemGraphic sideGraphic; } I'm able to get each graphic to display fine in my GUI. I can also independently drag each graphic. I'm missing the "linking." Some ideas I've been thinking about: Have one listener for the whole GUI. Loop through each Item and if the cursor is within the bounds of either graphic, move the other graphic. I'm concerned about the efficiency of this method. Multiple "paired" listeners (not quite sure how this would work, but the idea is that each graphic would have a listener for the other paired graphic)

    Read the article

  • How do I detect the colillison of components?

    - by Coupon22
    How do I detect the collision of components, specifically JLabels (or ImageIcons?)?I have tried this: add(test1); test1.setLocation(x, y); add(test2); test1.setLocation(x1, y1); validate(); if(intersects(test1, test2)) { ehealth-=50; } public boolean intersects(JLabel testa, JLabel testb) { boolean b3 = false; if(testa.contains(testb.getX(), testb.getY())) { b3 = true; } return b3; } When I run this, it does nothing! I used to use rectangle, but it didn't go well with me. I was thinking about an image with a border (using paint.net) and moving an imageicon, but I don't know how to get the x of an imageicon or detect collision. I don't know how to detect collision of a label or increase the location either. I have searched for collision detection with components/ImageIcons, but nothing has came up. I have also searched for getting the x of ImageIcons.

    Read the article

  • Background image in java

    - by Mona
    hi, i'm trying to put an image as a background of my interface in java , i tried to write a class that does that and using it , but is there a simpler way to do that . thanks

    Read the article

  • resizing a ImageIcon in a JButton

    - by Tony
    I am creating a JButton which includes a specific ImageIcon. The main issue is that the original icon size is much bigger than the button size. As result when the button is displayed, only part of the icon can be seen. What is the method that "resize" an ImageIcon i n order to make it fit inside a JButton?

    Read the article

  • create an hotspot and set an image without background (like earth icon) to my panel

    - by sirvan
    Hi I try to create an hotspot by Extends of canvas and I try to add it on a panel witch painted by images , so I must to draw an icon (image) instead of clear rectangle of the screen, to do that I override the paint method to draw the icon I want to use, so far there is no problem, the hotspot work true and the icon painted in true size I want(32,24 pixel) I try to add this hotspot after painting image on the my panel in mypanel.paint(g) that override too. The problem , I use an car icon that have no background !!(I hope you can understand me) just car icon must be show on the panel that painted with my images; But an unwanted rectangle created around the icon and made bad view, How I can paint may icon on panel without that background? Please help me.

    Read the article

  • How to write contents of a JTable to a txt file....

    - by eli1987
    Hi All, I wonder if anyone could kindly tell me how to write the contents of a JTable to a .txt file. I have a basic knowledge of java and know about FileReaders etc, but just don't know how to do something as complicated as this. Thanks-please could you also provide a bit of sample code. Thanks again

    Read the article

  • Changing JTextArea to JScrollPane Causes it to not be visible

    - by user1806716
    I am having an issue with JScrollPanes and JTextArea objects and getting them to work together. If I just add a JTextArea to my JPanel, it works fine and shows up where I tell it to. However, if I change the contentPane.add(textArea) to contentPane.add(new JScrollPane(textArea)), the textArea is not longer visible and there is no sign of the textarea either. Here is my code: public docToolGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 611, 487); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); textField = new JTextField(); textField.setBounds(253, 323, 86, 20); contentPane.add(textField); textField.setColumns(10); JLabel lblEnterRootDirectory = new JLabel("Enter Root Directory"); lblEnterRootDirectory.setBounds(253, 293, 127, 20); contentPane.add(lblEnterRootDirectory); JButton btnGo = new JButton("Go"); btnGo.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { new ToolWorker().execute(); } }); btnGo.setBounds(253, 361, 89, 23); contentPane.add(btnGo); textArea = new JTextArea(); textArea.setWrapStyleWord(true); textArea.setEditable(false); textArea.setBounds(25, 11, 560, 276); contentPane.add(new JScrollPane(textArea)); }

    Read the article

  • How do scroll the contents to bottom by default in JTextPane?

    - by Nitesh Panchal
    Hello, I have JTextPane on my window and i have JTextField. When i press enter enter in JTextField, the text gets added in JTextPane. Everything works fine and scrollbar too appears on its own. But, it doesn't appear properly. It automatically scrolls to the beginning of the content in JTextPane. How do i keep the scrollbar to the end of JTextPane? Please let me know if my question is unclear. Thanks in advance :)

    Read the article

  • How to add clear option to this whiteboard?

    - by swift
    i have to add clear screen option to my whiteboard application, usual procedure is to draw a fill rect to the sizeof the image. But in my app i have transparent panels added one above the other i.e as layers, if i follow the usual procedure the drawing from the underlying panel wont be visible. please tell me any logic to do this. public void createFrame() { JFrame frame = new JFrame(); JLayeredPane layerpane=frame.getLayeredPane(); board= new Whiteboard(client); //board is a transparent panel // tranparent image: board.image = new BufferedImage(590,690, BufferedImage.TYPE_INT_ARGB); board.setBounds(74,23,590,690); board.setImage(image); virtualboard.setImage(image); //virtualboardboard is a transparent panel virtualboard.setBounds(74,23,590,690); JPanel background=new JPanel(); background.setBackground(Color.white); background.setBounds(74,25,590,685); layerpane.add(board,new Integer(5)); layerpane.add(virtualboard,new Integer(4));//Panel where remote user draws layerpane.add(background,new Integer(3)); layerpane.add(board.colourButtons(),new Integer(2)); layerpane.add(board.shapeButtons(),new Integer(1)); layerpane.add(board.createEmptyPanel(),new Integer(0)); }

    Read the article

  • Java Graphics not displaying on successive function calls, why?

    - by primehunter326
    Hi, I'm making a visualization for a BST implementation (I posted another question about it the other day). I've created a GUI which displays the viewing area and buttons. I've added code to the BST implementation to recursively traverse the tree, the function takes in coordinates along with the Graphics object which are initially passed in by the main GUI class. My idea was that I'd just have this function re-draw the tree after every update (add, delete, etc...), drawing a rectangle over everything first to "refresh" the viewing area. This also means I could alter the BST implementation (i.e by adding a balance operation) and it wouldn't affect the visualization. The issue I'm having is that the draw function only works the first time it is called, after that it doesn't display anything. I guess I don't fully understand how the Graphics object works since it doesn't behave the way I'd expect it to when getting passed/called from different functions. I know the getGraphics function has something to do with it. Relevant code: private void draw(){ Graphics g = vPanel.getGraphics(); tree.drawTree(g,ORIGIN,ORIGIN); } vPanel is what I'm drawing on private void drawTree(Graphics g, BinaryNode<AnyType> n, int x, int y){ if( n != null ){ drawTree(g, n.left, x-10,y+10 ); if(n.selected){ g.setColor(Color.blue); } else{ g.setColor(Color.gray); } g.fillOval(x,y,20,20); g.setColor(Color.black); g.drawString(n.element.toString(),x,y); drawTree(g,n.right, x+10,y+10); } } It is passed the root node when it is called by the public function. Do I have to have: Graphics g = vPanel.getGraphics(); ...within the drawTree function? This doesn't make sense!! Thanks for your help.

    Read the article

  • How to make BoxLayout vertical but children flow top-aligned?

    - by user291701
    I'm trying to get get a vertical, top-aligned layout to work. This is what I have: JPanel pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); MyImagePanel panelImage = new MyImagePanel(); panelImage.setSize(400, 400); pane.add(new JComboBox()); pane.add(panelImage); pane.add(new JButton("1")); pane.add(new JButton("2")); pane.add(new JButton("3")); JFrame frame = new JFrame("Title"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 600); frame.add(pane); frame.pack(); frame.setVisible(true); All the controls appear, but it looks like padding is being applied at run time between their tops and bottoms so they're somewhat vertically centered. This is what I'm going for: ----------------------------------------------------- | ComboBox | | ------------ | | | | | Image | | | | | ------------ | | Button 1 | Any additional space fills the right | ------------ | | Button 2 | | ------------ | | Button 3 | | ------------ | | | | Any additional space fills the bottom | | | ----------------------------------------------------- How do I get BoxLayout to do that? Thanks ------------------------- Update ------------------------- Was able to use this: Box vertical = Box.createVerticalBox(); frame.add(vertical, BorderLayout.NORTH); vertical.add(new JComboBox()); vertical.add(new JButton("1")); ... to get what I wanted.

    Read the article

  • How to set the component size with GridLayout? Is there a better way?

    - by Blackbam
    Hello guys, I am working on a larger GUI with Java and I am becoming angry on Layout Managers. I have a "Settings-Panel" with a variable number of JComponents in it (Labels, Buttons, JSpinners, JSliders,...). I just want the following: JLabel <- JComponent JLabel <- JComponent JLabel <- JComponent ... My Panel has a size of 500px, so that there is enough space for a lot of components. Unfortunatly the GridLayout always stretches the size of the Components to the whole Panel, even if I set a MaximumSize for every component. It looks stupid if there are only two buttons each with a height of 250px. I tried flow Layout, but i cannot figure out a way to make new lines properly. I tried BoxLayout.Y_AXIS, but the Components are always centered, and Label and Component are not in the same line. Does anybody know a good and short way with LayoutManagers to handle this properly?

    Read the article

  • Passing.getText() String to another class

    - by DanMc
    I'm currently working on a first year university project and I have a problem, although I doubt it's a very complicated one, but I've been searching and I just can't find a suitable answer to it. The problem concerns two classes. A gui class (class1) and another class (class2). I have a JTextField in class1 and am trying to pass through the .getText() value to class2 and store it in a String type variable. The current code I'm trying to achieve this with is the following: (Class1) private JTextField textField = new JTextField("Something"); ... public String getTextFieldString() { return textField.getText(); } (Class2) private c1 Class1 = new Class1(); private String s = new String(); ... s = c1.getTextFieldString(); I'm pretty new to coding, I've read that maybe I need to pass through an argument somewhere and I assume that's because textField is not static in itself, it changes when somebody enters a new value. (sorry for stating the obvious there.) Anyway, help is appreciated. Thanks a lot!

    Read the article

  • How to get JOptionPane with three text fields

    - by Dr.Mostafa
    I want to know how i can do a messageBox from three input dialog .. Like this: JOptionPane.showInputMessageDialog("Enter your FirstName"); JOptionPane.showInputMessageDialog("Enter your MiddleName"); JOptionPane.showInputMessageDialog("Enter your LastName"); But I want one message has a three input boxes.

    Read the article

  • Resizing JPanel to prepare for printing without removing it from its original position

    - by Lesman
    In my program I frequently need to print various JComponents (generally JPanels) and I like them to be full-page. The way I do it now is by using the following code: g2d.scale(pf.getImageableWidth()/componentToPrint.getWidth(), pf.getImageableHeight()/componentToPrint.getHeight()); but this often stretches or otherwise deforms whatever I am trying to print, and I would much prefer to do something that re-sized intelligently, perhaps a functional version of: componentToPrint.setSize(pf.ImageableWidth(), pf.ImageableHeight); or say adding the component into a new JFrame and then setting the frame size (problem is components can't exist in two place at once). I wouldn't care if the resizing would make the rest of the GUI look terrible, as long as it is something that can easily be reset. Is there any way to do this?

    Read the article

< Previous Page | 32 33 34 35 36 37 38 39 40 41 42 43  | Next Page >